1

I am trying to obtain all related child (look up) records of a record whose record id is passed. Salesforce docs say that for every object, regardless of type, name is a standard field, just like Id.

However,

select name, id from case

throws the error

No such column name on entity case

Similar error is thrown for several objects other than Case when name (which is apparently a standard field for every object) is tried to be fetched.

So how does one fetch name field for a record of known record id and object type (which is easily obtained from record id)?

1
  • 1
    Case does not have a standard field called Name, it's CaseNumber. The same goes for Task or some other objects. I would say Use a dynamic SOQL in this case or put conditions based on the object you find. Commented May 27, 2022 at 7:41

1 Answer 1

2

Salesforce says, sometimes it is not accurate, what it says :)

There is a possibility to get "main" name field for every object: custom and standard. Use isNameField method.

String objectApiName = 'Case';
for(SObjectField field :((SObject)Type.forName(null, objectApiName).newInstance()).getSObjectType().getDescribe().fields.getMap().values()){
    DescribeFieldResult describe  = field.getDescribe();
    if(describe.isNameField()){
        System.debug('Name field: ' + describe.getName());
    }
}

For Case object it is to debug as:

DEBUG|Name field: CaseNumber

For WorkOrder:

DEBUG|Name field: WorkOrderNumber

For Task

DEBUG|Name field: Subject

For normal Account

DEBUG|Name field: Name

For Lead

DEBUG|Name field: Name


The same information is available via REST API Get Object Metadata, or from LWC getObjectInfo. Check nameFields in a response.

1
  • 1
    This is exactly what I wanted. Thanks for the detailed answer! Commented May 28, 2022 at 19:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .