function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bhu'1 salesforceBhu'1 salesforce 

Need to bulkify this code ? I have tried it, but still failing the some bulkification issue?

Please help me in bulkifying this code.
Thanks in advance..

Code :
global with sharing class LOYMemberServcies
{
   
    //-----------------------------------------------------------------
    //---- Member Enrollment
    //-----------------------------------------------------------------
     
    //Services that needs to be invoked after the member enrollment.
    //This method will be called from the member trigger.
    //Modified to exclude the DML operation inside loop
    public Map<Membership_card__C,Member__c> postMemberEnrollmentActivities(Map<String,Member__C> newMem)
    {
        //Get the base tier
        system.debug('%%%'+newMem);
      Map<Membership_card__C,Member__c> mapCardMember = new Map<Membership_card__C,Member__c>();
      for(String ss : newMem.keyset()){
        //create new membership card
        Member__c updateMem = new Member__c();
        updateMem = newMem.get(ss);
              system.debug('%%%'+updateMem );
        Membership_card__C mc = new Membership_Card__C();
        mc.member__c = updateMem.Id;
        List<String> tierClass = ss.split(',');
        mc.card_type__C = tierClass[0];
               
        //udpate hte member record
          updateMem.tier__C = tierClass[1];
          //updateMem.membership_card__C = mc.id;
          mapCardMember.put(mc,updateMem);
          
      }
      return mapCardMember;
    }
     
    //Enroll the contact into a given program.
    //If the contact id is present, then duplicate check will be done to ensure one contact is part of one program only once.
        
    global static List<String> EnrollTheContactIntoProgram(List<Contact> con,String programName)
    {
        List<String> result = new List<String>();
        Program__c program=[select id,Name from Program__c where name=:programName limit 1];
        List<Contact> newCon = new List<Contact>();
        Map<Id,Contact> existCon = new Map<Id,Contact>();
        List<Id> conId = new List<Id>();
        for(contact c : con){
          String contactId = c.Id;
        
        system.debug('***'+c);
        
        //check if contact id is null (new contact)
        if(contactId == null)
        {
            //create the new contact
            newCon.add(c);
            // then create a new member..   
            }
        else
        {
               conId.add(c.Id); //check the duplicate of hte contact.
               existCon.put(c.Id,c);
               system.debug('im in else'+existCon);
        }       
        }
        if(newCon.size()>0)
        {
        insert newCon;
}        
        
         List<Member__C> membersWithSameContact = [select id,Contact__c from Member__C where Contact__C IN :conId and Program__C = :program.ID LIMIT 1];
                //check if htere is atlest one membership for the contact in the same program
        system.debug('******'+membersWithSameContact);
     
     for(id i : existCon.keyset())
     {
        if(membersWithSameContact.size()>0)
        {
        for(Member__c m : membersWithSameContact)
          {
            
          if(existCon.get(m.Contact__c) != null )
                {
                    result.add( ' The contact is already part of the same Program'+m.Id);
                }
                else
                {
                    //create the new member for the existing contact.
                    newCon.add(existCon.get(m.Contact__c) );
                   }   
        
        
          }  
          }
          else
          {
           newCon.add(existCon.get(i) );
          }
          }   
        
        
  system.debug('****'+newCon);
        List<Member__c> newMem = new List<Member__c>();
        for(Contact cs : newCon){
        Member__c newMember = new Member__C();
        newMember.program__C = program.Id;
        newMember.Contact__C = cs.Id;
        newMember.Name = cs.Name;
        newMem.add(newMember);
        }                  
        insert newMem;
        for(Member__c m : newMem)
        {
            result.add('Contact Id '+m.Contact__C+' has been enrolled into the program. Member Number: '+m.Id);
        }        
            
            
        
        return result;
    }
}
Vasani ParthVasani Parth
"EnrollTheContactIntoProgram" method is being called many times from some other code ? To address that problem you would need to change the method signature to accept e.g. a set of program names and then modify both the calling code and the EnrollTheContactIntoProgram implementation.