You need to sign in to do that
Don't have an account?
vr8ce
Why can't I access the Name in the object returned from an SOQL query?
I'm missing really obvious, but I don't know what. I've simplified what I'm trying to do for example purposes.
I'm executing the below in an anonymous window. I'm getting 10 contacts, then writing the ID's of each to the debug log.
I'm executing the below in an anonymous window. I'm getting 10 contacts, then writing the ID's of each to the debug log.
List<SObject> conts = [SELECT Id,Name FROM Contact LIMIT 10]; for (Sobject cont : conts) { System.debug('Cont is ' + cont.Id; }What I want to do is write the name of each of the contacts. But changing cont.Id to cont.Name on the System.debug line doesn't work, it says it's not a valid variable.
List<SObject> conts = [SELECT Id,Name FROM Contact LIMIT 10]; for (Sobject cont : conts) { System.debug('Cont is ' + cont.Name; } Line: 7, Column: 36 Variable does not exist: NameBut the debug shows this variable assignment. It has both the Id and the Name. (As well as the RecordTypeId, which is probably a question for another time.)
19:02:19:009 VARIABLE_ASSIGNMENT [6]|cont|{"Id":"0034N00001pc6T2QAI","Name":"John Doe","RecordTypeId":"01261000000X007AAC"}|0x692d38c6Why can't I access the Name field?
Please try this code i tried this code achieved successfully.
Thank You
www.nubeselite.com
Developement | Training | Consulting
Please Mark this assolution if your problem resolved.
https://success.salesforce.com/ideaView?id=08730000000l9wHAAQ
Please check below code: By using above code you can identify Object name and based on that downcast. I know checking for each object is difficult but I believe we can hardcode this check conditions because we'll be always knowing what object type our query will return.
I hope it will help you 😀.
Regards,
Ravindra Nath Kashyap
Greetings to you!
If you want to perform operations on an sObject, it is recommended that you first convert it into a specific object
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Please try this code it will work.
Thank You
www.nubeselite.com
Developement | Training | Consulting
Please Mark this as solution if your problem resolved.
Your Code Says that "Variable does not exist: Name"
Because You Use sObject To Iterate Your List Since Not Every sObject has 'Name' Field, it says "Variable does not exist".
But when you Use 'cont.id' it Contains No Error Because Every Sobject has an Id.
You can Use Typecasting Like used in Below code:
List<SObject> conts = [SELECT Id,Name FROM Contact LIMIT 10];
for (Sobject cont : conts)
{
System.debug('Cont is ' + ((Contact)cont).Name);
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Nor can I have a giant if/else and/or switch statement. The SObject could be any object type.
The best solution I've found, which was given to me elsewhere, is to use the get method to retrieve the field value. In the real world problem, though, I need a string, so knowing that the value is a string, I do use a cast here.