New to Lightning and trying to figure out how to process a list of sobject/custom metadata type records returned by Apex class in js controller before passing them to the component.

Apex Controller

public static List<HDS_Service_Setting__mdt> getServiceCategoryAndServicesByFundingType(string inputFundingTypeId) {
    system.debug('Dev Debug - Method Entered - FundingAndServicesController.getServiceCategoryAndServicesByFundingType()');
    string inputFundingTypeName = 'HCP';

    List<HDS_Service_Setting__mdt> lstServices = [SELECT Funding_Type__c,Service_Category__c,Service__c FROM HDS_Service_Setting__mdt WHERE Funding_Type__c=:inputFundingTypeName ];

    system.debug('Dev Debug - size of lstServices - '+lstServices.size());
    return lstServices;

JS Controller

var recordTypeId = component.get("v.recordTypeId");

    var action_getCategoryAndServiceByFundingType = component.get("c.getServiceCategoryAndServicesByFundingType");
    action_getCategoryAndServiceByFundingType.setParams({  inputFundingTypeId : recordTypeId  });
    action_getCategoryAndServiceByFundingType.setCallback(this, function(a) {
        var categoryAndService = a.getReturnValue();
        for(var thisRecord in categoryAndService)

        component.set("v.CategoryAndService", categoryAndService);

        //var myJSON = JSON.stringify(categoryAndService); 

What the alert gave me is undefined for each record.

Anyone can tell me how I can get the field value from each sObject record? Thank you.

2 Answers 2


First of all (but maybe this is slightly overkill in this case) I would recommend to use wrapper inner class for sending data to the component. In your case it would be something like:

public class ServiceSettingWrapper {
    String service {get; set;}
    public ServiceSettingWrapper(String service) {
        this.service = service;

Of course if Service__c is a string. Then in your method you would need to create instance of this inner class add it ot a list and return.

List<HDS_Service_Setting__mdt> lstServices = [SELECT Funding_Type__c,Service_Category__c,Service__c FROM HDS_Service_Setting__mdt WHERE Funding_Type__c=:inputFundingTypeName ];

List<ServiceSettingWrapper> returnList = new List<ServiceSettingWrapper>();
for(HDS_Service_Setting_mdt setting : lstServices){
    returnList.add(new ServiceSettingWrapper(setting.Funding_Type__c,Service_Category__c,Service__c)

//In the end of methid
return returnList

Second thing: Please check state of the response so you can handle eventual errors:

var state = response.getState();
        if (state === "SUCCESS") {
        //Actions for success
        } else if (state === "ERROR") {
          //Actions for error
        } else {
         //Another stuff

And finally to your problem:

var listLength = categoryAndService.length;
for (var i = 0; i < listLength; i++) {
    alert(categoryAndService[i]); -- which will return an object
    alert(categoryAndService[i].service); -- which wil lreturn specific field

Don't use for(var x in y); that causes you to iterate through the array's keys, which is not what you intended to do. Instead, simply use forEach:

categoryAndService.forEach(record => console.log(record.Service_Category__c));

Note that JavaScript is also case sensitive, so you might need to check the capitalization of the field.


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.