0

I have a custom object opp__c, which has fields contId and role. I wrote after insert trigger where it will be get acctId from contact object. then checks in AcctcontRole with acctid,contId and role if record already exists checks role is null update the record with new role else insert record into acctcontrole. it is working fine for single record, i want to bulkify this, please help

trigger InsACR on Opp__c (after Insert) {
  
     if (trigger.isinsert) {
     for(Opp__c OCR:Trigger.new) {
     
            contact cont = [select Id,accountId from contact where Id = :OCR.cont__c];
            
          if (cont.accountId != null) {
             
                  
                 
                List<AcctcontRole> ACR = [select contactId,AccountId,Role from AcctcontRole where contactId = :OCR.cont__c and accountId = :cont.accountId and role = :OCR.role__c Limit 1];
                  
            if(!ACR.isEmpty()){
                        
  
               system.debug('Do Nothing -- Contact Role already Exists in the AccountContactRole Object');
                                      
            }
             else
            {
                List<AcctConttRole> ACR1 = [select contactId,AccountId,Role from AcctcontRole where contactId = :OCR.cont__c and accountId = :cont.accountId and role = '' Limit 1]; 
                                                       
            if(!ACR1.isEmpty())
            {
               ACR1[0].role = OCR.role__c;

               update ACR1;

            } 
             else 
            {   
               AcctcontRole acctctrole = new AcctcontRole();
               acctctrole.accountId = cont.accountId;
               acctctrole.contactId = OCR.cont__c;
              

               insert acctctrole;
            } 
         }    
      }                                                   
   }    
}
  • Are your certain that your opp__c doesn't have an account__c field with the accountId associated with the opp__c? – crmprogdev Jun 21 '15 at 16:04
2
trigger InsACR on Opp__c (after Insert) {
Set<Contact>cons = new set<Conntact>();
map<Id,string>con2ACR = new map<Id,string>();
map<Id,Id>con2acct = new map<Id,Id>();
list<AccountContactRole>ACRin = new list<AccountContactRole>();
list<AccountContactRole>ACRup = new list<AccountContactRole>();


   if (trigger.isinsert && trigger.isAfter) 
   {

      for(Opp__c OCR:Trigger.new) {

         cons.add(OCR.OCR.cont__c);
         con2ACR.put(OCR.OCR.cont__c,OCR.OCR.role__c);

      }

      list<contact>cont = [select Id,accountId from contact where Id IN :cons];

      For(contact c:cont)
      {
        con2acct.put(c.Id,c.accountId);

      }

      List<AccountContactRole> ACR1 = [select contactId,AccountId,Role from AccountContactRole where contactId IN :cont AND accountId IN : :con2acct.values AND role IN :con2ACR.values]

      for(AcountContactRole a:ACR1)
      {
          ctc2ACR.put(a.contactId,a.id);
      }

      set<Id>ACRctcIds = ctc2ACR.keyset();

      for(contact cont:cons)
         {
          If(ACRctcIds.contains(cont) == true)
          {
             AccountContactRole acctctrole = new AccountContactRole(id=ctc2ACR.get(cont));
             acctctrole.accountId = cont.accountId;
             acctctrole.contactId = cont.Id;
             acctctrole.role = con2ACR.get(cont.Id);

             ACRup.add(acctctrole);                
         } 
          else
         {

            AccountContactRole acctctrole = new AccountContactRole();
            acctctrole.accountId = cont.accountId;
            acctctrole.contactId = cont.Id;
            acctctrole.role = con2ACR.get(cont.Id);

            ACRin.add(acctctrole);

         }                          
      }
      if(ACRup.isEmpty() == false) update ACRup;
      if(ACRin.isEmpty() == false) insert ACRin;
   }   
}

Edit

Initially, I was thinking you wouldn't need to query AccountContactRole and could do an upsert. After thinking about it, based on something I recall reading in the object reference, I became concerned that a new ACR record would be created instead of updating the existing record for a contact. For that reason, I've revised my code to query ACR and then do either an update or an insert.

| improve this answer | |
  • Opp__c don't have acctId, it is custom object duplicate of opportunity contact role, as we cannot create triggers on it we created custom one – User777 Jun 21 '15 at 16:29
  • see my edits. I think your query for ACR may be necessary, so have revised code. – crmprogdev Jun 21 '15 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.