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
KimKim 

Trigger Error: First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []:

Hello, 

New to writing trigger so I'm unsure why I'm getting the Id is not specified error. Goal is if an existing user record's Channel OR LOB field  is updated then, it should look for existing User History record and update the Role_End_Date__c and create a new history record with for that same user with the Role_Start_Date__c as today. 
 
public class UserHistory_CreateUpdateFromUser  {

	public static void findUser(List<User> newUsers, Map<Id,User> oldUsers, Boolean isInsert){

		List<User> newUserList = new List<User>(); 
		List<User> lOBChannelList = new List<User>();
		List<User> isActiveList = new List<User>(); 
		List<User> uManagerLOBChannelList = new List<User>();
        List<User> uManagerList = new List<User>(); 
		Map<Id,List<User>> reasonForUpdatingUser = new Map<Id,List<User>>(); 
			
       	for(User u : newUsers){
            //New user && Channel != Non Sales  
            if(u.Channel__c != 'Non Sales'){
                if(isInsert){ 
       				newUserList.add(u); 
       				System.debug('newUserList: '+ newUserList); 
       			}
                //Existing user && Channel || LOB is changed 
       			else if(!isInsert && (u.Channel__c != oldUsers.get(u.Id).Channel__c || u.LOB_Focus__c != oldUsers.get(u.Id).LOB_Focus__c)) {
       				lOBChannelList.add(u); 
       				System.debug('lOBChannelList: '+ lOBChannelList); 
       			}
       			//Existing user && deactived 
       			else if(!isInsert && u.IsActive != oldUsers.get(u.Id).IsActive){
       				isActiveList.add(u); 
       				System.debug('isActiveList: '+ isActiveList);
                }
                //Existing user && Manager is changed && Channel || LOB is changed 
       			else if(!isInsert && u.ManagerId != oldUsers.get(u.Id).ManagerId &&
                        (u.Channel__c != oldUsers.get(u.Id).Channel__c || u.LOB_Focus__c != oldUsers.get(u.Id).LOB_Focus__c)){
       				uManagerLOBChannelList.add(u); 
       				System.debug('uManagerLOBChannelList: '+ uManagerLOBChannelList);
       			}
       			//Existing user && Manager is changed 
                else if(!isInsert && u.ManagerId != oldUsers.get(u.Id).ManagerId){
                    uManagerList.add(u); 
                    System.debug('uManagerList: '+ uManagerList);
                }
            }     
       	}

       	if(newUserList.size()>0){
       		newUser(newUserList);
       	}	

       	if(lOBChannelList.size()>0){
       		lOBChannelUpdate(lOBChannelList); 
       	}

       	if(isActiveList.size()>0){
       		userisActiveUpdate(isActiveList); 
       	}

       	if(uManagerList.size()>0){
       		managerUpdateOnly(uManagerList); 
       	}

       	if(uManagerLOBChannelList.size()>0){
       		managerLOBChannelUpdate(uManagerLOBChannelList); 
       	}
       
	}

	public static void lOBChannelUpdate(List<User> lOBChannelList){
		//Find existing User History record and update end date fields 
		//Create new user history record and update fields based on user changes 

		List<Id> userIds = new List<Id>(); 
		List<User_History__c> existingUHtoUpdate = new List<User_History__c>(); 
		List<User_History__c> newUHtoInsert = new List<User_History__c>(); 

		for(User u : lOBChannelList){
			userIds.add(u.Id); 
		}
		System.debug('userIds: '+ userIds);
		System.debug('lOBChannelList: '+ lOBChannelList);

		if(userIds.size()>0){
			List<User_History__c> eUH = [SELECT Id, User__C, Role_End_Date__c, Manager_End_Date__c FROM User_History__c
											WHERE User__c =:userIds]; 
            System.debug('eUH' + eUH); 

			for(User_History__c uH : eUH){

                User_History__c existingUH = new User_History__c(); 
                
                if(uH.Role_End_Date__c == NULL){
					existingUH.Role_End_Date__c = Date.today(); 
					existingUH.Id = uH.Id;
                }
                else if(uH.Manager_End_Date__c == NULL){
					existingUH.Manager_End_Date__c = Date.today(); 
					existingUH.Id = uH.Id;
                }
                else if(uH.Role_End_Date__c == NULL && uH.Manager_End_Date__c == NULL){
                    existingUH.Role_End_Date__c = Date.today(); 
                    existingUH.Manager_End_Date__c = Date.today(); 
                    existingUH.Id = uH.Id;
                }
				existingUHtoUpdate.add(existingUH); 
			}
			System.debug('existingUHtoUpdate: '+ existingUHtoUpdate);

			for(User u1 : lOBChannelList){
                
				User_History__c newUH = new User_History__c(
				User__c = u1.Id, 
				Channel__c = u1.Channel__c,  
				LOB_Focus__c = u1.LOB_Focus__c,  
				Role_Start_Date__c = Date.today(), 
				Manager_Start_Date__c = Date.today()
			); 
				newUHtoInsert.add(newUH); 
				
			}	
			System.debug('newUHtoInsert: '+ newUHtoInsert);
		}

		if(!existingUHtoUpdate.isempty()){
			Database.update(existingUHtoUpdate); 
		}
		System.debug('Databaseupdate existingUHtoUpdate: ' + existingUHtoUpdate); 

		if(!newUHtoInsert.isempty()){
			Database.insert(newUHtoInsert);
		}

	}
I've checked on the debug log that it is finding the Id of the existing User History record when I do the query and added it on the list 'eUH' and looped through that list. 

 
Wilfredo Morillo 20Wilfredo Morillo 20
Try This: 
public class UserHistory_CreateUpdateFromUser  {

