+ Start a Discussion
wmintunwmintun 

Simple trigger to update a user lookup field on a custom object

Hi

I'm a noob just trying to get my feet wet in the world of APEX and triggers. Help or direction here would be greatly appreciated.

I've got a custom object that tracks CSAT results for Cases. When the CSAT record is created, I want to populate 2 user lookup fields on the CSAT custom object: one with the last modified user on the related case; the other with the manager of the last modified user on the related case. 

I've had a few starts at doing this. But I'm getting stuck on where to start. All the examples I've found seem to take me in circles. 

Thanks!
W
Elie.RodrigueElie.Rodrigue
so basically you need a before insert trigger on the CSAT object.

trigger MyCSATTrigger on CSAT__c(before insert) { }

That trigger receive a list of newly inserted object. You need to iterate through them :

trigger MyCSATTrigger on CSAT__c(before insert)
{
     for(CSAT__c cs : trigger.new)
     {
          // insert processing code here.
     }
}

To ensure your trigger is bulkified, you need to populate a list of cases to query, to get the lastmodifiedby user and its manager.


trigger MyCSATTrigger on CSAT__c(before insert)
{
     List<Id> caseIds = new List<Id>();
     //Populate a list of all related cases
     for(CSAT__c cs : trigger.new)
     {
          caseIds.add(cs.Case__c);
     }
     if(caseIds.size()>0)
     {
          //Query those cases
          List<Case> cases = [Select Id, LastModifiedById, LastModifiedBy.ManagerId from Cases where Id in: caseIds];
         
     }
}


This way you have a list of all related cases.

You will now need to use a map, so you can find the case back without itering through each entries; then looping again through your newly created object to assign proper values.

trigger MyCSATTrigger on CSAT__c(before insert)
{
     List<Id> caseIds = new List<Id>();
     //Populate a list of all related cases
     for(CSAT__c cs : trigger.new)
     {
          caseIds.add(cs.Case__c);
     }
     if(caseIds.size()>0)
     {
          //Query those cases
          List<Case> cases = [Select Id, LastModifiedById, LastModifiedBy.ManagerId from Cases where Id in: caseIds];
   //Create a map
          Map<Id,Case> casesMap = new Map<Id,Case>(cases);
          //Update the CSAT__c objects
          for(CSAT__c cs : trigger.new)
          {
               //Only process if there's a case mapped
               if(casesMap.containsKey(cs.Case__c))
        {
       //Set the values
       cs.CaseLastModifiedBy__c = casesMap.get(cs.Case__c).LastModifiedById;
       cs.CaseLastModifiedByManager__c = casesMap.get(cs.Case__c).LastModifiedBy.ManagerId;
         }
     }
}


Don't forget to set as the answer if it helped you!
wmintunwmintun
This is great. Thanks!

I'll try it today and let you know.

- W
Elie.RodrigueElie.Rodrigue
Can't wait to know if it helped you!
wmintunwmintun
Worked perfectly! Thank you so much!

- W
Mattan LifshitzMattan Lifshitz
Hi Elie

I deal with a similar request only without the CSAT custom object, but the regular Case object (plus - i'm trying to populate the createdBy user's manager)
I tried to create a copy of this trigger with few adjustments somehow it did not work well

Could you give me a help? 

Thanx a lot!