Showing results for 
Search instead for 
Did you mean: 
Helper V
Helper V

Web API | iterate through the organisation structure of a contact



We have a requirement where the account lookup (on a form within a webpage) should be pre-filled with an account of the contact (or parentaccount) where a specific field (xxx_IsAgency) is set to YES. 


For this purpose, the organization tree must be iterated recursively from the contact upwards and a stop must be made at the point where "xxx_IsAgency" = Yes can be found. This organization must then be stored in the account lookup field.


Any suggestions (maybe an example) how to properly define the WEB API for this request?


Thank you very much!

Super User
Super User

Hi, I don't think you need to run iterate through all the account records.

You need a read operation "accounts where primarycontact = and xxx_isAgency = yes"

this might bring multiple results, so you might need to define a sorting criteria, and simply pick the first 

If you like this post, give a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users find it.

Power Apps Portals Super User

Oliver Rodrigues


Solution Sage
Solution Sage

If it is multi-level hieratical you may need to look at 'above' or 'under' type conditions - not sure how this will go with the Web Api

Helper V
Helper V

Hi @Fubar,


yes it is most likely multi-level hieratical. Do you know how to properly write it if the query should give me the first hieratical organisation that has the field set to yes?


type: "GET",
url: "https://"+ window.location.hostname+ `/_api/accounts(${accountId})?$select=`,
contentType: "application/json",
success: function (res) {



Contact => Org 1 (field = no) ==> Org 2 (field = no) ==> Org 3 (field = Yes)





Not sure if it will work with the Portal Web API, nor exactly what your query needs to be but the general 'Above' would be something like



(use XrmToolBox and the FetchXML builder, you can then see what the oData equivalent is of the FetchXML)


If the Portal Web API does not support it you can always revert to the old way of setting up a portal page with a liquid template making a FetchXML query and then calling that page via Ajax.

Helpful resources

Welcome Super Users.png

Welcome Super Users

The Super User program for 2022 - Season 2 has kicked off!

September Events 2022

Check out all of these events

Attend in person or online, there are incredible conferences and events happening all throughout the month of September.

Users online (2,175)