Skip to content

Commit 66dfd7d

Browse files
committed
Doc for offline retrieveMultiple with $expand
1 parent d0bb134 commit 66dfd7d

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

powerapps-docs/developer/model-driven-apps/clientapi/reference/Xrm-WebApi/retrieveMultipleRecords.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,91 @@ Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name&$top=3&$expand=prim
235235
}
236236
);
237237
```
238+
The above piece of code returns a result with a schema like:
239+
```JSON
240+
{
241+
"entities": [
242+
{
243+
"@odata.etag": "W/\"1459919\"",
244+
"name": "Test Account",
245+
"accountid": "119edfac-19c6-ea11-a81a-000d3af5e732",
246+
"primarycontactid": {
247+
"contactid": "6c63a1b7-19c6-ea11-a81a-000d3af5e732",
248+
"fullname": "Test Contact"
249+
}
250+
}
251+
]
252+
}
253+
```
254+
255+
#### For mobile offine scenario
256+
When doing an **$expand** operation while working offline, the structure of the result object is different from the online scenario and a different approach is needed to get to the linked data in this case. The following example demonstrates this:
238257

258+
```JavaScript
259+
Xrm.WebApi.offline.retrieveMultipleRecords("account", "?$select=name&$top=3&$expand=primarycontactid($select=contactid,fullname)", 3).then(
260+
function success(result) {
261+
for (var i = 0; i < result.entities.length; i++) {
262+
console.log(result.entities[i]);
263+
}
264+
// perform additional operations on retrieved records
265+
},
266+
function (error) {
267+
console.log(error.message);
268+
// handle error conditions
269+
}
270+
);
271+
```
272+
273+
The above piece of code returns a result with a schema like:
274+
```JSON
275+
{
276+
"entities": [
277+
{
278+
"accountid": "119edfac-19c6-ea11-a81a-000d3af5e732",
279+
"name": "Test Account",
280+
281+
"API": "{Xrm.Mobile.offline}.{retrieveRecord}",
282+
"id": "119edfac-19c6-ea11-a81a-000d3af5e732",
283+
"entityType": "account",
284+
"options": "?$select=accountid&$expand=primarycontactid($select=contactid,fullname)&$getOnlyRelatedEntity=true"
285+
},
286+
"primarycontactid": {}
287+
}
288+
]
289+
}
290+
```
291+
292+
Notice the empty `primarycontactid` property but an additional `[email protected]` property that let's us know how to get to the linked data that we need. Use the `id`, `entityType`, and `options` parameter of that property to construct an inner `Xrm.WebApi.offline.retrieveRecord` request. The following piece of code provides a complete example of how to do this:
293+
294+
```JavaScript
295+
Xrm.WebApi.offline.retrieveMultipleRecords("account", "?$select=name&$top=3&$expand=primarycontactid($select=contactid,fullname)").then(function(resultSet) {
296+
var promises = resultSet.entities.map(function(outerItem) {
297+
// We do a retrieveRecord for every item in the resultSet of retrieveMultipleRecords() and then
298+
// hydrate the retrieveMultipleRecords resultSet itself.
299+
return Xrm.WebApi.offline.retrieveRecord(
300+
outerItem["[email protected]"].entityType,
301+
outerItem["[email protected]"].id,
302+
outerItem["[email protected]"].options
303+
).then(function(innerResult) {
304+
if(innerResult.value.length === 0) {
305+
return outerItem;
306+
}
307+
outerItem.primarycontactid = innerResult.value[0];
308+
return outerItem;
309+
});
310+
});
311+
312+
return Promise.all(promises);
313+
}).then(function(allResults) {
314+
for (var i = 0; i < allResults.length; i++) {
315+
console.log(allResults[i]);
316+
}
317+
// perform additional operations on retrieved records
318+
}, function(error) {
319+
console.error(error);
320+
// handle error conditions
321+
});
322+
```
239323

240324
For more examples of retrieving multiple records using Web API, see [Query Data using the Web API](../../../../common-data-service/webapi/query-data-web-api.md).
241325

0 commit comments

Comments
 (0)