Skip to content

Commit 8ba10f3

Browse files
add: crud operations using alternate keys #14; duplicate detection
1 parent a5c1535 commit 8ba10f3

File tree

8 files changed

+262
-84
lines changed

8 files changed

+262
-84
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,16 @@ Property Name | Type | Operation(s) Supported | Description
181181
async | Boolean | All | **Important! XHR requests only!** Indicates whether the requests should be made synchronously or asynchronously. Default value is `true` (asynchronously).
182182
collection | String | All | The name of the Entity Collection, for example, for `account` use `accounts`, `opportunity` - `opportunities` and etc.
183183
count | Boolean | `retrieveMultipleRequest`, `retrieveAllRequest` | Boolean that sets the $count system query option with a value of true to include a count of entities that match the filter criteria up to 5000 (per page). Do not use $top with $count!
184+
duplicateDetection | Boolean | `createRequest`, `updateRequest`, `upsertRequest` | `v.1.3.4+` ** Web API v9+ only!** Boolean that enables duplicate detection. [More info](https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/update-delete-entities-using-web-api#check-for-duplicate-records)
184185
entity | Object | `updateRequest`, `upsertRequest` | A JavaScript object with properties corresponding to the logical name of entity attributes (exceptions are lookups and single-valued navigation properties).
185186
expand | Array | `retrieveRequest`, `updateRequest`, `upsertRequest` | An array of Expand Objects (described below the table) representing the $expand OData System Query Option value to control which related records are also returned.
186187
filter | String | `retrieveRequest`, `retrieveMultipleRequest`, `retrieveAllRequest` | Use the $filter system query option to set criteria for which entities will be returned.
187-
id | String | `retrieveRequest`, `updateRequest`, `upsertRequest`, `deleteRequest` | A String representing the GUID value for the record.
188+
id | String | `retrieveRequest`, `updateRequest`, `upsertRequest`, `deleteRequest` | `deprecated in v.1.3.4` Use `key` field, instead of `id`. A String representing the GUID value for the record.
188189
ifmatch | String | `retrieveRequest`, `updateRequest`, `upsertRequest`, `deleteRequest` | Sets If-Match header value that enables to use conditional retrieval or optimistic concurrency in applicable requests. [More info](https://msdn.microsoft.com/en-us/library/mt607711.aspx).
189190
ifnonematch | String | `retrieveRequest`, `upsertRequest` | Sets If-None-Match header value that enables to use conditional retrieval in applicable requests. [More info](https://msdn.microsoft.com/en-us/library/mt607711.aspx).
190191
impersonate | String | All | A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
191192
includeAnnotations | String | `retrieveRequest`, `retrieveMultipleRequest`, `retrieveAllRequest`, `updateRequest`, `upsertRequest` | Sets Prefer header with value "odata.include-annotations=" and the specified annotation. Annotations provide additional information about lookups, options sets and other complex attribute types.
193+
key | String | `retrieveRequest`, `updateRequest`, `upsertRequest`, `deleteRequest` | `v.1.3.4+` A String representing collection record's Primary Key (GUID) or Alternate Key(s).
192194
maxPageSize | Number | `retrieveMultipleRequest`, `retrieveAllRequest` | Sets the odata.maxpagesize preference value to request the number of entities returned in the response.
193195
navigationProperty | String | `retrieveRequest` | A String representing the name of a single-valued navigation property. Useful when needed to retrieve information about a related record in a single request.
194196
orderBy | Array | `retrieveMultipleRequest`, `retrieveAllRequest` | An Array (of Strings) representing the order in which items are returned using the $orderby system query option. Use the asc or desc suffix to specify ascending or descending order respectively. The default is ascending if the suffix isn't applied.
@@ -199,7 +201,7 @@ token | String | All | Authorization Token. If set, onTokenRefresh will not be c
199201
top | Number | `retrieveMultipleRequest`, `retrieveAllRequest` | Limit the number of results returned by using the $top system query option. Do not use $top with $count!
200202
userQuery | String | `retrieveRequest` | A String representing the GUID value of the user query.
201203

202-
Basic and Advanced functions are also have differences in `expand` parameters. For Basic ones this parameter is a type of String
204+
Basic and Advanced functions also have differences in `expand` parameters. For Basic ones this parameter is a type of String
203205
while request.expand property is an Array of Expand Objects for Advanced operations. The following table describes Expand Object properties:
204206

205207
Property Name | Type | Description

lib/dynamics-web-api-callbacks.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,18 @@ function DynamicsWebApi(config) {
267267
/**
268268
* Sends an asynchronous request to update a record.
269269
*
270-
* @param {string} id - A String representing the GUID value for the record to update.
270+
* @param {string} key - A String representing the GUID value or Alternate Key(s) for the record to update.
271271
* @param {string} collection - The Name of the Entity Collection.
272272
* @param {Object} object - A JavaScript object valid for update operations.
273273
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
274274
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
275275
* @param {string|Array} [prefer] - If set to "return=representation" the function will return an updated object
276276
* @param {Array} [select] - An Array representing the $select Query Option to control which attributes will be returned.
277277
*/
278-
this.update = function (id, collection, object, successCallback, errorCallback, prefer, select) {
278+
this.update = function (key, collection, object, successCallback, errorCallback, prefer, select) {
279279

280-
ErrorHelper.stringParameterCheck(id, "DynamicsWebApi.update", "id");
281-
id = ErrorHelper.guidParameterCheck(id, "DynamicsWebApi.update", "id")
280+
ErrorHelper.stringParameterCheck(key, "DynamicsWebApi.update", "key");
281+
key = ErrorHelper.keyParameterCheck(key, "DynamicsWebApi.update", "key")
282282
ErrorHelper.parameterCheck(object, "DynamicsWebApi.update", "object");
283283
ErrorHelper.stringParameterCheck(collection, "DynamicsWebApi.update", "collection");
284284
ErrorHelper.callbackParameterCheck(successCallback, "DynamicsWebApi.update", "successCallback");
@@ -294,7 +294,7 @@ function DynamicsWebApi(config) {
294294

295295
var request = {
296296
collection: collection,
297-
id: id,
297+
key: key,
298298
select: select,
299299
prefer: prefer,
300300
entity: object
@@ -306,18 +306,18 @@ function DynamicsWebApi(config) {
306306
/**
307307
* Sends an asynchronous request to update a single value in the record.
308308
*
309-
* @param {string} id - A String representing the GUID value for the record to update.
309+
* @param {string} key - A String representing the GUID value or Alternate Key(s) for the record to update.
310310
* @param {string} collection - The Name of the Entity Collection.
311311
* @param {Object} keyValuePair - keyValuePair object with a logical name of the field as a key and a value to update with. Example: {subject: "Update Record"}
312312
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
313313
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
314314
* @param {string|Array} [prefer] - If set to "return=representation" the function will return an updated object
315315
* @param {Array} [select] - An Array representing the $select Query Option to control which attributes will be returned.
316316
*/
317-
this.updateSingleProperty = function (id, collection, keyValuePair, successCallback, errorCallback, prefer, select) {
317+
this.updateSingleProperty = function (key, collection, keyValuePair, successCallback, errorCallback, prefer, select) {
318318

319-
ErrorHelper.stringParameterCheck(id, "DynamicsWebApi.updateSingleProperty", "id");
320-
id = ErrorHelper.guidParameterCheck(id, "DynamicsWebApi.updateSingleProperty", "id");
319+
ErrorHelper.stringParameterCheck(key, "DynamicsWebApi.updateSingleProperty", "key");
320+
key = ErrorHelper.keyParameterCheck(key, "DynamicsWebApi.updateSingleProperty", "key");
321321
ErrorHelper.parameterCheck(keyValuePair, "DynamicsWebApi.updateSingleProperty", "keyValuePair");
322322
ErrorHelper.stringParameterCheck(collection, "DynamicsWebApi.updateSingleProperty", "collection");
323323
ErrorHelper.callbackParameterCheck(successCallback, "DynamicsWebApi.updateSingleProperty", "successCallback");
@@ -331,15 +331,15 @@ function DynamicsWebApi(config) {
331331
ErrorHelper.arrayParameterCheck(select, "DynamicsWebApi.updateSingleProperty", "select");
332332
}
333333

334-
var key = Object.keys(keyValuePair)[0];
335-
var keyValue = keyValuePair[key];
334+
var field = Object.keys(keyValuePair)[0];
335+
var fieldValue = keyValuePair[field];
336336

337337
var request = {
338338
collection: collection,
339-
id: id,
339+
key: key,
340340
select: select,
341341
prefer: prefer,
342-
navigationProperty: key
342+
navigationProperty: field
343343
};
344344

345345
var result = RequestConverter.convertRequest(request, "updateSingleProperty", _internalConfig);
@@ -350,7 +350,7 @@ function DynamicsWebApi(config) {
350350
: successCallback();
351351
};
352352

353-
_sendRequest("PUT", result.url, { value: keyValue }, result.headers, onSuccess, errorCallback, result.async);
353+
_sendRequest("PUT", result.url, { value: fieldValue }, result.headers, onSuccess, errorCallback, result.async);
354354
};
355355

356356
/**
@@ -391,24 +391,24 @@ function DynamicsWebApi(config) {
391391
/**
392392
* Sends an asynchronous request to delete a record.
393393
*
394-
* @param {string} id - A String representing the GUID value for the record to delete.
394+
* @param {string} key - A String representing the GUID value or Alternate Key(s) for the record to delete.
395395
* @param {string} collection - The Name of the Entity Collection.
396396
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
397397
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
398398
* @param {string} [propertyName] - The name of the property which needs to be emptied. Instead of removing a whole record only the specified property will be cleared.
399399
*/
400-
this.deleteRecord = function (id, collection, successCallback, errorCallback, propertyName) {
400+
this.deleteRecord = function (key, collection, successCallback, errorCallback, propertyName) {
401401

402-
ErrorHelper.stringParameterCheck(id, "DynamicsWebApi.delete", "id");
403-
id = ErrorHelper.guidParameterCheck(id, "DynamicsWebApi.delete", "id");
402+
ErrorHelper.stringParameterCheck(key, "DynamicsWebApi.delete", "key");
403+
key = ErrorHelper.keyParameterCheck(key, "DynamicsWebApi.delete", "key");
404404
ErrorHelper.stringParameterCheck(collection, "DynamicsWebApi.delete", "collection");
405405
ErrorHelper.callbackParameterCheck(successCallback, "DynamicsWebApi.delete", "successCallback");
406406
ErrorHelper.callbackParameterCheck(errorCallback, "DynamicsWebApi.delete", "errorCallback");
407407

408408
if (propertyName != null)
409409
ErrorHelper.stringParameterCheck(propertyName, "DynamicsWebApi.delete", "propertyName");
410410

411-
var url = collection.toLowerCase() + "(" + id + ")";
411+
var url = collection.toLowerCase() + "(" + key + ")";
412412

413413
if (propertyName != null)
414414
url += "/" + propertyName;
@@ -453,17 +453,17 @@ function DynamicsWebApi(config) {
453453
/**
454454
* Sends an asynchronous request to retrieve a record.
455455
*
456-
* @param {string} id - A String representing the GUID value for the record to retrieve.
456+
* @param {string} key - A String representing the GUID value or Alternate Key(s) for the record to retrieve.
457457
* @param {string} collection - The Name of the Entity Collection.
458458
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
459459
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
460460
* @param {Array} [select] - An Array representing the $select Query Option to control which attributes will be returned.
461461
* @param {string|Array} [expand] - A String or Array of Expand Objects representing the $expand Query Option value to control which related records need to be returned.
462462
*/
463-
this.retrieve = function (id, collection, successCallback, errorCallback, select, expand) {
463+
this.retrieve = function (key, collection, successCallback, errorCallback, select, expand) {
464464

465-
ErrorHelper.stringParameterCheck(id, "DynamicsWebApi.retrieve", "id");
466-
id = ErrorHelper.guidParameterCheck(id, "DynamicsWebApi.retrieve", "id")
465+
ErrorHelper.stringParameterCheck(key, "DynamicsWebApi.retrieve", "key");
466+
key = ErrorHelper.keyParameterCheck(key, "DynamicsWebApi.retrieve", "key")
467467
ErrorHelper.stringParameterCheck(collection, "DynamicsWebApi.retrieve", "collection");
468468
ErrorHelper.callbackParameterCheck(successCallback, "DynamicsWebApi.retrieve", "successCallback");
469469
ErrorHelper.callbackParameterCheck(errorCallback, "DynamicsWebApi.retrieve", "errorCallback");
@@ -478,7 +478,7 @@ function DynamicsWebApi(config) {
478478

479479
var request = {
480480
collection: collection,
481-
id: id,
481+
key: key,
482482
select: select,
483483
expand: expand
484484
};
@@ -542,18 +542,18 @@ function DynamicsWebApi(config) {
542542
/**
543543
* Sends an asynchronous request to upsert a record.
544544
*
545-
* @param {string} id - A String representing the GUID value for the record to upsert.
545+
* @param {string} key - A String representing the GUID value or Alternate Key(s) for the record to upsert.
546546
* @param {string} collection - The Name of the Entity Collection.
547547
* @param {Object} object - A JavaScript object valid for update operations.
548548
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
549549
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
550550
* @param {string|Array} [prefer] - If set to "return=representation" the function will return an updated object
551551
* @param {Array} [select] - An Array representing the $select Query Option to control which attributes will be returned.
552552
*/
553-
this.upsert = function (id, collection, object, successCallback, errorCallback, prefer, select) {
553+
this.upsert = function (key, collection, object, successCallback, errorCallback, prefer, select) {
554554

555-
ErrorHelper.stringParameterCheck(id, "DynamicsWebApi.upsert", "id");
556-
id = ErrorHelper.guidParameterCheck(id, "DynamicsWebApi.upsert", "id")
555+
ErrorHelper.stringParameterCheck(key, "DynamicsWebApi.upsert", "key");
556+
key = ErrorHelper.keyParameterCheck(key, "DynamicsWebApi.upsert", "key")
557557

558558
ErrorHelper.parameterCheck(object, "DynamicsWebApi.upsert", "object");
559559
ErrorHelper.stringParameterCheck(collection, "DynamicsWebApi.upsert", "collection");
@@ -571,7 +571,7 @@ function DynamicsWebApi(config) {
571571

572572
var request = {
573573
collection: collection,
574-
id: id,
574+
key: key,
575575
select: select,
576576
prefer: prefer,
577577
entity: object

0 commit comments

Comments
 (0)