+ Start a Discussion

user lookup field trigger


I am hoping someone can help. 

I have 2 custom user look up fields on the user object, (approver 1 and approver 2)

I am looking for a way to update the lookup fields (approver 1 and approver 2)  on a custom object, when the approver 1 and approver 2 fields on the user record are updated. 


Thank you in advance for your help!


For lookup fields, you have update with the object's Id.

To update the Approver1 lookup field, you have give User1.Id


trigger myTestTrigger on Custom__c (after update){
  for(Custom__c c : Trigger.new){
    User.Approver1__c = c.ApproverUser1__c.Id;

 Note: Above is just an example. It does not contain full code to solve your need.


I am new to building triggers and i am not sure what you mean by this.

Do I have to hard code the user id in the trigger?


I would like to be able to use data loader to update these to fields on the user object, and have the trigger update the same fields on the custom object with the new field values.


You can use Apex Loader to update fields in bulk for already existing records that have not updated yet.

Triggers execute on the fly. If user creates new record of a object or edits or deletes or undelete from Recycle bin, trigger fires & do the operation you have specified in it.Triggers also execute if you use Apex Loader to update a sObject in bulk. You just have to bulkify you code inside your trigger to handle more than 100 DML statments, etc.

For more help see here Trigger help

Sample code given in that link will help you.