Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

For a method in apex we are creating accounts and rather than inserting we are directly returning the list out of that method in apex?

My code:

public XXXXX createAccount(some arguments){
    List<Account> accts = new List<Account>();
    //some code
    accList.add(accts);
    return accList;
}

What should be the datatype here ? I tried sObjects that doesn't works.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

You are returning accList which (I assume) is of type List<Account> so that is that the datatype should be using. See example below.

public List<Account> createAccount(some arguments){
    List<Account> accts = new List<Account>();
    //some code
    accList.add(accts);
    return accList;
}
share|improve this answer
    
thanks !!!....just can we return acclist and contList(Contact related to account) in the same method using <sObjects>......a code snippet will be helpful. –  codeath Sep 2 at 9:04
1  
@codeath If you query the Contacts in the same SOQL as the Accounts, then each Account object returned will have a relationship field populated (called Contacts) that is a list of the related Contact objects. So the signature posted above will work unchanged to do what you want; probably best though to change the method name to something like "queryAccountsAndRelatedContacts" to try and make what is going on clearer. –  Keith C Sep 2 at 9:32

You are returning List<Account>, as accList is a list of List<Account> . So method should be like below -

public List<Account> createAccount(some arguments){
    List<Account> accts = new List<Account>();
    accList.add(accts);
    return accList;
}
share|improve this answer

Your original question has been answered, you need to return List<Account>.

As for how to return Accounts with Contacts, you could use a multi dimensional map, like so:

public Map<Id, Map<Account, List<Contact>>> createAccount(some arguments){

    Map<Id, Map<Account, List<Contact>>> = accountContactsMap = new Map<Id, Map<Account, List<Contact>>>();

    // Query for accounts & contacts
    for (Account acc : [SELECT Id, (SELECT Id FROM Contacts) FROM Account]) {

        accountContactsMap.put(acc.Id, new Map<Account, List<Contact>>);
        accountContactsMap.get(acc.Id).put(acc, acc.Contacts);

    }

    return accountContactsMap
}
share|improve this answer
    
okay thanks...thats working fine –  codeath Sep 2 at 9:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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