7
public class A
{
@AuraEnabled
public static contact method(){
public account ac;
public contact ct;
ac=[select id,name from account Limit 1];
ct=[select id,name from contact LIMIT 1];
return ct;
}

In Lightning : I know that one method return only one value. Here I'am returning contact type at the the time of javascript call

My question: Is it possible to return account value ac,Contact value ct [I mean both value] at one shot, at the time of java script controller call.Why beacuse after this method call I want to to access ac value and ct value in component so..

public class A{
@AuraEnabled
public static contact method1(){
public account ac;
public contact ct;
ac=[select id,name from account Limit 1];
ct=[select id,name from contact LIMIT 1];
return ct;
}
@AuraEnabled
public static void method2(){
//Here I want to access ac.name
}

In the java script call,I called a method1 and set to component attribute .It contains only contact.

Question 2: shall I access ac.name in method2 [its static method we cant access I know] but these kind of scenario.Whats the best practise?

14

There are primarily two approaches

1. Server side Wrapper in apex

This can be handy if the number of rows and query are within apex governor limit .Example can be lets say you return less than 50K rows and total number of queries you need is less than 100 .

You can generate a wrapper schema like above

public class AccountContactWrapper {
  @AuraEnabled
  public list<Account> accounts {get;set;}
  @AuraEnabled
  public list<contact> contacts {get;set;}
  public AccountContactWrapper(){
      this.accounts = new list<Account>();
      this.contacts = new list<contacts>();
  }
}

public class Response{
@AuraEnabled
 public static AccountContactWrapper method(){
    AccountAndContact acWrapper = new AccountAndContact();
    acWrapper.accounts = [select id, name from account limit 20000];
    acWrapper.contacts = [select id, name from contact LIMIT 20000];
    return acWrapper;
 }
}

2.Using Promise In Javascript

Javascript ES6 Promises are commonly used to call one method after other and resolve them as you wish on the client side .The benefit of this method is you can chain multiple server side calls .

Here is a pattern to help call one function after the other

accountPromise.then(
    $A.getCallback(function(result){
        // We have the account - set the attribute
        cmp.set('v.account', result);

        // return a promise to retrieve a contact
        var contAction = cmp.get("c.GetContact");
        var contParams={"accountIdStr":accId};
        contAction.setParams(contParams);
        var contPromise=self.executeAction(cmp, contAction);
        return contPromise;
    })
  )
   .then(
       $A.getCallback(function(result){
        // We have the contact - set the attribute
        cmp.set('v.contact', result);
      })
  .catch(
     $A.getCallback(function(error){
        // Something went wrong
        alert('An error occurred : ' + e.message);
    })
 ); 

Here is a neat blogpost that shows how to use them

If Promises are hard to wrap your heads around you can use below simplified callback pattern as well

// better version - separate callback functions
getAccountOwner2 : function(cmp, event, helper) {
    // cleanup
    cmp.find("contactName").set('v.value', '');
    cmp.find("accountName").set('v.value', '');
    cmp.find("ownerName").set('v.value', '');

    var getContact = cmp.get("c.getContact");
    getContact.setParams( {"contactId": cmp.get("v.recordId")} );
    getContact.setCallback(this, contactCallback);
    $A.enqueueAction(getContact);

    function contactCallback(contact) {
        contact = contact.getReturnValue();
        cmp.find("contactName").set('v.value', contact.Name);
        var getAccount = cmp.get("c.getAccount");
        getAccount.setParams( {"accountId": contact.AccountId} );
        getAccount.setCallback(this, accountCallback);
        $A.enqueueAction(getAccount);
    }
    function accountCallback(account) {
        account = account.getReturnValue();
        cmp.find("accountName").set('v.value', account.Name);
        var getUser = cmp.get("c.getUser");
        getUser.setParams( {"userId": account.OwnerId} );
        getUser.setCallback(this, userCallback);
        $A.enqueueAction(getUser);
    }
    function userCallback(user) {
        user = user.getReturnValue();
        cmp.find("ownerName").set('v.value', user.Name);
    }
}
0
2

You can simply use a map as output parameter and return for example:

return new Map<string,object> {'account': acc, 'contact': con}
1

If you want to return both an Account and a Contact from a method, you just have to define a data structure that contains an account and a contact:

public class AccountAndContact {
    @AuraEnabled public Account a {get; set;}
    @AuraEnabled public Contact c {get; set;}
}

An then in your Apex controller, return your account and contact records as part of that data structure:

public class A{
    @AuraEnabled
    public static AccountAndContact method1(){
        private AccountAndContact ac = new AccountAndContact();
        ac.a = [select id,name from account Limit 1];
        ac.c = [select id,name from contact LIMIT 1];
        return ac;
    }
}
2
  • return type is AccountAndContact right?..... Then what about second question??.....this scenario contains only one account then its fine..but in real time sceanrio within a method it may contains so many queries right..if I need any query value means every time I have to use this kind of data structure? is it good approach? – SFDC Learner Mar 22 '17 at 5:00
  • @AnnappaPH yes the return type would be AccountAndContact. As for the number of queries, it depends on your application. If you have a lot of account/contact pairs that you want to display on a page, you would probably want one lightning component to query a list of such pairs and define the overall page structure, and iterate over that list, using another lightning component that takes a single account and contact as attributes that define what to do with each individual pair. – martin Mar 22 '17 at 5:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.