Pass list attribute from lightning component to apex controller

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












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










share|improve this question





















  • You would simply pass it using the method param name and the component.get of the attribute
    – Eric
    Aug 8 at 7:12










  • But how will I iterate over the list<string> and grab accRecmId from each instance?
    – devforce
    Aug 8 at 7:13










  • More of a map isn’t it?
    – Eric
    Aug 8 at 7:15
















up vote
1
down vote

favorite












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










share|improve this question





















  • You would simply pass it using the method param name and the component.get of the attribute
    – Eric
    Aug 8 at 7:12










  • But how will I iterate over the list<string> and grab accRecmId from each instance?
    – devforce
    Aug 8 at 7:13










  • More of a map isn’t it?
    – Eric
    Aug 8 at 7:15












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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










share|improve this question













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







lightning-components






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 8 at 6:55









devforce

10710




10710











  • You would simply pass it using the method param name and the component.get of the attribute
    – Eric
    Aug 8 at 7:12










  • But how will I iterate over the list<string> and grab accRecmId from each instance?
    – devforce
    Aug 8 at 7:13










  • More of a map isn’t it?
    – Eric
    Aug 8 at 7:15
















  • You would simply pass it using the method param name and the component.get of the attribute
    – Eric
    Aug 8 at 7:12










  • But how will I iterate over the list<string> and grab accRecmId from each instance?
    – devforce
    Aug 8 at 7:13










  • More of a map isn’t it?
    – Eric
    Aug 8 at 7:15















You would simply pass it using the method param name and the component.get of the attribute
– Eric
Aug 8 at 7:12




You would simply pass it using the method param name and the component.get of the attribute
– Eric
Aug 8 at 7:12












But how will I iterate over the list<string> and grab accRecmId from each instance?
– devforce
Aug 8 at 7:13




But how will I iterate over the list<string> and grab accRecmId from each instance?
– devforce
Aug 8 at 7:13












More of a map isn’t it?
– Eric
Aug 8 at 7:15




More of a map isn’t it?
– Eric
Aug 8 at 7:15










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










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.





share|improve this answer




















  • var ids = selectedList.map(value => value.accRecmId); There's usually a shorter way than a for(...) loop in modern JS.
    – sfdcfox
    Aug 8 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 at 16:14

















up vote
1
down vote













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...







share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "459"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f228109%2fpass-list-attribute-from-lightning-component-to-apex-controller%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    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.





    share|improve this answer




















    • var ids = selectedList.map(value => value.accRecmId); There's usually a shorter way than a for(...) loop in modern JS.
      – sfdcfox
      Aug 8 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 at 16:14














    up vote
    4
    down vote



    accepted










    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.





    share|improve this answer




















    • var ids = selectedList.map(value => value.accRecmId); There's usually a shorter way than a for(...) loop in modern JS.
      – sfdcfox
      Aug 8 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 at 16:14












    up vote
    4
    down vote



    accepted







    up vote
    4
    down vote



    accepted






    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.





    share|improve this 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.






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 8 at 7:17









    salesforce Developer

    1,20621329




    1,20621329











    • var ids = selectedList.map(value => value.accRecmId); There's usually a shorter way than a for(...) loop in modern JS.
      – sfdcfox
      Aug 8 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 at 16:14
















    • var ids = selectedList.map(value => value.accRecmId); There's usually a shorter way than a for(...) loop in modern JS.
      – sfdcfox
      Aug 8 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 at 16:14















    var ids = selectedList.map(value => value.accRecmId); There's usually a shorter way than a for(...) loop in modern JS.
    – sfdcfox
    Aug 8 at 10:10




    var ids = selectedList.map(value => value.accRecmId); There's usually a shorter way than a for(...) loop in modern JS.
    – sfdcfox
    Aug 8 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 at 16:14




    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 at 16:14












    up vote
    1
    down vote













    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...







    share|improve this answer


























      up vote
      1
      down vote













      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...







      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        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...







        share|improve this answer














        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...








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 8 at 10:10









        sfdcfox

        229k10177391




        229k10177391










        answered Aug 8 at 9:02









        Ruslan Kurchenko

        16018




        16018



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f228109%2fpass-list-attribute-from-lightning-component-to-apex-controller%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            pylint3 and pip3 broken

            Missing snmpget and snmpwalk

            How to enroll fingerprints to Ubuntu 17.10 with VFS491