+ Start a Discussion
StenderStender 

Variable sObject type as method parameter

I have a method that I want to run in a few circumstances that will require a record be passed in and return a date.  The issue is that the sObject being passed will be different, depending on the situation. 


I want to house all of the logic in the same method as they are related AND share most of the logic itself, but I am unsure if I can cast a parameter of 'ambiguous' sObject type.

 

would the below line work?

 

public static Date gimmeTheDate(sObject s){
//logic to determine sObject type passed in and adjust logic used from there
}

 

 

Please let me know!

 

Thanks,
Jeremy Stender

Best Answer chosen by Admin (Salesforce Developers) 
StenderStender

Actually, I did some testing and resolved it myself.  Had some issues with casting the sObject passed in until i found the syntax for doing that.  If the object passed in is an Account you will need to use the below line to 'cast' it as an account

public static Date gimmeTheDate(sObject s){
//logic to determine sObject type (assume its an account for below)
Account a = (Account)s;
}