11

EDIT: This problem seems to have been fixed on Salesforce's end. See Shamina's answer below.

The SOQL toLabel function doesn't work in my @AuraEnabled method when called from a Lightning component. UserInfo.getLanguage returns "fi" (for Finnish) but the Record Type Name field that's supposed to be translated is still in English. Calling this method manually in Developer Console's Execute Anonymous window works as expected, i.e. the field is translated to Finnish.

global without sharing class SomeController {
    @AuraEnabled
    global static List<AnApexClass> getData() {
        System.debug(UserInfo.getLanguage()); // -> "fi"

        List<CustomObject__c> recs = [
            SELECT Id, toLabel(RelatedObject__r.RecordType.Name)
            FROM CustomObject__c
            WHERE Field__c = :UserInfo.getUserId()
            AND RelatedObject__c != null
            ORDER BY RelatedObject__r.RecordTypeId, AnotherField__c];

        System.debug(recs[0].RelatedObject__r.RecordType.Name);
        // -> English when called from Lightning, Finnish otherwise

        // ...
    }
}
1

This post is a bit old and looks like the issue is fixed since then because toLabel function correctly translates in user language in lightning component.

For reference, this quick sample code below works on a custom object (Favorite__c) related to another one (Property__r) which has recordtypes

Apex controller method

public with sharing class ApexController{
      @AuraEnabled
      public static String getRecordTypeLabel() {    
          System.debug(UserInfo.getLanguage());  
          String result = 'No data found';
           List<Favorite__c> fav = [ SELECT Id, toLabel(Property__r.RecordType.Name)
                                  FROM Favorite__c 
                                  where Property__r.RecordTypeId != null 
                                  limit 1];

           if(fav.size()>0){
              system.debug(fav[0].Property__r.RecordType.Name);    
              result=fav[0].Property__r.RecordType.Name;
           }

           return result;
      }
}

LightningComponentController.js

({
    doInit: function (component, event, helper) {
          helper.getRecLabel(component);
    }
})

LightningComponentHelper.js

({
    getRecLabel: function (component) {
        var action = component.get('c.getRecordTypeLabel');
        action.setCallback(this, function (actionResult) {
            component.set('v.recType', actionResult.getReturnValue());
        });
        $A.enqueueAction(action);
    }
})

LightningComponent.cmp

<aura:component controller="ApexController">

  <aura:attribute name="recType" type="String"/>
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <div>
        <b>Record Type label: {!v.recType}</b> <!-- translates to recordtype label based on user language -->
    </div>
</aura:component>

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.