Skip to content

Commit b376d69

Browse files
authored
Merge pull request #3128 from MicrosoftDocs/ankarm-clientapi-retrieveMultipleRecords-expand
Doc for offline retrieveMultiple with $expand
2 parents dfb2bd0 + 531f68e commit b376d69

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

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

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ search.app:
6363
<td>successCallback</td>
6464
<td>Function</td>
6565
<td>No</td>
66-
<td><p>A function to call when entity records are retrived. An object with the following attributes is passed to the function:</p>
66+
<td><p>A function to call when entity records are retrieved. An object with the following attributes is passed to the function:</p>
6767
<ul>
6868
<li><b>entities</b>: An array of JSON objects, where each object represents the retrieved entity record containing attributes and their values as <code>key: value</code> pairs. The Id of the entity record is retrieved by default.</li>
6969
<li><b>nextLink</b>: String. If the number of records being retrieved is more than the value specified in the <code>maxPageSize</code> parameter in the request, this attribute returns the URL to return next set of records.</li>
@@ -131,7 +131,7 @@ Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name,_primarycontactid_v
131131
);
132132
```
133133

134-
#### For mobile offine scenario
134+
#### For mobile offline scenario
135135

136136
This example queries the accounts entity set and uses the `$select` and `$filter` system query options to return the name and primarycontactid property for accounts that have a particular primary contact when working in the offline mode:
137137

@@ -169,7 +169,7 @@ Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name", 3).then(
169169
);
170170
```
171171

172-
This example will display 3 records and a link to the next page. Here is an example outout from the **Console** in the browser developer tools:
172+
This example will display 3 records and a link to the next page. Here is an example output from the **Console** in the browser developer tools:
173173

174174
```
175175
{@odata.etag: "W/"1035541"", name: "A. Datum", accountid: "475b158c-541c-e511-80d3-3863bb347ba8"}
@@ -218,6 +218,7 @@ Next page link: [Organization URI]/api/data/v9.0/accounts?$select=name&$skiptoke
218218
> The value of the `nextLink` property is URI encoded. If you URI encode the value before you send it, the XML cookie information in the URL will cause an error.
219219
220220
### Retrieve related entities by expanding navigation properties
221+
#### For online scenario (connected to server)
221222

222223
Use the **$expand** system query option in the navigation properties to control the data that is returned from related entities. The following example demonstrates how to retrieve the contact for all the account records. For the related contact records, we are only retrieving the `contactid` and `fullname`:
223224

@@ -235,7 +236,77 @@ Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name&$top=3&$expand=prim
235236
}
236237
);
237238
```
239+
The above piece of code returns a result with a schema like:
240+
```JSON
241+
{
242+
"entities": [
243+
{
244+
"@odata.etag": "W/\"1459919\"",
245+
"name": "Test Account",
246+
"accountid": "119edfac-19c6-ea11-a81a-000d3af5e732",
247+
"primarycontactid": {
248+
"contactid": "6c63a1b7-19c6-ea11-a81a-000d3af5e732",
249+
"fullname": "Test Contact"
250+
}
251+
}
252+
]
253+
}
254+
```
255+
256+
#### For mobile offline scenario
257+
**$expand** for the mobile offline scenario is different from the online scenario and is a multi-part process. An offline **\$expand** operation returns a `@odata.nextLink` annotation containing information on how to get to the related record's information. We use the `id`, `entityType`, and `options` parameter of that annotation to construct one or more additional `Xrm.WebApi.offline.retrieveRecord` request(s). The following piece of code provides a complete example of how to do this:
238258

259+
```JavaScript
260+
Xrm.WebApi.offline.retrieveMultipleRecords("account", "?$select=name&$top=3&$expand=primarycontactid($select=contactid,fullname)").then(function(resultSet) {
261+
/**
262+
* resultSet has a structure like:
263+
* {
264+
* "entities": [
265+
* {
266+
* "accountid": "119edfac-19c6-ea11-a81a-000d3af5e732",
267+
* "name": "Test Account",
268+
269+
* "API": "{Xrm.Mobile.offline}.{retrieveRecord}",
270+
* "id": "119edfac-19c6-ea11-a81a-000d3af5e732",
271+
* "entityType": "account",
272+
* "options": "?$select=accountid&$expand=primarycontactid($select=contactid,fullname)&$getOnlyRelatedEntity=true"
273+
* },
274+
* "primarycontactid": {}
275+
* }
276+
* ]
277+
* }
278+
*
279+
* Notice the empty `primarycontactid` property but an additional `[email protected]`
280+
* annotation that lets us know how to get to the linked data that we need.
281+
**/
282+
283+
var promises = resultSet.entities.map(function(outerItem) {
284+
// We do a retrieveRecord() for every item in the result set of retrieveMultipleRecords() and then
285+
// combine the results into the retrieveMultipleRecords() result set itself.
286+
return Xrm.WebApi.offline.retrieveRecord(
287+
outerItem["[email protected]"].entityType,
288+
outerItem["[email protected]"].id,
289+
outerItem["[email protected]"].options
290+
).then(function(innerResult) {
291+
if (innerResult.value.length === 0) {
292+
return outerItem;
293+
}
294+
outerItem.primarycontactid = innerResult.value[0];
295+
return outerItem;
296+
});
297+
});
298+
299+
return Promise.all(promises);
300+
}).then(function(allResults) {
301+
for (var i = 0; i < allResults.length; i++) {
302+
console.log(allResults[i]);
303+
}
304+
// perform additional operations on retrieved records
305+
}, function(error) {
306+
console.error(error);
307+
// handle error conditions
308+
});
309+
```
239310

240311
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).
241312

0 commit comments

Comments
 (0)