I'm trying to create REST API where I will return all accounts with Type = Parent and their child accounts.

I have Account Hierarchy in place and I'm confused on how to return Parent account A and child accounts x,y,z which have parentID = A.Id in single call.

I'm returning Multiple parent accounts and their child accounts.


Without getting into the low level of implementation details, if I had to solve this requirement, I would return a JSON from the API. Assuming you already have the logic to pull the hierarchy for parent and their child accounts in place, you could create a JSON response which would consist of the Parent accounts and their Child accounts in an array. A sample structure would have looked liked as below:

  "1Parent": [
  "2Parent": [

Two part answers.

  1. You can use soql child relationship query to get all associated child accounts for given account. By adding Type = Parent predicate, you are selecting parents its children. Keep in mind this can lead to limit issues if you have lots of child records.

  2. Returning this information in rest response is pretty straight forward. You can return the parent objects itself, which should serialize into json. Or you could create class to represent the format for full control and return those entities.

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.