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
Sam KamenskySam Kamensky 

If I pass an sObject as a parameter to a method, which fields will be available?

I think my two options are pass the id to a class and query the fields I need there, or, pass the entire sObject and do no querying.

Assuming that the sObject will always come from a trigger (as I understand it no querying is needed when using trigger.new as it automatically contains the entire sObject), do I ever need to query again? Or can I simply pass the sObject I get from the trigger to any and every class I want? 
Best Answer chosen by Sam Kamensky
Ravi Dutt SharmaRavi Dutt Sharma
As it is coming from a trigger, you can pass the sObject as it is and all the fields will be available. You need not query the fields again. If you want to access data through relationship fields, then you need to issue a separate query. For example, you have trigger on Contact object and you want to access the account name through account lookup present on Contact, then you need to do a query on Account to get the fields on Account because relationship fields are not present in a trigger,

All Answers

DebasisDebasis
Hi Sam,

when you wil pass entire sobject(assuming it is comming form trigger), no need query, all the field for that object will be accssible in the method.


if i have a trigger on account object and passed the list of records to method then inside method without querying i can access all the fields of the object.
 
Trigger AccTrigger on Account(before insert){
List<account> acclist = trigger.new;
​  MyClass obj = new MyClass();
obj.myMethod(acclist);
}


//In my class code
 public clas MyClass{
 public void myMethod(list<Account> acclist){
  //here you can access all the field of the account list.
}
}




Thanks,
Debasis
Sam KamenskySam Kamensky
Thank you!
DebasisDebasis
Hi sam,
If answer helps you please select as best answer so that it will be easily refer in future by anyone.
Ravi Dutt SharmaRavi Dutt Sharma
As it is coming from a trigger, you can pass the sObject as it is and all the fields will be available. You need not query the fields again. If you want to access data through relationship fields, then you need to issue a separate query. For example, you have trigger on Contact object and you want to access the account name through account lookup present on Contact, then you need to do a query on Account to get the fields on Account because relationship fields are not present in a trigger,
This was selected as the best answer