0

I am new to the lightning component. I want to get two lists in the js controller how it's possible?


 public class ContactListController {

    
@AuraEnabled
public  static List<Contact> getContactlist(){
        List<integer> listTwo=new list<integer>();
         listTwo.add(1);
        listTwo.add(4);
         listTwo.add(9);
        List<Contact> listOne= new  List<Contact>([select id, FirstName,LastName,email,Phone from Contact]);
    
    
       return listOne;
        
    }

}

Js controller

({
    doInit : function(component, event, helper) {
        var action= component.get('c.getContactlist');
       
        action.setCallback(this,               
             function(response){
            var responseValue = response.getReturnValue();
            
           component.set('v.contactlist',responseValue);
        },'SUCCESS');
        $A.enqueueAction(action,false)
        
    }
})

Now Question is how can we get two lists in js controller. the getContactlist return only one list. I want to return many things from the same method like integer , string, boolean etc

1 Answer 1

2

This example is not especially realistic, but the general solution remains the same: define a custom type that wraps all of the data you're interested in transferring, and return an instance of that class:

public class MyDTO {
    @AuraEnabled
    public List<Contact> contacts { get; set; }
    @AuraEnabled
    public List<Integer> ints { get; set; }
    // ...
}

See the Aura Developer Guide for more information and details on shipping data back and forth between Aura and Apex.

6
  • so how can I get these two lists in the js controller?
    – user91246
    Jun 15, 2021 at 18:14
  • David has linked the resources to get you started, if you are expecting someone to provide you a complete code sample, note that this is not the purpose of this site.
    – glls
    Jun 15, 2021 at 18:19
  • 1
    Your getReturnValue() result has a shape as a JavaScript object that matches your Apex class shape.
    – David Reed
    Jun 15, 2021 at 18:24
  • like that `` component.set('v.contactlist',responseValue.contactlsit);`` and `` component.set('v.intlist',responseValue.ints);`` ?
    – user91246
    Jun 15, 2021 at 18:26
  • 1
    @fawad, if you are new to lwc and/or aura, I would strongly suggest you follow the corresponding trailhead modules.
    – glls
    Jun 15, 2021 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy