2

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

3
  • You would simply pass it using the method param name and the component.get of the attribute
    – Eric
    Aug 8, 2018 at 7:12
  • But how will I iterate over the list<string> and grab accRecmId from each instance?
    – devforce
    Aug 8, 2018 at 7:13
  • More of a map isn’t it?
    – Eric
    Aug 8, 2018 at 7:15

2 Answers 2

7

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.
2
  • var ids = selectedList.map(value => value.accRecmId); There's usually a shorter way than a for(...) loop in modern JS.
    – sfdcfox
    Aug 8, 2018 at 10:10
  • Why not just pass the map to the apex method and do the work needed there? Provide much more flexibility to extend the functionality later on....Maybe I am missing something.
    – Eric
    Aug 8, 2018 at 16:14
1

My recommendation is to prepare the required data on the client and send only the list of ids you have to work within the server:

MyComponentController.js

// get the list of pricing details
var pricingDetails = component.get('v.pricingRecommendationListDetailsData');
// map through the list to create a list of acc rec ids
var accRecmIds = pricingDetails.map(function (detail) {
    return detail.accRecmId;
});

// get the server call action
var action = component.get("c.iterateOverAccRecmIds");
// set the list of acc rec ids as a parameter
action.setParams({
    "accRecmIds":accRecmIds
});

MyComponentController.cls

public with sharing class MyComponentController {
    @AuraEnabled
    public static void iterateOverAccRecmIds(List<String> accRecmIds) {
        // do what you want with the list of ids...
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .