Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Query nested relations on web api

(0) ShareShare
ReportReport
Posted on by 115

Via REST Builder i created a statement to get all workorders with related workorderservicetask and another statement to recieve the inspection of a servicetask. But i dont know how to combine them correctly resulting in nested expands.


Get workorders with related servicetasks

Xrm.WebApi.retrieveRecord(
            "msdyn_workorder",
            "6bf60eff-2e64-ed11-9561-6045bd8edfa9",
            `?$select=msdyn_workorderid
        	&$expand=msdyn_msdyn_workorder_msdyn_workorderservicetask_WorkOrder(
            $select=msdyn_name,
            msdyn_description,
            msdyn_workorderservicetaskid,
            msdyn_actualduration,
     	)`).then(function success(result) {console.log(result);
	},
	function(error) {
		console.log(error.message);
	}
);

Get servicetask with related inspection

Xrm.WebApi.retrieveRecord(
	"msdyn_workorderservicetask",
	"2ab23304-2f64-ed11-9562-6045bd8c9366",
	"?$expand=msdyn_Inspection($select=msdyn_inspectionid,msdyn_description,msdyn_name,statecode,msdyn_state,statuscode)").then(
	function success(result) {
		console.log(result);
	},
	function(error) {
		console.log(error.message);
	}
);

I would expect that i have to expand the "msdyn_inspection" property of the servicetasks like this but i get the error "Query option '$expand' was specified more than once, but it must be specified at most once."

Xrm.WebApi.retrieveRecord(
            "msdyn_workorder",
            "6bf60eff-2e64-ed11-9561-6045bd8edfa9",
            `?$select=msdyn_workorderid
        &$expand=msdyn_msdyn_workorder_msdyn_workorderservicetask_WorkOrder(
            $select=msdyn_name,
            msdyn_description,
            msdyn_workorderservicetaskid,
            msdyn_actualduration,
            msdyn_inspection
            &$expand=msdyn_Inspection($select=msdyn_inspectionid,msdyn_description,msdyn_name,statecode,msdyn_state,statuscode)

     	)`).then(function success(result) {console.log(result);
	},
	function(error) {
		console.log(error.message);
	}
);

Screenshot-2022_2D00_12_2D00_01-185943.png

  • Guido Preite Profile Picture
    54,075 Moderator on at
    RE: Query nested relations on web api

    Xrm.WebApi.retrieveMultipleRecords support the fetchXML parameter, inside Dataverse REST Builder the code will escape the fetchxml before passing it

  • AndreasB83 Profile Picture
    115 on at
    RE: Query nested relations on web api

    Would like to use it in a pcf component - not sure if an urlencoded fetchxml will work with webAPI.retrieveRecord

  • Suggested answer
    Guido Preite Profile Picture
    54,075 Moderator on at
    RE: Query nested relations on web api

    there is a $expand syntax to query the nested relations (in your example work order -> service task -> inspection) but afaik the values from the latest relation (inspection in your case) return null. The alternative is to use fetchxml, you can create the query using fetchxml (using with fetchxml builder) and after you can execute it using the "Predefined Query" request type inside Dataverse REST Builder (Query Type select FetchXML)

    hope it helps

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,001 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,833 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans