You need to sign in to do that
Don't have an account?
SBhatia
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;
}
}
}
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.
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.
Ok am modifying the class lil bit
Now just pass trigger.new from the trigger.
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.
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.
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. :(
The code sample works for me
yes the account field is filled in when i insert the opportunity. Still didnt work.
Can you post the code for your trigger and class whatever you have in place.
And please check whether the trigger is active.