You need to sign in to do that
Don't have an account?
SKT
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:
From the above code,
Thanks!
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
Thanks!
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.
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!
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 this above code will give output in JSON string 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