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
AchyuthAchyuth 

How to assign the field value to a variable in salesforce lightning controller and assign to a variable?

Hi All,
Can anyone help me on the below requirement please!!
I have an apex controller(with an argument passed), lightning component, lightning controller where as I can able to fetch the records by passing the value from the ui using lightning component...and is working fine 
I need is : i want to assign the field value to a variable in salesforce lightning controller  and assign to a variable ABC.

Please find the code:-
@auraEnabled
     public static List<Account> getAccount(Id searchKey ){
         List <Account> acclist = new  List <Account>();
         if(searchKey!= Null){
           acclist = [SELECT Id, Name, Description from Account WHERE ID =: searchKey]; 
        }         
        
        return acclist; 
======================================================
getAccountList : function(component, event, helper) {
        var action = component.get("c.getAccount");
        var search = component.get("v.searchKey");
        action.setParams({
            "searchKey": search            
        });
                
        
        action.setCallback(this, function(response){
            var state = response.getState();            
            if(state == 'SUCCESS') {
                var acclist = response.getReturnValue();
                component.set("v.Accountlist",acclist); 
                var fieldValue = component.get("v.Accountlist");                 
       console.log(JSON.stringify(fieldValue ));  // i am getting all the values based on the query.
               console.log('Value ::::::: '+fieldValue );

======================================================

the entire query result is in fieldValue now... I want to assign only the name to another value...

Can anyone Help me on this !!

Thanks in Adv.!!
Chinna.
Raj VakatiRaj Vakati
Define the attribute and assign it .. call the attribute from the controller 
 
action.setCallback(this, function(response){
            var state = response.getState();            
            if(state == 'SUCCESS') {
                var acclist = response.getReturnValue();
                component.set("v.Accountlist",acclist); 
				                component.set("v.ABC",acclist.size()); 

                var fieldValue = component.get("v.Accountlist");                 
       console.log(JSON.stringify(fieldValue ));  // i am getting all the values based on the query.
               console.log('Value ::::::: '+fieldValue );
 
<aur:attribute name="ABC" type="String" />

 
AchyuthAchyuth
Hi Raj,
Actually i done the same thing.In the abve code "v.Accountlist "is an attribute where i set the result from acclist. by using " console.log(JSON.stringify(fieldValue )); " i can able to see the result .But i want to assign the only "name" field from the result to another variable from controller to do one if else logic..
If name='raj' then it should call the flow
else.... it should call another flow..
Can u help me on this !!

Thanks,
Chinna
Ravi Dutt SharmaRavi Dutt Sharma
The return type of your apex method is List of accounts. And you said in your last answer that you want to assign the name field to a variable. Which records name you want to assign to the variable? The first record in the list? If yes, then use below code:
 
component.set("v.ABC", response.getReturnValue()[0].Name);

If this helps you, please close the thread by marking this as the best answer. Thanks.