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
SBhatiaSBhatia 

Apex code to update a picklist field in Opportunity from a picklist field on Account

Hi All,

I need Apex code to update a picklist field in Opportunity by copying the value of a picklist field on Account object for that opportunity record.

 

I have a 'Geo' field in Opty that should be set to the same as the 'Geo' field on the account. We cant use the forumula field for this because this field has to be editable.

 

So here's the code i am using but it is not working-

 

Can anyone help with this please. thanks.

 

public class CopyGeoClass
{
public static void CopyGeotMethod(Opportunity[] Optys)
     {
      for (Opportunity O:Optys)
        {
          O.geo__c=O.Account.geo__c;
        }
     }
}
Avidev9Avidev9

The catch here is you have to query explicitly for this field "Opportunity.Account.geo__c" if it is a trigger. Because triggerNew/triggerOld only holds the value from current object not from the relationships/lookups.

You can query for this field by doing [SELECT Id,Account.geo__c FROM Opportunity Where Id IN:triggerNew]; and pass the value to the new modified method. Make sure you are calling from an after trigger.

 

Now when you are calling From After trigger you have to explicity call an update on Opportunity object, which can cause cause recursion. So to avoid that just make some small modification.

 

public class CopyGeoClass
{
public static void CopyGeotMethod(Opportunity[] Optys)
     {
List<Opportunity> opp2Update = new List<Opportunity>();
      for (Opportunity O:Optys)
        {
if(O.geo__c != O.Account.geo__c){
           O.geo__c=O.Account.geo__c;
opp2Update.add(O)
}

        }
update opp2Update;
     }
}
SBhatiaSBhatia

Thanks Avi. Appreciate it.

I am a new bie at programming though so can you guide me as to where/how the query will get plugged in in the trigger code. Thanks.

Avidev9Avidev9

Ok am modifying the class lil bit 

 

public class CopyGeoClass
{
public static void CopyGeotMethod(Opportunity[] triggerNewRecords)
     {
List<Opportunity> opp2Update = new List<Opportunity>(); for (Opportunity O:[SELECT Id,Account.Geo__c FROM Opportunity WHERE Id IN:triggerNewRecords]) { if(O.geo__c != O.Account.geo__c){ O.geo__c=O.Account.geo__c; opp2Update.add(O) } } update opp2Update; } }

 Now just pass trigger.new from the trigger.

sushant sussushant sus
Avidev9 code is good buy u have to consider you cant edit it make chenges it will always give value of account of geo__c ,not the value u edited .
SBhatiaSBhatia

This is not working for me though. I am sure it is a user error but also i wanted to just basically understand the concept of how to create a variable for a field in a related object and i am not sure i still get it. I am still playing w it. Hopefully can make it work.

 

Avidev9Avidev9
Make sure you are calling this from a after trigger
SBhatiaSBhatia

Yes i am but still not working. Code compiles correctly. No errors when inserting a new opty but it doesnt work. It doesnt copy over the data. My field stays blank.

Avidev9Avidev9
You need to select a account while inserting an opportunity
SBhatiaSBhatia

Didnt work for me. I get no errors on compliling the class and trigger but the intended result does not happen. It doesnt copy the value of from the account field into Opty field. :(

Avidev9Avidev9
Can you post all of your code and explain how you aer working on it ?

The code sample works for me
SBhatiaSBhatia

yes the account field is filled in when i insert the opportunity. Still didnt work.

Avidev9Avidev9

Can you post the code for your trigger and class whatever you have in place.

 

And please check whether the trigger is active.