1- /*! dynamics-web-api v1.3.3 (c) 2017 Aleksandr Rogov */
1+ /*! dynamics-web-api v1.3.4 (c) 2017 Aleksandr Rogov */
22( function webpackUniversalModuleDefinition ( root , factory ) {
33 if ( typeof exports === 'object' && typeof module === 'object' )
44 module . exports = factory ( ) ;
@@ -278,6 +278,38 @@ var ErrorHelper = {
278278 }
279279 } ,
280280
281+ /**
282+ * @param parameter {string} - parameter
283+ *
284+ */
285+ keyParameterCheck : function ( parameter , functionName , parameterName ) {
286+
287+ try {
288+ ErrorHelper . stringParameterCheck ( parameter , functionName , parameterName ) ;
289+
290+ //check if the param is a guid
291+ var match = / [ 0 - 9 A - F ] { 8 } [ - ] ? ( [ 0 - 9 A - F ] { 4 } [ - ] ? ) { 3 } [ 0 - 9 A - F ] { 12 } / i. exec ( parameter ) ;
292+ if ( match ) {
293+ return match [ 0 ] ;
294+ }
295+
296+ //check the alternate key
297+ var alternateKeys = parameter . split ( ',' ) ;
298+
299+ if ( alternateKeys . length ) {
300+ for ( var i = 0 ; i < alternateKeys . length ; i ++ ) {
301+ alternateKeys [ i ] = alternateKeys [ i ] . trim ( ) ;
302+ / ^ [ \w \d \_ ] + \= ' [ ^ \' \r \n ] + ' $ / i. exec ( alternateKeys [ i ] ) [ 0 ] ;
303+ }
304+ }
305+
306+ return alternateKeys . join ( ',' ) ;
307+ }
308+ catch ( error ) {
309+ throwParameterError ( functionName , parameterName , "String representing GUID or Alternate Key" ) ;
310+ }
311+ } ,
312+
281313 callbackParameterCheck : function ( callbackParameter , functionName , parameterName ) {
282314 ///<summary>
283315 /// Private function used to check whether required callback parameters are functions
@@ -490,12 +522,12 @@ function convertRequestOptions(request, functionName, url, joinSymbol, config) {
490522
491523 if ( request ) {
492524 if ( request . navigationProperty ) {
493- ErrorHelper . stringParameterCheck ( request . navigationProperty , " DynamicsWebApi." + functionName , "request.navigationProperty" ) ;
525+ ErrorHelper . stringParameterCheck ( request . navigationProperty , ' DynamicsWebApi.' + functionName , "request.navigationProperty" ) ;
494526 url += "/" + request . navigationProperty ;
495527 }
496528
497529 if ( request . select != null && request . select . length ) {
498- ErrorHelper . arrayParameterCheck ( request . select , " DynamicsWebApi." + functionName , "request.select" ) ;
530+ ErrorHelper . arrayParameterCheck ( request . select , ' DynamicsWebApi.' + functionName , "request.select" ) ;
499531
500532 if ( functionName == "retrieve" && request . select . length == 1 && request . select [ 0 ] . endsWith ( "/$ref" ) ) {
501533 url += "/" + request . select [ 0 ] ;
@@ -518,30 +550,30 @@ function convertRequestOptions(request, functionName, url, joinSymbol, config) {
518550 }
519551
520552 if ( request . filter ) {
521- ErrorHelper . stringParameterCheck ( request . filter , " DynamicsWebApi." + functionName , "request.filter" ) ;
553+ ErrorHelper . stringParameterCheck ( request . filter , ' DynamicsWebApi.' + functionName , "request.filter" ) ;
522554 requestArray . push ( "$filter=" + request . filter ) ;
523555 }
524556
525557 if ( request . savedQuery ) {
526- requestArray . push ( "savedQuery=" + ErrorHelper . guidParameterCheck ( request . savedQuery , " DynamicsWebApi." + functionName , "request.savedQuery" ) ) ;
558+ requestArray . push ( "savedQuery=" + ErrorHelper . guidParameterCheck ( request . savedQuery , ' DynamicsWebApi.' + functionName , "request.savedQuery" ) ) ;
527559 }
528560
529561 if ( request . userQuery ) {
530- requestArray . push ( "userQuery=" + ErrorHelper . guidParameterCheck ( request . userQuery , " DynamicsWebApi." + functionName , "request.userQuery" ) ) ;
562+ requestArray . push ( "userQuery=" + ErrorHelper . guidParameterCheck ( request . userQuery , ' DynamicsWebApi.' + functionName , "request.userQuery" ) ) ;
531563 }
532564
533565 if ( request . count ) {
534- ErrorHelper . boolParameterCheck ( request . count , " DynamicsWebApi." + functionName , "request.count" ) ;
566+ ErrorHelper . boolParameterCheck ( request . count , ' DynamicsWebApi.' + functionName , "request.count" ) ;
535567 requestArray . push ( "$count=" + request . count ) ;
536568 }
537569
538570 if ( request . top && request . top > 0 ) {
539- ErrorHelper . numberParameterCheck ( request . top , " DynamicsWebApi." + functionName , "request.top" ) ;
571+ ErrorHelper . numberParameterCheck ( request . top , ' DynamicsWebApi.' + functionName , "request.top" ) ;
540572 requestArray . push ( "$top=" + request . top ) ;
541573 }
542574
543575 if ( request . orderBy != null && request . orderBy . length ) {
544- ErrorHelper . arrayParameterCheck ( request . orderBy , " DynamicsWebApi." + functionName , "request.orderBy" ) ;
576+ ErrorHelper . arrayParameterCheck ( request . orderBy , ' DynamicsWebApi.' + functionName , "request.orderBy" ) ;
545577 requestArray . push ( "$orderby=" + request . orderBy . join ( ',' ) ) ;
546578 }
547579
@@ -552,33 +584,38 @@ function convertRequestOptions(request, functionName, url, joinSymbol, config) {
552584 }
553585
554586 if ( request . ifmatch != null && request . ifnonematch != null ) {
555- throw new Error ( " DynamicsWebApi." + functionName + ". Either one of request.ifmatch or request.ifnonematch parameters should be used in a call, not both." )
587+ throw new Error ( ' DynamicsWebApi.' + functionName + ". Either one of request.ifmatch or request.ifnonematch parameters should be used in a call, not both." )
556588 }
557589
558590 if ( request . ifmatch ) {
559- ErrorHelper . stringParameterCheck ( request . ifmatch , " DynamicsWebApi." + functionName , "request.ifmatch" ) ;
591+ ErrorHelper . stringParameterCheck ( request . ifmatch , ' DynamicsWebApi.' + functionName , "request.ifmatch" ) ;
560592 headers [ 'If-Match' ] = request . ifmatch ;
561593 }
562594
563595 if ( request . ifnonematch ) {
564- ErrorHelper . stringParameterCheck ( request . ifnonematch , " DynamicsWebApi." + functionName , "request.ifnonematch" ) ;
596+ ErrorHelper . stringParameterCheck ( request . ifnonematch , ' DynamicsWebApi.' + functionName , "request.ifnonematch" ) ;
565597 headers [ 'If-None-Match' ] = request . ifnonematch ;
566598 }
567599
568600 if ( request . impersonate ) {
569- ErrorHelper . stringParameterCheck ( request . impersonate , " DynamicsWebApi." + functionName , "request.impersonate" ) ;
570- headers [ 'MSCRMCallerID' ] = ErrorHelper . guidParameterCheck ( request . impersonate , " DynamicsWebApi." + functionName , "request.impersonate" ) ;
601+ ErrorHelper . stringParameterCheck ( request . impersonate , ' DynamicsWebApi.' + functionName , "request.impersonate" ) ;
602+ headers [ 'MSCRMCallerID' ] = ErrorHelper . guidParameterCheck ( request . impersonate , ' DynamicsWebApi.' + functionName , "request.impersonate" ) ;
571603 }
572604
573605 if ( request . token ) {
574- ErrorHelper . stringParameterCheck ( request . token , "DynamicsWebApi." + functionName , "request.token" ) ;
575- headers [ "Authorization" ] = "Bearer " + request . token ;
606+ ErrorHelper . stringParameterCheck ( request . token , 'DynamicsWebApi.' + functionName , "request.token" ) ;
607+ headers [ 'Authorization' ] = 'Bearer ' + request . token ;
608+ }
609+
610+ if ( request . duplicateDetection ) {
611+ ErrorHelper . boolParameterCheck ( request . duplicateDetection , 'DynamicsWebApi.' + functionName , 'request.duplicateDetection' ) ;
612+ headers [ 'MSCRM.SuppressDuplicateDetection' ] = 'false' ;
576613 }
577614
578615 if ( request . expand && request . expand . length ) {
579- ErrorHelper . stringOrArrayParameterCheck ( request . expand , " DynamicsWebApi." + functionName , "request.expand" ) ;
580- if ( typeof request . expand === " string" ) {
581- requestArray . push ( " $expand=" + request . expand ) ;
616+ ErrorHelper . stringOrArrayParameterCheck ( request . expand , ' DynamicsWebApi.' + functionName , "request.expand" ) ;
617+ if ( typeof request . expand === ' string' ) {
618+ requestArray . push ( ' $expand=' + request . expand ) ;
582619 }
583620 else {
584621 var expandRequestArray = [ ] ;
@@ -624,17 +661,24 @@ function getCollectionName(collectionName) {
624661function convertRequest ( request , functionName , config ) {
625662
626663 if ( ! request . collection ) {
627- ErrorHelper . parameterCheck ( request . collection , " DynamicsWebApi." + functionName , "request.collection" ) ;
664+ ErrorHelper . parameterCheck ( request . collection , ' DynamicsWebApi.' + functionName , "request.collection" ) ;
628665 }
629666 else {
630- ErrorHelper . stringParameterCheck ( request . collection , " DynamicsWebApi." + functionName , "request.collection" ) ;
667+ ErrorHelper . stringParameterCheck ( request . collection , ' DynamicsWebApi.' + functionName , "request.collection" ) ;
631668 }
632669
633670 var url = getCollectionName ( request . collection ) ;
634671
635- if ( request . id ) {
636- request . id = ErrorHelper . guidParameterCheck ( request . id , "DynamicsWebApi." + functionName , "request.id" ) ;
637- url += "(" + request . id + ")" ;
672+ //add alternate key feature
673+ if ( request . key ) {
674+ request . key = ErrorHelper . keyParameterCheck ( request . key , 'DynamicsWebApi.' + functionName , "request.key" ) ;
675+ }
676+ else if ( request . id ) {
677+ request . key = ErrorHelper . guidParameterCheck ( request . id , 'DynamicsWebApi.' + functionName , "request.id" ) ;
678+ }
679+
680+ if ( request . key ) {
681+ url += "(" + request . key + ")" ;
638682 }
639683
640684 var result = convertRequestOptions ( request , functionName , url , '&' , config ) ;
@@ -644,7 +688,7 @@ function convertRequest(request, functionName, config) {
644688 }
645689
646690 if ( request . hasOwnProperty ( 'async' ) && request . async != null ) {
647- ErrorHelper . boolParameterCheck ( request . async , " DynamicsWebApi." + functionName , "request.async" ) ;
691+ ErrorHelper . boolParameterCheck ( request . async , ' DynamicsWebApi.' + functionName , "request.async" ) ;
648692 result . async = request . async ;
649693 }
650694 else {
@@ -932,16 +976,16 @@ function DynamicsWebApi(config) {
932976 /**
933977 * Sends an asynchronous request to retrieve a record.
934978 *
935- * @param {string } id - A String representing the GUID value for the record to retrieve.
979+ * @param {string } key - A String representing the GUID value or Aternate Key for the record to retrieve.
936980 * @param {string } collection - The Name of the Entity Collection.
937981 * @param {Array } [select] - An Array representing the $select Query Option to control which attributes will be returned.
938982 * @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.
939983 * @returns {Promise }
940984 */
941- this . retrieve = function ( id , collection , select , expand ) {
985+ this . retrieve = function ( key , collection , select , expand ) {
942986
943- ErrorHelper . stringParameterCheck ( id , "DynamicsWebApi.retrieve" , "id " ) ;
944- id = ErrorHelper . guidParameterCheck ( id , "DynamicsWebApi.retrieve" , "id " )
987+ ErrorHelper . stringParameterCheck ( key , "DynamicsWebApi.retrieve" , "key " ) ;
988+ key = ErrorHelper . keyParameterCheck ( key , "DynamicsWebApi.retrieve" , "key " )
945989 ErrorHelper . stringParameterCheck ( collection , "DynamicsWebApi.retrieve" , "collection" ) ;
946990
947991 if ( select && select . length ) {
@@ -954,7 +998,7 @@ function DynamicsWebApi(config) {
954998
955999 var request = {
9561000 collection : collection ,
957- id : id ,
1001+ key : key ,
9581002 select : select ,
9591003 expand : expand
9601004 } ;
@@ -1002,17 +1046,17 @@ function DynamicsWebApi(config) {
10021046 /**
10031047 * Sends an asynchronous request to update a record.
10041048 *
1005- * @param {string } id - A String representing the GUID value for the record to update.
1049+ * @param {string } key - A String representing the GUID value or Alternate Key for the record to update.
10061050 * @param {string } collection - The Name of the Entity Collection.
10071051 * @param {Object } object - A JavaScript object valid for update operations.
10081052 * @param {string } [prefer] - If set to "return=representation" the function will return an updated object
10091053 * @param {Array } [select] - An Array representing the $select Query Option to control which attributes will be returned.
10101054 * @returns {Promise }
10111055 */
1012- this . update = function ( id , collection , object , prefer , select ) {
1056+ this . update = function ( key , collection , object , prefer , select ) {
10131057
1014- ErrorHelper . stringParameterCheck ( id , "DynamicsWebApi.update" , "id " ) ;
1015- id = ErrorHelper . guidParameterCheck ( id , "DynamicsWebApi.update" , "id " )
1058+ ErrorHelper . stringParameterCheck ( key , "DynamicsWebApi.update" , "key " ) ;
1059+ key = ErrorHelper . keyParameterCheck ( key , "DynamicsWebApi.update" , "key " )
10161060 ErrorHelper . parameterCheck ( object , "DynamicsWebApi.update" , "object" ) ;
10171061 ErrorHelper . stringParameterCheck ( collection , "DynamicsWebApi.update" , "collection" ) ;
10181062
@@ -1026,7 +1070,7 @@ function DynamicsWebApi(config) {
10261070
10271071 var request = {
10281072 collection : collection ,
1029- id : id ,
1073+ key : key ,
10301074 select : select ,
10311075 prefer : prefer ,
10321076 entity : object
@@ -1038,22 +1082,22 @@ function DynamicsWebApi(config) {
10381082 /**
10391083 * Sends an asynchronous request to update a single value in the record.
10401084 *
1041- * @param {string } id - A String representing the GUID value for the record to update.
1085+ * @param {string } key - A String representing the GUID value or Alternate Key for the record to update.
10421086 * @param {string } collection - The Name of the Entity Collection.
10431087 * @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"}
10441088 * @param {string|Array } [prefer] - If set to "return=representation" the function will return an updated object
10451089 * @param {Array } [select] - An Array representing the $select Query Option to control which attributes will be returned.
10461090 * @returns {Promise }
10471091 */
1048- this . updateSingleProperty = function ( id , collection , keyValuePair , prefer , select ) {
1092+ this . updateSingleProperty = function ( key , collection , keyValuePair , prefer , select ) {
10491093
1050- ErrorHelper . stringParameterCheck ( id , "DynamicsWebApi.updateSingleProperty" , "id " ) ;
1051- id = ErrorHelper . guidParameterCheck ( id , "DynamicsWebApi.updateSingleProperty" , "id " )
1094+ ErrorHelper . stringParameterCheck ( key , "DynamicsWebApi.updateSingleProperty" , "key " ) ;
1095+ key = ErrorHelper . keyParameterCheck ( key , "DynamicsWebApi.updateSingleProperty" , "key " )
10521096 ErrorHelper . parameterCheck ( keyValuePair , "DynamicsWebApi.updateSingleProperty" , "keyValuePair" ) ;
10531097 ErrorHelper . stringParameterCheck ( collection , "DynamicsWebApi.updateSingleProperty" , "collection" ) ;
10541098
1055- var key = Object . keys ( keyValuePair ) [ 0 ] ;
1056- var keyValue = keyValuePair [ key ] ;
1099+ var field = Object . keys ( keyValuePair ) [ 0 ] ;
1100+ var fieldValue = keyValuePair [ field ] ;
10571101
10581102 if ( prefer ) {
10591103 ErrorHelper . stringOrArrayParameterCheck ( prefer , "DynamicsWebApi.updateSingleProperty" , "prefer" ) ;
@@ -1065,15 +1109,15 @@ function DynamicsWebApi(config) {
10651109
10661110 var request = {
10671111 collection : collection ,
1068- id : id ,
1112+ key : key ,
10691113 select : select ,
10701114 prefer : prefer ,
1071- navigationProperty : key
1115+ navigationProperty : field
10721116 } ;
10731117
10741118 var result = RequestConverter . convertRequest ( request , "updateSingleProperty" , _internalConfig ) ;
10751119
1076- return _sendRequest ( "PUT" , result . url , { value : keyValue } , result . headers , result . async )
1120+ return _sendRequest ( "PUT" , result . url , { value : fieldValue } , result . headers , result . async )
10771121 . then ( function ( response ) {
10781122 if ( response . data ) {
10791123 return response . data ;
@@ -1112,21 +1156,21 @@ function DynamicsWebApi(config) {
11121156 /**
11131157 * Sends an asynchronous request to delete a record.
11141158 *
1115- * @param {string } id - A String representing the GUID value for the record to delete.
1159+ * @param {string } key - A String representing the GUID value or Alternate Key for the record to delete.
11161160 * @param {string } collection - The Name of the Entity Collection.
11171161 * @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.
11181162 * @returns {Promise }
11191163 */
1120- this . deleteRecord = function ( id , collection , propertyName ) {
1164+ this . deleteRecord = function ( key , collection , propertyName ) {
11211165
1122- ErrorHelper . stringParameterCheck ( id , "DynamicsWebApi.deleteRecord" , "id " ) ;
1123- id = ErrorHelper . guidParameterCheck ( id , "DynamicsWebApi.deleteRecord" , "id " )
1166+ ErrorHelper . stringParameterCheck ( key , "DynamicsWebApi.deleteRecord" , "key " ) ;
1167+ key = ErrorHelper . keyParameterCheck ( key , "DynamicsWebApi.deleteRecord" , "key " )
11241168 ErrorHelper . stringParameterCheck ( collection , "DynamicsWebApi.deleteRecord" , "collection" ) ;
11251169
11261170 if ( propertyName != null )
11271171 ErrorHelper . stringParameterCheck ( propertyName , "DynamicsWebApi.deleteRecord" , "propertyName" ) ;
11281172
1129- var url = collection . toLowerCase ( ) + "(" + id + ")" ;
1173+ var url = collection . toLowerCase ( ) + "(" + key + ")" ;
11301174
11311175 if ( propertyName != null )
11321176 url += "/" + propertyName ;
@@ -1181,17 +1225,17 @@ function DynamicsWebApi(config) {
11811225 /**
11821226 * Sends an asynchronous request to upsert a record.
11831227 *
1184- * @param {string } id - A String representing the GUID value for the record to upsert.
1228+ * @param {string } key - A String representing the GUID value or Alternate Key for the record to upsert.
11851229 * @param {string } collection - The Name of the Entity Collection.
11861230 * @param {Object } object - A JavaScript object valid for update operations.
11871231 * @param {string|Array } [prefer] - If set to "return=representation" the function will return an updated object
11881232 * @param {Array } [select] - An Array representing the $select Query Option to control which attributes will be returned.
11891233 * @returns {Promise }
11901234 */
1191- this . upsert = function ( id , collection , object , prefer , select ) {
1235+ this . upsert = function ( key , collection , object , prefer , select ) {
11921236
1193- ErrorHelper . stringParameterCheck ( id , "DynamicsWebApi.upsert" , "id " ) ;
1194- id = ErrorHelper . guidParameterCheck ( id , "DynamicsWebApi.upsert" , "id " )
1237+ ErrorHelper . stringParameterCheck ( key , "DynamicsWebApi.upsert" , "key " ) ;
1238+ key = ErrorHelper . keyParameterCheck ( key , "DynamicsWebApi.upsert" , "key " )
11951239
11961240 ErrorHelper . parameterCheck ( object , "DynamicsWebApi.upsert" , "object" ) ;
11971241 ErrorHelper . stringParameterCheck ( collection , "DynamicsWebApi.upsert" , "collection" ) ;
@@ -1206,7 +1250,7 @@ function DynamicsWebApi(config) {
12061250
12071251 var request = {
12081252 collection : collection ,
1209- id : id ,
1253+ key : key ,
12101254 select : select ,
12111255 prefer : prefer ,
12121256 entity : object
0 commit comments