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

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Carousel_PP_768x460_Wave2 (1).png

2022 Release Wave 2 Plan

Power Platform release plan for the 2022 release wave 2 describes all new features releasing from October 2022 through March 2023.

Users online (4,717)