1

I initially wrote the below trigger to update opportunity field based on value in account field. However, it should be other way around.

Explaining: When an opportunity is created/Edited and if the lookup field at account level called Sales_Manager__c in not null, the same value should be populated onto Opportunity lookup field Sales_Manager__c

trigger updateOppSalesManager on Account (After update)  
    {
    /* checking the value of sales manager at account level by looping through Opportunities */ 
      List<Opportunity> opps = [
            Select Id, sales_manager__c, account.sales_manager__c
            From Opportunity
            Where AccountId In :Trigger.newMap.keySet()
        ];

        /* Updating opportunity field with the value from account Sales_manager__c*/
        for (Opportunity opp : opps) {
            opp.sales_manager__c = opp.Account.sales_manager__c;
        }
        update opps;

}

I tried in every possible way i can by using set: to set account id , list: to loop through opportunity with the account ID , also used maps, but with no luck. Any Help, Suggestion, Guidence will be very useful for me.

Thank you Sam

1
  • 1
    You should try using Process Builder instead.
    – Adrian Larson
    Commented Oct 1, 2015 at 18:21

3 Answers 3

1

You could try something like this. Please forgive any typos.

trigger updateOppSalesManager on Account (After update)  
    {
    //Create Sets and Lists
    Set<ID> OppAcc = New Set<id>();
    List<Opportunity> opps = New List<Opportunity>();

    //Loop through inbound opps and add the accountid to the ID set
    For(Opportunity o : Trigger.New)
    {
        OppAcc.add(o.Accountid);
    }    

    Map<id, account> AccMap=new Map<id, account>();
    AccMap.putAll([select id, Sales_Manager__c from Account where id in :OppAcc]);    

    //Loop back through the same opps. Link the account through the map above, then match the 2 fields. Finally, add the opp to a list for bulk updates.
    For(Opportunity o : Trigger.New)
    {
        Account a = AccMap.get(o.Account);
        o.sales_manager__c = a.sales_manager__c;
        opps.add(o);
    }    

    //make sure that you are only running an update if anything is in the list to update. Otherwise, you will get an error message.
    if(opps.size() > 0)
    {
        update opps;
    }

}
1
  • Hi smckitrick , thank you for the code. But the idea is a user updated accounts then this will work but what if a new opportunity created or existing one edited. can i use the same at opportunity level. Because the Sales_manager__c is updated at account ?
    – sam
    Commented Oct 2, 2015 at 9:16
1

You can accomplish this in the process builder on the Opportunity object.

Steps:

  1. Select the Opportunity Object and start when created or edited
  2. Set Criteria to look at the related Account Sales Manager and see if it is not null and if it does not match what is already on oppty record.
  3. Update Sales Manager on Opportunity with a Reference to the related Account Sales Manager.
6
  • Thank you for your reply Jenny <br/> Yes, but the later part of the process is to use this field to send out emails alerts to users on Opportunity stage changes. I have created workflow rule to do that. Does Process builder and workflow rule work in tandem ? <br/> Also when the field is updated at account level does the value get updated at opportunity level ?
    – sam
    Commented Oct 1, 2015 at 18:27
  • @sam Could you move the email notification to be a 2nd action after the field update, then everything would be in one spot?
    – Jenny B
    Commented Oct 1, 2015 at 18:30
  • @sam in this example only when the opportunity is edited/created with the process run - you would need a separate process on the account to then update the children records.
    – Jenny B
    Commented Oct 1, 2015 at 19:16
  • Please correct me if i am wrong but, i think i can trigger the field update on the custom Lookup at opportunity level field by saying Sales_magaer__c <> null .... by either using a formual or rule criteria ...... as i cannot use ISCHANGED or ISNEW on a lookup field. ?
    – sam
    Commented Oct 2, 2015 at 9:16
  • 1
    Thank you for all your help Jenny, smckitrick, developer Wonk. I have eventually used the trigger/Class at account level to push the information after update and at opportunity level after insert, after update. It works now but i will also look into building Process flow to achieve this functionality and deactivate the trigger (Less maintenance) .
    – sam
    Commented Oct 2, 2015 at 12:03
0

Add a formula field on Opportunity that displays the Sales_Manager__c custom field on the account object.

2
  • 1
    I don't think you can email based off of a formula field.
    – Jenny B
    Commented Oct 1, 2015 at 18:37
  • Formual data type email does not exist and we can only use email field or lookup to user field to send out emails. I have used a workflow to update the user field by saying account.sales_manager__c.email. but the issue was: what if the field value at account level is changes (we cannot use Ischanged or Isnew on lookup data type !!
    – sam
    Commented Oct 2, 2015 at 9:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .