web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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

  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    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

  • AndreasB83 Profile Picture
    115 on at

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

  • Guido Preite Profile Picture
    54,086 Moderator on at

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

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Syed Aqib Raza Profile Picture

Syed Aqib Raza 96

#2
11manish Profile Picture

11manish 90 Super User 2026 Season 2

#3
ParthPatel249 Profile Picture

ParthPatel249 80

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans