web
You’re offline. This is a read only version of the page.
close
Skip to main content
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

I have the same question (0)
  • Guido Preite Profile Picture
    54,084 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,084 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daniyal Khaleel Profile Picture

Daniyal Khaleel 143

#2
DAnny3211 Profile Picture

DAnny3211 134

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 70 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans