1

How to use toLabel(RecordType.Name) and RecordType.Name in the same SOQL query? I am trying to retrieve both the values and use them. If I can get the query to work then how can I refer the values from the result?

List<Case> csList = [Select Id, RecordType.Name, toLabel(RecordType.Name) RName
                         From Case limit 10];

When I execute the above code in the developer console I get the below error

System.QueryException: No such column 'RName' on entity 'RecordType'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

I tried another approach and the query worked but I am unable to use the toLabel(Status) value. How can I use it?

String soql = ' SELECT Id, RecordType.Name, toLabel(RecordType.Name) StatusLabel, Description '
          + ' FROM Case ORDER BY CreatedDate DESC LIMIT 100';

// Query the cases object
List<Case> cases = Database.query(soql);
for (Case cs : cases) {
  system.debug(cs.get('StatusLabel'));  
}

When I try to display the translated record type name I get the following error

System.SObjectException: Invalid field StatusLabel for Case
2

You can't use both the API value and translated value on the same field in the same query. It's not possible to alias the fields either, and aliases only have an effect in aggregate queries. You would need two different queries. In both cases, they would be referenced by the field name (e.g. cs.RecordType.Name).

  • Thanks for the reply. I figured out a way to get both the values in a single query. I am querying toLabel(RecordType.Name) & RecordType.DeveloperName - first field give me the translated value and the second field is same as the RecordType.Name – Sriram Lns Jun 20 at 20:29
  • 1
    @SriramLns Fair enough, but keep in mind that DeveloperName may not be the same as Name, so there could be a discrepancy. For example, DeveloperName cannot hold spaces, cannot start with a number, etc. Ideally, you should use two queries. – sfdcfox Jun 21 at 3:23
  • I definitely get what you are saying, the ideal way is to use two separate queries to get the values. Thanks for the help. – Sriram Lns Jun 21 at 13:25
0
List<Case> csList = [Select Id, RecordType.DeveloperName, toLabel(RecordType.Name)
                     From Case limit 10];

Above query will give me the original value (since DeveloperName and Name hold the same value) and also the translated value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.