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
Maciej GuniaMaciej Gunia 

Trigger to Copy value from Custom Object field to Users field

Hello,

 

I am very new to that forum, so at the beginning I want to say "hello" to everyone!

 

I am new to APEX and I face the problem which APEX trigger seems to be a solution. I would need a trigger in Salesforce which would update the field located in Users object (standard object) using the value from Holidays Remaining field from Staff object (custom object). In other words, what is populated in Holidays Remaining in Staff it should be copied to the field called holidays Remaining in the Users level. Could anyone help me with that please?

Tim BarsottiTim Barsotti

Are the staff and user objects related? 

 

I would create a hierarchical relation on the user object (this is a field) and then a formula field to lookup the values. Try to avoid APEX whenever possible :-)

Maciej GuniaMaciej Gunia

Hello Tim,

 

What do you mean by making a hierarchical relation. As far as I understand this kind of realtion can be done to indicate who is the manager of this person. Staff object is a auto-number list where records are created by the HR person. So how should I apply your solution to it?

 

Regards,

Maciej

Grazitti InteractiveGrazitti Interactive

Hi,

 

As you have explained above, I think there is Parent-Child relationship (either Lookup or Master-Detail) between Staff__c and User. User is Parent and Staff__c is Child and User and Staff__c both have field Holiday_Remaining__cthen use following code to populate Holiday_Remaining__c on User from Holiday_Remaining__c  of Staff_c -

 

trigger UpdateHolidayRemaining on Staff__c (After insert,After update) {

    Map<String, Staff__c> userMap = new Map<String, Staff__c>();

    List<User> usersTobeUpdated = new List<User>();

   for(Staff__c stf : Trigger.New){

         userMap .put(stf.User__c,stf);

    }

   for(User usr : [Select Holiday_Remaining__c from User where Id IN :userMap.keySet() ]){

       usr.Holiday_Remaining__c = userMap.get(usr.Id).Holiday_Remaining__c;

       usersTobeUpdated.add(usr);

  }

  if(!usersTobeUpdated.isEmpty())

      try{update usersTobeUpdated;}catch(Exception e){}

}

 

/**If this post helps you then please don't forget to give me kudo's by clicking star aside and mark it as a solution.***/

 

Thanks

Grazitti

Maciej GuniaMaciej Gunia
Hi Grazitti,

When I use your trigger code i get the error: "Compile Error: unexpected token: ';' at line 2 column 63". Could you please advise about the amendments needed?
Tim BarsottiTim Barsotti
 
trigger UpdateHolidayRemaining on Staff__c (After insert,After update) {
    Map<ID, Staff__c> userMap = new Map<ID, Staff__c>();
    List<User> usersTobeUpdated = new List<User>();
   for(Staff__c stf : Trigger.New){
         userMap.put(stf.User__c,stf);
    }
   for(User usr : [Select Holiday_Remaining__c from User where Id IN :userMap.keySet() ]){
usr.Holiday_Remaining__c = userMap.get(usr.Id).Holiday_Remaining__c;
usersTobeUpdated.add(usr); } if(!usersTobeUpdated.isEmpty()) try{update usersTobeUpdated;}catch(Exception e){} }

 

Grazitti InteractiveGrazitti Interactive

Hi,

 

Sorry for getting late, I have upadated my trigger  above and also please make sure that the API name of of Custom Object and fields are same as your organization.

Maciej GuniaMaciej Gunia
Hi Guys,

I tried both Grazitti's and Tim Barsotti's code but it doesn't return me any values. The field in a User level remains blank. At the beginning my Holidays remaining (Staff level) was a Formula(Number). I had to change this to Text as the code was not accepting decimals. But changing it to Text didn't help much as the field doesn't populate. Any idea where is the problem?