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
vr8cevr8ce 

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.
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: Name
But 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"}|0x692d38c6
Why can't I access the Name field?
Team NubesEliteTeam NubesElite
Hi vr8ce,
Please try this code i tried this code achieved successfully.
List<contact> conts = [SELECT Id,Name FROM Contact LIMIT 10];
for (contact cont : conts) {
    System.debug('Cont is ' + cont.Name);
}


Thank You
www.nubeselite.com

Developement | Training | Consulting

Please Mark this assolution if your problem resolved.
 
vr8cevr8ce
Sorry, I should have said: I used SObject intentionally. In the real-world problem that this is a simplification of, I don't know the object type.
Ravindra Kashyap 7Ravindra Kashyap 7
You can use <YourObject>.getSObjectType().getDescribe().getName() to get object name and based on that you can perform operation.

https://success.salesforce.com/ideaView?id=08730000000l9wHAAQ

Please check below code:
List<SObject> conts = [SELECT Id,Name FROM Contact LIMIT 10];
for (Sobject cont : conts) {
    if(cont.getSObjectType().getDescribe().getName() == 'Contact'){
        Contact con = (Contact)cont;
        System.debug('Cont is ' + con.Name);   
    } else if(cont.getSObjectType().getDescribe().getName() == 'Account'){
        Account acc = (Account)cont;
        System.debug('Cont is ' + acc.Name);   
    }    
}
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



 
Deepali KulshresthaDeepali Kulshrestha
Hi vr8ce,

Greetings to you!

If you want to perform operations on an sObject, it is recommended that you first convert it into a specific object
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,
Deepali Kulshrestha
www.kdeepali.com
Team NubesEliteTeam NubesElite
Hi,
Please try this code it will work.
List<sObject> conts = [SELECT Id,Name FROM Contact LIMIT 10];
for (sObject cont : conts) {
    System.debug('Cont is ' + cont.get('Name'));
}

Thank You
www.nubeselite.com

Developement | Training | Consulting

Please Mark this as solution if your problem resolved.
Ajay K DubediAjay K Dubedi
Hi, 

   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
vr8cevr8ce
Thanks, everyone. As I said in an earlier reply (it doesn't appear we can edit questions after the fact), in the real world problem, I don't know the object type. Therefore any form of casting to Contact is not the solution. (If I could cast to Contact, I would have used Contact in the first place instead of SObject.)

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.
System.debug('Cont is ' + cont.get('Name'))
In the real world problem, though, I need a string, so knowing that the value is a string, I do use a cast here.
System.debug('Cont is ' + (String)cont.get('Name'))