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

I have the same question (0)
  • 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

Responsible AI policies

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

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 72 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 29 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans