0

I have two custom fields on user object and if those fields are changed then those same fields on account,contact,opportunity owned by that user should also change accordingly Any suggestions about how to write a trigger for this ?

  • On top of my head, this is what I would do: Loop through the users in the trigger, use Trigger.New & Trigger.Old to check if any of those field changed and store the ID's of those who changed in a set. Do a map for each field, and map the user id to the field value. Select a list of every object you want to update filtering the ownerID with your ownerid set. Loop through the records and user your maps to update the fields. – feddus Nov 5 '15 at 13:15
  • 1
    Why not just have those fields as formula fields? – Jenny B Nov 5 '15 at 13:16
  • 1
    Agreed. If the fields on Account/Contact/Etc are always aligned with the values on the User, just use formula fields. I'll also offer that there are challenges using triggers when working from System-objects to standard objects. You'll have to use @future calls to make the triggers work, which is it's own barrel of fun. – HomerJ Nov 5 '15 at 14:50
0

You can do this using Process builder

  1. create new process

  2. Choose User object as the start object

  3. Add criteria if you want

  4. Add Immediate Action

In immediate action you can map User field value to Account,contact,Opportunity field value.

Hope this helps.

| improve this answer | |
0

trigger Gen_UpdateField on User (before insert,before update) { List userlist =[Select id,name,Micro_Segment__c,Main_Sales_Channel__c from user where id=:UserInfo.getUserId()]; system.debug(userlist); List accountlist =[Select id,name,Main_Sales_Channel__c,Micro_Segment__c from Account where ownerid=:UserInfo.getUserId()]; system.debug(accountlist); list opportunitylist=[Select id,name,Micro_Segment__c,Main_Sales_Channel__c from Opportunity where ownerid=:UserInfo.getUserId()]; system.debug(opportunitylist); list leadlist=[Select id,name,Micro_Segment__c,Main_Sales_Channel__c from Lead where ownerid=:UserInfo.getUserId()]; system.debug(leadlist); if(trigger.isInsert) { for (Account account:accountlist) { account.Micro_Segment__c=userlist[0].Micro_Segment__c; account.Main_Sales_Channel__c=userlist[0].Main_Sales_Channel__c;

}

for (Opportunity opportunity : opportunitylist) { opportunity.Micro_Segment__c=userlist[0].Micro_Segment__c; opportunity.Main_Sales_Channel__c=userlist[0].Main_Sales_Channel__c;

}

for (Lead lead : Leadlist) { lead .Micro_Segment__c=userlist[0].Micro_Segment__c; lead .Main_Sales_Channel__c=userlist[0].Main_Sales_Channel__c;

}
}

if(trigger.isUpdate) { for (Account account:accountlist) { account.Micro_Segment__c=userlist[0].Micro_Segment__c; account.Main_Sales_Channel__c=userlist[0].Main_Sales_Channel__c;

}

for (Opportunity opportunity : opportunitylist) { opportunity.Micro_Segment__c=userlist[0].Micro_Segment__c; opportunity.Main_Sales_Channel__c=userlist[0].Main_Sales_Channel__c;

}

for (Lead lead : Leadlist) { lead.Micro_Segment__c=userlist[0].Micro_Segment__c; lead.Main_Sales_Channel__c=userlist[0].Main_Sales_Channel__c;

}
} }

| improve this answer | |
  • i have written this code but it works only when we create a new record. I want it to work for the old existing records also. What should be done? – Rujuta Mokashi Nov 6 '15 at 6:00
0

Jenny answer is Best, why you are not using Formula fields? You are also avoiding DML by the use of formula fields.

Although it is possible using Trigger or Process unless it is required I would not recommend. Kindly go through your use case again.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.