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
Alexander AtkinsonAlexander Atkinson 

Lightning Component: How do I pass a list from Apex to a JavaScript controller and assign record fields to variables?

Hello I am creating a lightning component. 
I have a list of accounts obtained by a query in an apex class. I want to send this list to an array in a javascript controller that will let me assign the record values to variables.

Apex:
public with sharing class AccountController 
{
    @AuraEnabled
    public static List<Account> getAccounts() 
    {
        List<Account> Accounts =  [SELECT Id, Name FROM Account];
        return Accounts;
    }
}

JavaScript controller:
    doInit: function(component, event, helper) 
    {
        ///Obtain account record list from Apex SOQL and pass it to an array.
        //var records = [] ???
  
        //This list is what will be passed to lightning component when function ends.
        var newItems=[];
        
        //Loop through records array and create item list using record field values.
        for (var i=0; i< records.length; i++)
        {
            var record = records[i];
            var Item = {title: record.name, id: record.id, status: "Unassigned"};
            newItems.push(Item);
        }  
        //Pass items into component
        component.set("v.allItems", newItems);
    },
Best Answer chosen by Alexander Atkinson
Khan AnasKhan Anas (Salesforce Developers) 
Hi Alexander,

I trust you are doing very well.

Please try below code:
 
doInit : function(component, event, helper) {
        var action = component.get("c.getAccounts");
        action.setCallback(this,function(response){   
         
            var state = response.getState();
            if(state === "SUCCESS"){
                var records = response.getReturnValue();
                console.log('Server-> ' + JSON.stringify(records));
                
                var newItems=[];
                for (var i=0; i< records.length; i++)
                {
                    var record = records[i];
                    console.log('record-> ' + JSON.stringify(record));

                    var Item = {title: record.Name, id: record.Id, status: "Unassigned"};
                    console.log('Item-> ' + JSON.stringify(Item));

                    newItems.push(Item);
                    console.log('newItems-> ' + JSON.stringify(newItems));
                }
                component.set("v.allItems", newItems);
            }
        });
        $A.enqueueAction(action);
 },

Note: Please take care of lowercase and uppercase as JavaScript is case-sensitive.


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Alexander,

I trust you are doing very well.

Please try below code:
 
doInit : function(component, event, helper) {
        var action = component.get("c.getAccounts");
        action.setCallback(this,function(response){   
         
            var state = response.getState();
            if(state === "SUCCESS"){
                var records = response.getReturnValue();
                console.log('Server-> ' + JSON.stringify(records));
                
                var newItems=[];
                for (var i=0; i< records.length; i++)
                {
                    var record = records[i];
                    console.log('record-> ' + JSON.stringify(record));

                    var Item = {title: record.Name, id: record.Id, status: "Unassigned"};
                    console.log('Item-> ' + JSON.stringify(Item));

                    newItems.push(Item);
                    console.log('newItems-> ' + JSON.stringify(newItems));
                }
                component.set("v.allItems", newItems);
            }
        });
        $A.enqueueAction(action);
 },

Note: Please take care of lowercase and uppercase as JavaScript is case-sensitive.


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Alexander AtkinsonAlexander Atkinson
It works, thank you so much!