You need to sign in to do that
Don't have an account?
Giancarlo Amati
SQL apex retrieve account from case
Dear all,
I am building an Apex Case trigger where each case is linked to an Account field and I can access the c.AccoundId. Having the Id of the account I need to access the "Sales division" field.
But I'm not sure what's the problem here.
Can you help? It looks that using c.AccountId it's not possible in the SQL, isn't it? The error message I get is "Unexpected token 'accSalesDivistion'."
Thank you for your help.
GC
I am building an Apex Case trigger where each case is linked to an Account field and I can access the c.AccoundId. Having the Id of the account I need to access the "Sales division" field.
Case c = Trigger.new[0]; Account accSalesDivistion = [SELECT Id, Sales_Division__c FROM ACCOUNT WHERE Id = c.AccountId];I'm calling the above query but it's returning me a lot of syntax errors.
But I'm not sure what's the problem here.
Can you help? It looks that using c.AccountId it's not possible in the SQL, isn't it? The error message I get is "Unexpected token 'accSalesDivistion'."
Thank you for your help.
GC
All Answers
You need to use : after = to use a variable
I have a question tho: what does the SQL query return? is it an Account object? or it simply automatically cast it to Account?
Can I then use
in my code?
Thank you. GC
Please use tested code:-
Account accSalesDivistion = [SELECT Id, Sales_Division__c FROM ACCOUNT WHERE Id =: Trigger.new[0].AccountId];
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
GC
sorry one last question: is it best practices for the Triggers to consider a more general approach and loop through all the case, instead of Trigger.new[0] only?
I am guessing the Case Trigger can be called multiple times and the case instances put in the Trigger.new Q?
Thank you. GC
Best practice is to use Trigger.New in for loop so that trigger will work for all the bulk records. Please refer this doc for more details:
https://developer.salesforce.com/page/Apex_Code_Best_Practices
Trailhead for Trigger
https://trailhead.salesforce.com/en/modules/apex_triggers/units/apex_triggers_intro