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
ABC XYZ 39ABC XYZ 39 

Both after insert and After update triggers for record creation and updation scenarios; Multiple records getting pushed to queue object

Hi, 
Basic Logic:
 Create Contact Scenario - 
 Conditions - Last Name should not be same as Account Name, Contact type is Customer
  Everytime a new contact is created meeting the above conditions, it is written to Queue object
   In this case, Description would be 'New Contact' (to be written into Queue object)
   
 Update Contact Scenario -
 2 scenarios here -   
  1) In addition to abouve conditions being valid, if custom field PID is missing and any of the 
    fields in Contact record are updated, push the record to Queue object.
    In this case, Description would be 'PID Missing'
    
  2)  In addition to above conditions - Last Nane not same as Account Name, Contact type being 'Customer'
     type,  if custom fields A, B, C or D are changed on the existing record, push the record to 
       Queue object.
      In this case, Description would be 'Updated Contact' 
  
Behavior Observed Now :
Whenever a  new contact  is created, 2 copies of the same record are inserted into the Queue object. This I am assuming is happening because 'after insert' and 'after update' triggers aer firing for every new contact created. 

Insert Scenario(New Contact Creation) - Update scenario triggered - 'MDM Id Missing' message observed; Record inserted into Queue object twice
Update Scenario (PID null and record updated) - Working as expected - 'MDM Id Missing' message written into Queue object
Update scenario ( A,B,C or D field values change) - Update Scenario triggerd - 'Update contact' message written into Queue object



I don't understand why this is happening. There are no workflows impacting the behavior . It has to do with logic in the class below or the way I am invoking the trigger. 
Controller Class and Trigger Invocation are below.

I am trying to fix this code for sometime without any luck. I need to fix it as soon as possible. I am new to programming and can't figure out how to make this functionality work.
Please help. Thanks.









// Controller Class with processContact method

public class ContactController {

public  void processContact(Map<Id, Contact> newContactMap, Map<Id, Contact> oldContactMap)
{
       List<Queue__c> lstQ = new List<Queue__c>();   
         Id idContactCustomerRecType;
         Map<Id, String> mapIdToAccountName = new Map<Id, String>();
       
           List<RecordType> lstRecType = new List<RecordType>([SELECT Id FROM RecordType WHERE Name = 'Customer' AND SobjectType = 'Contact']);
          
      if(!lstRecType.isEmpty())
     idContactCustomerRecType = lstRecType[0].Id;
       
        if(idContactCustomerRecType != null)
         {
         // we can't fetch parent fields directly, so collecting account Ids first
           for(Contact objContact : newContactMap.values())
              {
                  if(objContact.AccountId != null)
                mapIdToAccountName.put(objContact.AccountId, null);
              }
          }
 
         // iterate over account with matching collected Ids and fetch names
          for(Account objAccount : [SELECT Id, Name FROM Account WHERE Id IN : mapIdToAccountName.keySet()])
          {
           mapIdToAccountName.put(objAccount.Id, objAccount.Name);
           }
  
         // loop again to perform business logic
        for(Contact objContact : newContactMap.values())
        {
             /* 1. If Contact RecordType is Customer AND if LastName != Account.Name, insert Queue **/
            if( objContact.RecordTypeId == idContactCustomerRecType
               && objContact.AccountId != null   && objContact.LastName != mapIdToAccountName.get(objContact.AccountId)
             {
             if(Trigger.isUpdate)
             {
                 if(label.Unauthorized_User!=objContact.lastmodifiedbyid)
                 {
                    if(objContact.A__c!= oldContactMap.get(objContact.Id).A__c
                        || objContact.B__c!= oldContactMap.get(objContact.Id).B__c
                        || objContact.D__c!= oldContactMap.get(objContact.Id).D__c)
                  {
                  
                   objQ.Description__c= 'Updated Contact';
                   objQ.Record_Id__c = objContact.Id;
                   objQ.Notification_Timestamp__c= objContact.CreatedDate;
                   lstQ.add(objQ);
                   }
                   
                   else if(objContact.PID__c == null)
                   {
                   objQ.Description__c= 'PID Missing';
                   objQ.Record_Id__c = objContact.Id;
                   objQ.Notification_Timestamp__c= objContact.CreatedDate;
                   lstQ.add(objQ);
                   
                   }
                   
                 }
             }
             
               if(Trigger.isInsert)
               {
                 objQ.Description__c= 'New Contact';
                   objQ.Record_Id__c = objContact.Id;
                   objQ.Notification_Timestamp__c= objContact.CreatedDate;
                   lstQ.add(objQ);
                   
               }
             }

            
                   
                
       }

       if(!lstQ.isEmpty())
       insert lstQ;
             
    }
     
    
     
}


Trigger Invocation :

Contact - After Insert 

if(Trigger.isInsert && Trigger.isAfter){
   ContactController.processContact(Trigger.newMap, Trigger.oldMap);
   }

Contact - After Update 

 if(Trigger.isUpdate && Trigger.isAfter){
        ContactController.processContact(Trigger.newMap, Trigger.oldMap);
    } 
Rohit Sharma 66Rohit Sharma 66
Please try putting system.debug('************'+ListName); and see the debug log where its creating the duplicate records.