0

I have a custom setting with API Name: Company_Setting__c. I need to access the value stored in this custom setting in lightning component. I got a field with API Name: Company_Phone__c.

Below is my code:

Apex Controller:

@AuraEnabled
public static Company_Setting__c getCompanySetting(){
    return Company_Setting__c.getInstance();
}

Ligntning Component:

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

Lightning Component Controller:

doInit: function(component, event, helper) {
    var getSettingsAction = component.get("c.getCompanySetting");

    getSettingsAction.setCallback(this, function(response) {
        if (component.isValid() && response !== null && response.getState() == 'SUCCESS') {
            component.set("v.companySetting", response.getReturnValue());
            console.log("Company Setting loaded.");
        } else {
            console.log("Failed to load Company Setting.");
        }
    });
    $A.enqueueAction(getSettingsAction);
}

However, when use a button to alert with value alert(component.get("v.companySetting.Company_Phone__c")); it just show as undefined.

1

What you implemented is correct.

The problem could be 1. You are not getting Company_Setting__c values OR 2. API name might be wrong - case sensitive. Put logs in both places - apex and lightning - and check.

Apex:

@AuraEnabled
public static Company_Setting__c getCompanySetting(){
    System.debug('Company_Setting__c.getInstance() => '+Company_Setting__c.getInstance());
    return Company_Setting__c.getInstance();
}

JS:

if (component.isValid() && response !== null && response.getState() == 'SUCCESS') {
            component.set("v.companySetting", response.getReturnValue());
            console.log("Company Setting loaded."+JSON.stringify(component.get("v.companySetting")));
        }

Now test both apex logs and browser logs to verify and correct it

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.