Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

Query nested relations on web api

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

Categories:
  • Guido Preite Profile Picture
    Guido Preite 54,057 Super User 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
    AndreasB83 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
    Guido Preite 54,057 Super User 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

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,986 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans