+ Start a Discussion
Manj_SFDCManj_SFDC 

Need help on parsing response from URL mapping

Hello everyone, I am using this in my REST class

List <Account> accountList;
        try {
            accountList = [SELECT Id, Name,(SELECT Id, Name FROM Contacts) FROM Account];
            return accountList;
        }

in the RESTHandler I am getting the response as lsit of accounts with associated contacts , I need the count of number of accounts in the response, can someone help 
Best Answer chosen by Manj_SFDC
Manj_SFDCManj_SFDC
I was able to do this using the JSON Wrapper generated using https://json2apex.herokuapp.com/

All Answers

Waqar Hussain SFWaqar Hussain SF
Create a wrapper class inside your REST class
global class AccountWrapper{
            global list<Account> Accs;
            global integer TotalAccounts;
        }

 Now update your code as       
List <AccountWrapper> accountList = new list<AccountWrapper>();
        try {
            accounts = [SELECT Id, Name,(SELECT Id, Name FROM Contacts) FROM Account];
            AccountWrapper aw = new AccountWrapper();
            aw.Accs = accounts;
            aw.TotalAccounts = accounts.size();
            accountList.add(aw);
            
            return accountList;
        }

 
Manj_SFDCManj_SFDC
Thaank you, can we not achieve using RestResponse restRes = Restcontext.response; 
Manj_SFDCManj_SFDC
when I try to retrieve in the Reponse Handler class, its giving me only the Accounts but not contacts, please suggest
Manj_SFDCManj_SFDC
I was able to do this using the JSON Wrapper generated using https://json2apex.herokuapp.com/
This was selected as the best answer