+ Start a Discussion
Rajendra Prasad 44Rajendra Prasad 44 

Sobject doubt

I am confused by fallowing Sobject statement 
'Account a = (Account)s' .  Could you explain it? 
Ajay K DubediAjay K Dubedi
Hi Rajendra,
'Account a = (Account)s' in this line 's' is a variable which might hold the Account information and you are typecasting it to Account type.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Ravi Dutt SharmaRavi Dutt Sharma
Think of 's' as on object which is returned by another method. Now 's' can be anything. It can be an Account, a contact or any other sObject. As a caller, I know that the retunred 's' will always be an Account. But the method who is returning it, that method might be returning it as a generic sObject. To store this generic sObject in a variable of type Account, you need to first type cast it into Account. 

As a caller, if you know that 's' will be an Account, you will typecast it as below:
Account acc = (Account)s;

As a caller, if you know that 's' will be a Contact, you will typecast it as below:
Contact con = (Contact) con;