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
NMNM 

How can I use a lookup-field value in Trigger code?

Hello,
I'm trying to update an Account record by using an Opportunity "after update" trigger.
I want to update the Account record, which has the same id as the id of the Opportunity Account Name.
My code is as follows:
 
trigger changeAccountToActive on Opportunity (after update) {
Opportunity[] opp = trigger.new;
Account[] acct;

acct = [select id, Active__c from Account where id = :opp[0].Account.id];
//acct = [select id, Active__c from Account where Site = :opp[0].NextStep];
if (acct.size()>0)
{
  if (opp[0].status__c == 'Full Payment')
  {
  acct[0].Active__c = 'Yes';
  }
  update acct[0];
}
}
 
I used some debug printing code and I realised that the code doesn't get the lookup field value, but it does get the values of other fields. If I use the code in comments, where the "where" statement works on another opportunity field, it works...
 
Does anyone have an idea???
Thanks.


Message Edited by NM on 01-19-2008 10:57 PM

Message Edited by NM on 01-19-2008 11:00 PM
PBS_IC786PBS_IC786
try this...

acct = [select id, Active__c from Account where id = opp[0].AccountId];
NMNM
It works! Thank you sooooo much!!
Can you please tell why does the system use AccountId and not Account.Id? (I was wondering...)
Thanks again. You helped me a lot :)


Message Edited by NM on 01-22-2008 03:28 AM
Vijay RautVijay Raut
Hey,

when you look the API Name of field account in Opportunity fields list, you will get the API Name as AccountId. System uses the API name to access values of fields.