0

I am new to salesforce lightning and trying to learn it. I want to show an account information when the app is initialized. I have checked in the console, getting the value but its not displaying on the UI. Below is my code.

Component:-

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="acc" type="Account"/>
<div class="slds-grid">
    <div class="slds-col">{!v.acc.name}</div>
    <div class="slds-col">{!v.acc.Id}</div>
    <div class="slds-col">{!v.acc.Industry}</div>
</div>

Client Controller:-

({
  doInit : function(component, event, helper) {
         var action = component.get("c.retrieveAccount");
        console.log('in client side controller');
         action.setCallback(this,function(response){
         var state = response.getState();
         if(component.isValid() && state== "SUCCESS"){
             console.log('got response');

            component.set("v.acc",response.getReturnValue());
             console.log('response is:- '+response.getReturnValue());
             console.log('value of account '+JSON.stringify(component.get("v.acc")));
             console.log('--------'+JSON.stringify(response.getReturnValue()));
         }

        });
        $A.enqueueAction(action);

   },
 })

Apex controller:- GridAccountController

public class GridAccountController {

@AuraEnabled
public static Account retrieveAccount(){
    System.debug('retrieveAccount');
    return [SELECT Id,Name,Email__c,Industry,childCount__c,SLA__c FROM ACCOUNT LIMIT: 1];
}

}

1 Answer 1

3

Your query in the Apex class in not valid and is throwing an error:

[SELECT Id,Name,Email__c,Industry,childCount__c,SLA__c FROM ACCOUNT LIMIT: 1];

should be (No colon)

[SELECT Id,Name,Email__c,Industry,childCount__c,SLA__c FROM ACCOUNT LIMIT 1];

And you have an extra comma at the end of your init function which will invalidate the code

It may be that you did not post the exact code from your markup as you said you got value back which is not possible with the code you posted. You could use the below as a starting point and add in the rest of your stuff little by little. When it breaks you know what caused it.

I threw the following into my org and it works just fine:

Component

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

<div class="slds-grid">
    <div class="slds-col">{!v.acc.name}</div>
    <div class="slds-col">{!v.acc.Id}</div>
    <div class="slds-col">{!v.acc.Industry}</div>
</div>

JS Controller

({
    doInit: function (component, event, helper) {
        var action = component.get("c.retrieveAccount");
        console.log('in client side controller');
        action.setCallback(this,function(response){
            var state = response.getState();
            if(component.isValid() && state== "SUCCESS"){
                console.log('got response');

                component.set("v.acc",response.getReturnValue());
                console.log('response is:- '+response.getReturnValue());
                console.log('value of account '+JSON.stringify(component.get("v.acc")));
                console.log('--------'+JSON.stringify(response.getReturnValue()));
            }

        });
        $A.enqueueAction(action);
    }
})

Apex Controller

public class myProblemController {

    @AuraEnabled
    public static Account retrieveAccount(){
        System.debug('retrieveAccount');
        return [SELECT Id,Name,Industry FROM ACCOUNT LIMIT 1];
    }

}
2
  • Thanks Eric. I made the changes and its working now. Note:- One strange thing which I noticed is that its case sensitive. If we use {! v.acc.name } , {! v.acc.id}, {! v.acc.industry} then nothing gets displayed. Commented Mar 20, 2017 at 9:27
  • @ArpitSethi lightning is sensative to case yes. However I thought the same thing so I initially did not change the case thinking it would not work but to my surprise it did, exactly as I posted above.
    – Eric
    Commented Mar 20, 2017 at 12:44

You must log in to answer this question.

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