1

This is a method in my apex controller class:

@AuraEnabled
public static void sendMail(List<Object> selectedRows) {
     if (selectedRows.size() > 0) {
          for (Object obj : selectedRows) {
               Map<Object, Object> mapJson = (Map<Object, Object>)obj;
               String title = (String)mapJson.get('title');
               String link = (String)mapJson.get('link');

               //rest of the code
          }
     }
}

The selectedRows list consist of selected rows from a lightning:datatable which have field names 'link' and 'title'. Here is the code:

<aura:component>
    <lightning:datatable
         keyField="id"
         data="{! v.providerList }"
         columns="{! v.columns }"
         hideCheckboxColumn="false"
         onrowselection="{!c.selectedRowHandler}"/>
</aura:component>

//controller.js
selectedRowHandler: function(component, event, helper) {
    var selectedRows = event.getParam('selectedRows');
    component.set("v.userSelectedRows", selectedRows);
},

userSelectedRows is the attribute I send to the apex method.

This code works fine. The problem I have is that, in the test method of the above method, I cannot mock selectedRows. So, how can I create a Object list with 'link' and 'title' field names?

1 Answer 1

2

First, in your actual code, it should be Map<String, Object>, as no other types of keys are valid in JSON. Next, you just need to deserializeUntyped:

List<Object> selectedRows = (List<Object>)JSON.deserializeUntyped(
 '[{"title":"Title1","link":"https://www.google.com"}]');

And pass that in as the parameter.

1
  • It worked. I think this is the fifth or sixth time u answering my question. U r a real life saver. Thanks very much. Commented Jul 24, 2020 at 15:45

You must log in to answer this question.

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