+ Start a Discussion
Saad Ahmad 27Saad Ahmad 27 

Trigger to Auto Populate Lookup Fields based on field values

I have 10 look up fields that need to be updated based on 10 text fields on the custom object. The text field contains the user ID of a person and this field is used to fill out the look up field. 
e.g. CDS User ID (text field) = 12345, the look up field (related to User object) will be updated with user who's federation identifier = 12345.

I have written a trigger to update 1 look up field but i'm unable to expand this to include all 10 fields.
 
trigger UpdateUserOnRepairHistory on Repair_History__c (before insert, before update) {
    
    Map<String, Repair_History__c> RepHistorytoUser = new Map<String, Repair_History__c>();
    
    for (Repair_History__c rH:Trigger.new)
    {
        RepHistorytoUser.put(rH.CDS_End_User_ID__c, rH);
    }
	
    List<User> userList = [SELECT Id, FederationIdentifier from User where FederationIdentifier in :RepHistorytoUser.keySet()];
    
    for (User usr : userList){
        RepHistorytoUser.get(usr.FederationIdentifier).CDS_End_User_Name__c = usr.Id;
    }
    
}