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
Jennifer Dos Reis ICOJennifer Dos Reis ICO 

"A generic sObject variable can be assigned to any specific sObject"?

I was doing the Trailhead when in the module of "Using sObjects" one of the right answers is "A generic sObject variable can be assigned to any specific sObject, standard or custom. Such as Account or Book__c." But I think this answer is wrong or I`m not understanding very well the question.

With the frase "A generic sObject variable can be assigned to any specific sObject" I understand something like this:

sObject a = new Account();  // This is a generic sObject variable

Contact b = new Contact(); // This is a specific sObject

// A generic sObject variable can be assigned to any specific sObject 

b = (Account) a;  // Error


I understand that the last statement is an error because b is a variable of Contact type no of Account type. For this reason i'm not agree with the frase "A generic sObject variable can be assigned to any specific sObject". 

Am I missing something ? 

Thanks for your help!

Prateek Singh SengarPrateek Singh Sengar
Hi Jennifer,
I believe by the statement "A generic sObject variable can be assigned to any specific sObject" they mean something like
SObject a;
Account acc = (Account)a;

many times the output that we recieve are generic sobjects. However we then type cast them into specific sobject custom or standard. A good example would be parsing of a JSON response into a instane of custom of standard reponse.
 
Magesh Mani YadavMagesh Mani Yadav

Hi Jennifer,

Did you read they also said on the same trailhead module under title "Working with the Generic sObject Data Type".

https://trailhead.salesforce.com/en/apex_database/apex_database_sobjects
 

"In contrast, variables that are declared with the specific sObject data type can reference only the Salesforce records of the same type."
 

sObject sobj1 = new Account(Name='Trailhead');
sObject sobj2 = new Book__c(Name='Workbook 1');
So here the sObject variable sobj1 is declared as Account sobject type. So this variable can only be assigend to Account object type of another variable and not to the Book__c object.
And i your case also you have declared variable "a" as Account object so its not possible to assign it to Contact object Type.
I hope you got the clear answer.


Regards,
Magesh

 

Jennifer Dos Reis ICOJennifer Dos Reis ICO

Thanks for your responses,

I think I understand the theory but I'm not aggre or I don't understand the wording. Because they said in the trailhead:  

"This example shows how the generic sObject variable can be assigned to any Salesforce object: an account and a custom object called Book__c."

sObject sobj1 = new Account(Name='Trailhead');
sObject sobj2 = new Book__c(Name='Workbook 1');


If variable A can be assigned to variable B the direction of the assigment is:

B = A

And not A = B like the trailhead clames. 

am I right?

My concern about this is that one of the trailhead questions used that wording. And as I'm studying for certification I'm worried that that the wording makes me fail.

Magesh Mani YadavMagesh Mani Yadav
Hi Jennifer,
I agree with you about the sentence (generic sObject variable can be assigned to any Salesforce object) may be it should be like "generic sObject variable can represent any salesforce object"