+ Start a Discussion
Giancarlo AmatiGiancarlo 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.
 
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




 
Best Answer chosen by Giancarlo Amati
Om PrakashOm Prakash
Yes, it will return the Account record and you can get the field value like bellow.
accSalesDivision.Sales_Division__c
Additionally here is your optimized code
trigger CaseTrigger on Case (after insert) {
    Case objCase = Trigger.new[0]; // Only for first Case. Not for bulk currently.
    if(objCase.AccountId != null){
        Account accSalesDivistion = [SELECT Id, Sales_Division__c FROM ACCOUNT WHERE Id =: objCase.AccountId];
        System.debug('Account Id =  ' + accSalesDivistion.id );
		System.debug('Sales Division=  ' + accSalesDivistion.Sales_Division__c );
    }
}
Let me know if any other query
 

All Answers

Om PrakashOm Prakash
Hi,
You need to use : after = to use a variable 
Account accSalesDivistion = [SELECT Id, Sales_Division__c FROM ACCOUNT WHERE Id =: c.AccountId];
Giancarlo AmatiGiancarlo Amati
Ah on that note the Salesforce world now knows how basic I am at software-developing in SF :)

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
accSalesDivision.Sales_Division__c

in my code?

Thank you. GC
mukesh guptamukesh gupta
Hi Giancarlo,

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

 
Om PrakashOm Prakash
Yes, it will return the Account record and you can get the field value like bellow.
accSalesDivision.Sales_Division__c
Additionally here is your optimized code
trigger CaseTrigger on Case (after insert) {
    Case objCase = Trigger.new[0]; // Only for first Case. Not for bulk currently.
    if(objCase.AccountId != null){
        Account accSalesDivistion = [SELECT Id, Sales_Division__c FROM ACCOUNT WHERE Id =: objCase.AccountId];
        System.debug('Account Id =  ' + accSalesDivistion.id );
		System.debug('Sales Division=  ' + accSalesDivistion.Sales_Division__c );
    }
}
Let me know if any other query
 
This was selected as the best answer
Giancarlo AmatiGiancarlo Amati
Fantastic thank you all
GC
Giancarlo AmatiGiancarlo Amati
Hi Om, 

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
Om PrakashOm Prakash
Hi,
Best practice is to use Trigger.New in for loop so that trigger will work for all the bulk records.
for(Case objCase : Trigger.new){
     
 }
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