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
Jayesh Deo 2Jayesh Deo 2 

error encountered on after update trigger.

I tried many times but I am getting below error after updating the recruiter record.Please help.
**recruiter record and Hiring Manager records are having exact same fields**

Error:
SyncRecruiterRecords: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] Class.RecruiterRecordsSyncHandler.AfterUpdate: line 36, column 1 Trigger.SyncRecruiterRecords: line 6, column 1
---------------------------------------------------------------------------------------------
  • Hiring_Manager__c =Parent Object
  • Recruiter__c= Child Object (Lookup relation)
---------------------------------------------------------------------------------------------
Trigger=Recruiter Object

trigger SyncRecruiterRecords on Recruiter__c (After update) 
{
      if(trigger.isAfter && trigger.IsUpdate)
        {
    
        RecruiterRecordsSyncHandler.Afterupdate(trigger.New);
            
        }
}
---------------------------------------------------------------------------------------------
Trigger Handler Class:

public class RecruiterRecordsSyncHandler 
{
   
    Public static void AfterUpdate(List< Recruiter__C> lstrecs)
    {
        
        If (!lstrecs.isEmpty())
        {
            List <ID>HrIds= new List<ID>();
            for(recruiter__c  rec: lstrecs)
            {
                
                HrIds.add(rec.Hiring_manager__c);
            }
            Map<id, Hiring_Manager__C> mapHrs = new Map<ID,Hiring_MAnager__C>([select id, name,
                                 location__C, email__c,Phone__C from Hiring_Manager__C
                                         where id in : hrIds]);
            
            if(!MapHrs.isEmpty())
            {
                List<Hiring_manager__C> lstHRS= new list <Hiring_manager__C>();
                
                for(recruiter__c rec:lstrecs)
                {
                    Hiring_Manager__c hr=new Hiring_Manager__c();
                    hr.Name=rec.name;
                    hr.Phone__c=rec.phone__C;
                    hr.Email__c=rec.Email__c;
                    hr.location__c=rec.location__c;
                    
                    lstHRS.add(hr);
                    
                }
                if(!lsthrs.isEmpty())
                {
                    update lsthrs;
                }
            }
        }
    }
    
}
Best Answer chosen by Jayesh Deo 2
AshwiniAshwini (Salesforce Developers) 
Hi @Jayesh Deo 2 ,

The error you have encountered is because you are attempting to update  list of Hiring_Manager__c records without specifying their IDs.

In your RecruiterRecordsSyncHandler class, you are creating new Hiring_Manager__c objects and adding them to a list lstHRS, but you have missed to set their IDs before updating them.

To fix this issue, you need to ensure that you set the IDs of the Hiring_Manager__c records to match the IDs of the corresponding Recruiter__c records. 

You can refer below logic:
public class RecruiterRecordsSyncHandler {
   
    public static void AfterUpdate(List<Recruiter__c> lstrecs) {
        
        if (!lstrecs.isEmpty()) {
            List<ID> HrIds = new List<ID>();
            for (Recruiter__c rec : lstrecs) {
                HrIds.add(rec.Hiring_manager__c);
            }
            
            Map<ID, Hiring_Manager__c> mapHrs = new Map<ID, Hiring_Manager__c>([
                SELECT Id, Name, Location__c, Email__c, Phone__c
                FROM Hiring_Manager__c
                WHERE Id IN :HrIds
            ]);
            
            if (!mapHrs.isEmpty()) {
                List<Hiring_Manager__c> lstHrs = new List<Hiring_Manager__c>();
                
                for (Recruiter__c rec : lstrecs) {
                    if (mapHrs.containsKey(rec.Hiring_manager__c)) {
                        Hiring_Manager__c hr = mapHrs.get(rec.Hiring_manager__c);
                        hr.Name = rec.Name;
                        hr.Phone__c = rec.Phone__c;
                        hr.Email__c = rec.Email__c;
                        hr.Location__c = rec.Location__c;
                        lstHrs.add(hr);
                    }
                }
                
                if (!lstHrs.isEmpty()) {
                    update lstHrs;
                }
            }
        }
    }
}

If this information helps, please mark the answer as best. Thank you

All Answers

AshwiniAshwini (Salesforce Developers) 
Hi @Jayesh Deo 2 ,

The error you have encountered is because you are attempting to update  list of Hiring_Manager__c records without specifying their IDs.

In your RecruiterRecordsSyncHandler class, you are creating new Hiring_Manager__c objects and adding them to a list lstHRS, but you have missed to set their IDs before updating them.

To fix this issue, you need to ensure that you set the IDs of the Hiring_Manager__c records to match the IDs of the corresponding Recruiter__c records. 

You can refer below logic:
public class RecruiterRecordsSyncHandler {
   
    public static void AfterUpdate(List<Recruiter__c> lstrecs) {
        
        if (!lstrecs.isEmpty()) {
            List<ID> HrIds = new List<ID>();
            for (Recruiter__c rec : lstrecs) {
                HrIds.add(rec.Hiring_manager__c);
            }
            
            Map<ID, Hiring_Manager__c> mapHrs = new Map<ID, Hiring_Manager__c>([
                SELECT Id, Name, Location__c, Email__c, Phone__c
                FROM Hiring_Manager__c
                WHERE Id IN :HrIds
            ]);
            
            if (!mapHrs.isEmpty()) {
                List<Hiring_Manager__c> lstHrs = new List<Hiring_Manager__c>();
                
                for (Recruiter__c rec : lstrecs) {
                    if (mapHrs.containsKey(rec.Hiring_manager__c)) {
                        Hiring_Manager__c hr = mapHrs.get(rec.Hiring_manager__c);
                        hr.Name = rec.Name;
                        hr.Phone__c = rec.Phone__c;
                        hr.Email__c = rec.Email__c;
                        hr.Location__c = rec.Location__c;
                        lstHrs.add(hr);
                    }
                }
                
                if (!lstHrs.isEmpty()) {
                    update lstHrs;
                }
            }
        }
    }
}

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
Jayesh Deo 2Jayesh Deo 2
Hi Ashwini,

Thank you for the Answer.
I got that I was missing to collect only hiring manager record IDs  which have associated recruiter records.
I did not implement if() statement suggested because mapHrs collection has already all the associated ID's of recruiter object.(child)


 if(!MapHrs.isEmpty())
            {
                List<Hiring_manager__C> lstHRS= new list <Hiring_manager__C>();
                
                for(recruiter__c rec:lstrecs)
                {
                    Hiring_Manager__c hr=mapHrs.get(rec.Hiring_manager__C);
                    hr.Name=rec.name;
                    hr.Phone__c=rec.phone__C;
                    hr.Email__c=rec.Email__c;
                    hr.location__c=rec.location__c;
                    
                    lstHRS.add(hr);