function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SKTSKT 

Append One JSON node under other in Apex

I have a requirement to display Accounts-->child Accounts-->Tasks-->Child Tasks in the JSON.

Here is my Apex code:
 
List<Account> accountRecordsList = new list<Account>(); 
List<Custom_Tasks__c> taskRecordsList = new list<Custom_Tasks__c>(); 

accountRecordsList = [SELECT Id,Name,parent_account__c, (SELECT Id, Name FROM Accounts1__r) FROM Account WHERE Id = '<some record id>'];
string strJSON = JSON.serialize(accountRecordsList); //Account and Child Accounts

 Set<Id> accountIds = new Set<Id>();
        for(Account pr : accountRecordsList){
            for(Account cpr : pr.Accounts1__r){
            if(cpr.id != null){
                accountIds.add(cpr.id);
            }
            }
        }
taskRecordsList = [SELECT id, name,account__c, (SELECT id, name,pse__Start_Date__c, pse__End_Date__c from Sub_Tasks1__r ) from Custom_Tasks__c where account__c in :accountIds];
string ptJSON = JSON.serialize(taskRecordsList); //Tasks and Child Tasks related to above obtained child accounts

From the above code,
  • in strJSON, I am getting Accounts and its child accounts data.
  • in ptJSON, I am getting Tasks and Child Tasks related to above Child accounts
Now, is there anyway, I can append ptJSON under its related child Accounts in strJSON. Can anyone please suggest solution on how to get this done.

Thanks! 
Surya GSurya G
Hi SKT,

I believe, we can achieve this using wrapper class. create child objects list  variable as hierarchy inside wrapper class and combine the values using wrapper.

 
SKTSKT
Hi Surya,

Based on my requirement, could you please suggest sample code on how to do so. I am new to development and not much fluent with Wrapper classes. Any suggestion will be highly helpful. 

Thanks!
Surya GSurya G
Hi SKT,
Here is the sample code that has relationships like  Parent Account > Child Account > Cases > Tasks
for this example,
I have 1 parent account - Burlington Textiles Corp of America,
1 child account - Burlington corp of UK
1 relates case to child account - '5005g00000Ch3UjAAJ'
2  related task to case -Email & Send Quote
public class RelationshipWrapper {

    Public class Accounts {
        public Account accounts {get;set;}
        public List<ChildAccount> childAccounts {get;set;}
    }
    public class ChildAccount {
        public Account account {get;set;}
        public List<AccountCase> cases {get;set;}
    }
    public class AccountCase {
        public Case accountCase {get;set;}
        public List<Task> caseTasks {get;set;}
    }
    
    public static String getAccountRelation() {
        List<Account> parentAccount = new List<Account>();
		List<Accounts> finalList = new List<Accounts>();
        Set<Id> accountIds = new Set<Id>();
        parentAccount = [select Id, Name from Account WHERE id='0015g000003X9oZAAS'];
        for (Account acc : parentAccount) {
                Accounts accs = new Accounts();
                List<ChildAccount> childAccountList = new List<ChildAccount>();
                for (Account childAcc : [SELECT Id,Name,ParentId,Parent.name FROM Account WHERE ParentId = :acc.Id] ) {
                    ChildAccount chiAcc = new ChildAccount();
                    chiAcc.account = childAcc;
                    childAccountList.add(chiAcc);
                    accountIds.add(childAcc.Id);
                }
            	accs.accounts = acc;
            	accs.childAccounts = childAccountList;
            	finalList.add(accs);
        }
		List<case> caseList = [SELECT id,accountId, (select subject from tasks) from case where accountId IN :accountIds];
        for(Accounts acc : finalList) {        
            for (ChildAccount CA : acc.childAccounts) {
                List<AccountCase> accountCaseList = new List<AccountCase>();
                for(Case caseRecord : caseList) {
                    if(CA.account.Id == caseRecord.accountId ) {
                        AccountCase AC=  new AccountCase();
                        AC.accountCase = caseRecord;
                        AC.caseTasks = caseRecord.Tasks;
                        accountCaseList.add(AC);
                    }
                }
                CA.cases = accountCaseList;
            }
        }
	 return JSON.serialize(finalList);
     }
}
this above code will give output in JSON string 
[{"childAccounts":[{"cases":[{"caseTasks":[{"attributes":{"type":"Task","url":"/services/data/v52.0/sobjects/Task/00T5g00000M7JxZEAV"},"WhatId":"5005g00000Ch3UjAAJ","Id":"00T5g00000M7JxZEAV","Subject":"Email"},{"attributes":{"type":"Task","url":"/services/data/v52.0/sobjects/Task/00T5g00000M7JxeEAF"},"WhatId":"5005g00000Ch3UjAAJ","Id":"00T5g00000M7JxeEAF","Subject":"Send Quote"}],"accountCase":{"attributes":{"type":"Case","url":"/services/data/v52.0/sobjects/Case/5005g00000Ch3UjAAJ"},"Id":"5005g00000Ch3UjAAJ","AccountId":"0015g00000JLuU5AAL","Tasks":{"totalSize":2,"done":true,"records":[{"attributes":{"type":"Task","url":"/services/data/v52.0/sobjects/Task/00T5g00000M7JxZEAV"},"WhatId":"5005g00000Ch3UjAAJ","Id":"00T5g00000M7JxZEAV","Subject":"Email"},{"attributes":{"type":"Task","url":"/services/data/v52.0/sobjects/Task/00T5g00000M7JxeEAF"},"WhatId":"5005g00000Ch3UjAAJ","Id":"00T5g00000M7JxeEAF","Subject":"Send Quote"}]}}}],"account":{"attributes":{"type":"Account","url":"/services/data/v52.0/sobjects/Account/0015g00000JLuU5AAL"},"Id":"0015g00000JLuU5AAL","Name":"Burlington corp of UK","ParentId":"0015g000003X9oZAAS","Parent":{"attributes":{"type":"Account","url":"/services/data/v52.0/sobjects/Account/0015g000003X9oZAAS"},"Id":"0015g000003X9oZAAS","Name":"Burlington Textiles Corp of America"}}}],"accounts":{"attributes":{"type":"Account","url":"/services/data/v52.0/sobjects/Account/0015g000003X9oZAAS"},"Id":"0015g000003X9oZAAS","Name":"Burlington Textiles Corp of America"}}]
I believe there must be some other approach aswell, because, this seems bit lengthy process.But this is what i could come up with for now and it will work for your requirement.

Thanks
Surya G