    public static void findUser(List<User> newUsers, Map<Id,User> oldUsers, Boolean isInsert){

        List<User> newUserList = new List<User>(); 
        List<User> lOBChannelList = new List<User>();
        List<User> isActiveList = new List<User>(); 
        List<User> uManagerLOBChannelList = new List<User>();
        List<User> uManagerList = new List<User>(); 
        Map<Id,List<User>> reasonForUpdatingUser = new Map<Id,List<User>>(); 
            
        for(User u : newUsers){
            //New user && Channel != Non Sales  
            if(u.Channel__c != 'Non Sales'){
                if(isInsert){ 
                    newUserList.add(u); 
                    System.debug('newUserList: '+ newUserList); 
                }
                //Existing user && Channel || LOB is changed 
                else if(!isInsert && (u.Channel__c != oldUsers.get(u.Id).Channel__c || u.LOB_Focus__c != oldUsers.get(u.Id).LOB_Focus__c)) {
                    lOBChannelList.add(u); 
                    System.debug('lOBChannelList: '+ lOBChannelList); 
                }
                //Existing user && deactived 
                else if(!isInsert && u.IsActive != oldUsers.get(u.Id).IsActive){
                    isActiveList.add(u); 
                    System.debug('isActiveList: '+ isActiveList);
                }
                //Existing user && Manager is changed && Channel || LOB is changed 
                else if(!isInsert && u.ManagerId != oldUsers.get(u.Id).ManagerId &&
                        (u.Channel__c != oldUsers.get(u.Id).Channel__c || u.LOB_Focus__c != oldUsers.get(u.Id).LOB_Focus__c)){
                    uManagerLOBChannelList.add(u); 
                    System.debug('uManagerLOBChannelList: '+ uManagerLOBChannelList);
                }
                //Existing user && Manager is changed 
                else if(!isInsert && u.ManagerId != oldUsers.get(u.Id).ManagerId){
                    uManagerList.add(u); 
                    System.debug('uManagerList: '+ uManagerList);
                }
            }     
        }

        if(newUserList.size()>0){
            newUser(newUserList);
        }   

        if(lOBChannelList.size()>0){
            lOBChannelUpdate(lOBChannelList); 
        }

        if(isActiveList.size()>0){
            userisActiveUpdate(isActiveList); 
        }

        if(uManagerList.size()>0){
            managerUpdateOnly(uManagerList); 
        }

        if(uManagerLOBChannelList.size()>0){
            managerLOBChannelUpdate(uManagerLOBChannelList); 
        }
       
    }

    public static void lOBChannelUpdate(List<User> lOBChannelList){
        //Find existing User History record and update end date fields 
        //Create new user history record and update fields based on user changes 

        List<Id> userIds = new List<Id>(); 
        List<User_History__c> existingUHtoUpdate = new List<User_History__c>(); 
        List<User_History__c> newUHtoInsert = new List<User_History__c>(); 

        for(User u : lOBChannelList){
            userIds.add(u.Id); 
        }
        System.debug('userIds: '+ userIds);
        System.debug('lOBChannelList: '+ lOBChannelList);

        if(userIds.size()>0){
        /*  List<User_History__c> eUH = [SELECT Id, User__C, Role_End_Date__c, Manager_End_Date__c FROM User_History__c
                                            WHERE User__c =:userIds];*/
             
              //WilfredoMorillo - Get the existing User History and do the changes. 
             existingUHtoUpdate =[SELECT Id, User__C, Role_End_Date__c, Manager_End_Date__c FROM User_History__c
                                            WHERE User__c =:userIds];
 
            System.debug('existingUHtoUpdate ' +existingUHtoUpdate ); 

            for(User_History__c uH : existingUHtoUpdate ){
                //Not use a new one.
              //  User_History__c existingUH = new User_History__c(); 
                
                if(uH.Role_End_Date__c == NULL){
                    uH.Role_End_Date__c = Date.today(); 
                    
                }
                else if(uH.Manager_End_Date__c == NULL){
                    uH.Manager_End_Date__c = Date.today(); 
                    
                }
                else if(uH.Role_End_Date__c == NULL && uH.Manager_End_Date__c == NULL){
                    uH.Role_End_Date__c = Date.today(); 
                    uH.Manager_End_Date__c = Date.today(); 
                    
                }
                 
            }
            System.debug('existingUHtoUpdate: '+ existingUHtoUpdate);

            for(User u1 : lOBChannelList){
                
                User_History__c newUH = new User_History__c(
                User__c = u1.Id, 
                Channel__c = u1.Channel__c,  
                LOB_Focus__c = u1.LOB_Focus__c,  
                Role_Start_Date__c = Date.today(), 
                Manager_Start_Date__c = Date.today()
            ); 
                newUHtoInsert.add(newUH); 
                
            }   
            System.debug('newUHtoInsert: '+ newUHtoInsert);
        }

        if(!existingUHtoUpdate.isempty()){
            Database.update(existingUHtoUpdate); 
        }
        System.debug('Databaseupdate existingUHtoUpdate: ' + existingUHtoUpdate); 

        if(!newUHtoInsert.isempty()){
            Database.insert(newUHtoInsert);
        }

    }
Instead of create a new list of User History, get all existing and work with the same one eUH. Let me know if it works for you. 

Wil, 
Srinivasa Chary TaduriSrinivasa Chary Taduri
Use upsert instead of update in the below line
Database.update(existingUHtoUpdate);
Ajay K DubediAjay K Dubedi
This error means that your Org does not contain the  record IDs that are required for matching to update existing records.
So you should replace 
Database.update(existingUHtoUpdate); [line -> 121]with    Database.upsert(existingUHtoUpdate);