function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BB 

Why my controller is not returning anything?

I try to get a specified field from the account that im in, but for some reason i can't . Here is my code
Aura comp:
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" controller="AccountController">
    <aura:attribute name="account" type="Account[]"/>
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
</aura:component>

Js controller 
({
    init : function(component, event, helper) {
        
        var action = component.get("c.getAcc");
        action.setParams({
            "accid": component.get("v.recordId")                    
        }); 
        
         action.setCallback(this, function(a) {
            component.set("v.account", a.getReturnValue());
        });
        $A.enqueueAction(action);

        var test = component.get("v.account");
        alert(test.Bridger_check__c);
       
    }
})

Apex controller 
 @AuraEnabled
    public static List<Account> getAcc(String accid) {
           return [SELECT id,Bridger_check__c
            FROM Account
            WHERE id =: accid 
            LIMIT:1]; 
            
        }

 
Best Answer chosen by B
Ravi Dutt SharmaRavi Dutt Sharma
Hi, 

Can you try below code and let us know the output. 

Component:
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" controller="AccountController">
    <aura:attribute name="account" type="Account"/>
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
    	Account: {!v.Account.Bridger_check__c}
</aura:component>

Controller:
({
    init : function(component, event, helper) {
        
        var action = component.get("c.getAcc");
        action.setParams({
            "accid": component.get("v.recordId")                    
        }); 
        
        action.setCallback(this, function(response) {
        	var state = response.getState(); // get the response state
        	if (state === "SUCCESS"){
        		component.set("v.account", response.getReturnValue());
        	}else if(state === "ERROR"){
        		var errors = response.getError();
        		et toastParams = {
			        title: "Error",
			        message: "Unknown error", // Default error message
			        type: "error"
			    };
			    // Pass the error message if any
			    if (errors && Array.isArray(errors) && errors.length > 0) {
			        toastParams.message = errors[0].message;
			    }
			    // Fire error toast
			    let toastEvent = $A.get("e.force:showToast");
			    toastEvent.setParams(toastParams);
			    toastEvent.fire();
        	}
            
        });
        $A.enqueueAction(action);
    }
})

Apex Class:
@AuraEnabled
    public static Account getAcc(String accid) {
           return [SELECT id,Bridger_check__c
            FROM Account
            WHERE id =: accid 
            LIMIT 1]; 
            
    }

​​​​​​​

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
Hi, 

Can you try below code and let us know the output. 

Component:
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" controller="AccountController">
    <aura:attribute name="account" type="Account"/>
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
    	Account: {!v.Account.Bridger_check__c}
</aura:component>

Controller:
({
    init : function(component, event, helper) {
        
        var action = component.get("c.getAcc");
        action.setParams({
            "accid": component.get("v.recordId")                    
        }); 
        
        action.setCallback(this, function(response) {
        	var state = response.getState(); // get the response state
        	if (state === "SUCCESS"){
        		component.set("v.account", response.getReturnValue());
        	}else if(state === "ERROR"){
        		var errors = response.getError();
        		et toastParams = {
			        title: "Error",
			        message: "Unknown error", // Default error message
			        type: "error"
			    };
			    // Pass the error message if any
			    if (errors && Array.isArray(errors) && errors.length > 0) {
			        toastParams.message = errors[0].message;
			    }
			    // Fire error toast
			    let toastEvent = $A.get("e.force:showToast");
			    toastEvent.setParams(toastParams);
			    toastEvent.fire();
        	}
            
        });
        $A.enqueueAction(action);
    }
})

Apex Class:
@AuraEnabled
    public static Account getAcc(String accid) {
           return [SELECT id,Bridger_check__c
            FROM Account
            WHERE id =: accid 
            LIMIT 1]; 
            
    }

​​​​​​​
This was selected as the best answer
BB
First this is gives me error :
cos it not recognized toastParams 
}else if(state === "ERROR"){
              var errors = response.getError();
               et toastParams = {
                    title: "Error",
                    message: "Unknown error", // Default error message
                    type: "error"
                };
                // Pass the error message if any
                if (errors && Array.isArray(errors) && errors.length > 0) {
                    toastParams.message = errors[0].message;
                }
                // Fire error toast
                let toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams(toastParams);
                toastEvent.fire();
            }

if i remove it and run it in order to test it   

Error while creating component for lightning component quick action [Unable to find action 'getAcc' on the controller of c:LCC]
 
Ravi Dutt SharmaRavi Dutt Sharma
For the first error, please change et toastParams to let toastParams

For the second error, please check if AccountController has the method getAcc or not. The method should be annotated with @AuraEnabled
BB
Sorry the  error 
Error while creating component for lightning component quick action [Unable to find action 'getAcc' on the controller of c:LCC] 
was beacuse of not having the auraEnabled 
BB
Thank you :)