[SalesForce] Pass list attribute from lightning component to apex controller

I have an attribute in lightning component:

<aura:attribute name="pricingRecommendationListDetailsData" access="PUBLIC" type="list" default="object[]" />

Data stored in it visible as:

{accRecmId: "a07e000", category: "test", Price: 56, PriceRange: {…}, cost: 0.3, …}

Now I need to pass this attribute as parameter to apex controller on button click.And I need to iterate over values such as accRecmId in apex class

Best Answer

you have two option:

  1. Send only the accRecmId to your Server method like this:

JS Controller:

var selectedList = component.get('v.pricingRecommendationListDetailsData');

    var ids=new Array();
    for (var i= 0 ; i < selectedList.length ; i++){
        ids.push(selectedList[i].accRecmId);
    }

    var idListJSON=JSON.stringify(ids);

    var action = component.get("c.callServier");
    action.setParams({
            "accRecmId":idListJSON
    });

Apex Controller:

public static void callServier(String accRecmId) {
    Type idArrType = Type.forName('List<string>');
    List<string> wrapperList = (List<string>) JSON.deserialize(accRecmId, idArrType);
}
  1. Pass all attribute to your apex controller and inside your apex controller get the accRecmId.