1- /*! dynamics-web-api-callbacks v1.5.12 (c) 2019 Aleksandr Rogov */
1+ /*! dynamics-web-api-callbacks v1.5.13 (c) 2019 Aleksandr Rogov */
22( function webpackUniversalModuleDefinition ( root , factory ) {
33 if ( typeof exports === 'object' && typeof module === 'object' )
44 module . exports = factory ( ) ;
@@ -168,13 +168,13 @@ function generateUUID() { // Public Domain/MIT
168168}
169169
170170function getXrmContext ( ) {
171- if ( typeof GetGlobalContext != 'undefined' ) {
171+ if ( typeof GetGlobalContext !== 'undefined' ) {
172172 return GetGlobalContext ( ) ;
173173 }
174174 else {
175- if ( typeof Xrm != 'undefined' ) {
175+ if ( typeof Xrm !== 'undefined' ) {
176176 //d365 v.9.0
177- if ( ( ! isNull ( Xrm . Utility ) && ! isNull ( Xrm . Utility . getGlobalContext ) ) ) {
177+ if ( ! isNull ( Xrm . Utility ) && ! isNull ( Xrm . Utility . getGlobalContext ) ) {
178178 return Xrm . Utility . getGlobalContext ( ) ;
179179 }
180180 else if ( ! isNull ( Xrm . Page ) && ! isNull ( Xrm . Page . context ) ) {
@@ -203,11 +203,11 @@ function initWebApiUrl(version) {
203203
204204function getXrmInternal ( ) {
205205 //todo: Xrm.Internal namespace is not supported
206- if ( typeof Xrm !== 'undefined' ) {
207- return Xrm . Internal ;
208- }
206+ return typeof Xrm !== "undefined" ? Xrm . Internal : null ;
207+ }
209208
210- return null ;
209+ function getXrmUtility ( ) {
210+ return typeof Xrm !== "undefined" ? Xrm . Utility : null ;
211211}
212212
213213var Utility = {
@@ -249,6 +249,8 @@ var Utility = {
249249
250250 getXrmInternal : getXrmInternal ,
251251
252+ getXrmUtility : getXrmUtility ,
253+
252254 getClientUrl : getClientUrl ,
253255
254256 initWebApiUrl : initWebApiUrl
@@ -775,7 +777,8 @@ var _entityNames;
775777 */
776778function findCollectionName ( entityName ) {
777779 var xrmInternal = Utility . getXrmInternal ( ) ;
778- if ( ! Utility . isNull ( xrmInternal ) ) {
780+
781+ if ( ! Utility . isNull ( xrmInternal ) && typeof xrmInternal . getEntitySetName === "function" ) {
779782 return xrmInternal . getEntitySetName ( entityName ) || entityName ;
780783 }
781784
@@ -785,7 +788,7 @@ function findCollectionName(entityName) {
785788 collectionName = _entityNames [ entityName ] ;
786789 if ( Utility . isNull ( collectionName ) ) {
787790 for ( var key in _entityNames ) {
788- if ( _entityNames [ key ] == entityName ) {
791+ if ( _entityNames [ key ] === entityName ) {
789792 return entityName ;
790793 }
791794 }
@@ -989,26 +992,37 @@ function sendRequest(method, path, config, data, additionalHeaders, responsePara
989992
990993function _getEntityNames ( entityName , config , successCallback , errorCallback ) {
991994
992- var resolve = function ( result ) {
993- _entityNames = { } ;
994- for ( var i = 0 ; i < result . data . value . length ; i ++ ) {
995- _entityNames [ result . data . value [ i ] . LogicalName ] = result . data . value [ i ] . LogicalCollectionName ;
996- }
995+ var xrmUtility = Utility . getXrmUtility ( ) ;
997996
998- successCallback ( findCollectionName ( entityName ) ) ;
999- } ;
997+ //try using Xrm.Utility.getEntityMetadata first (because D365 caches metadata)
998+ if ( ! Utility . isNull ( xrmUtility ) && typeof xrmUtility . getEntityMetadata === "function" ) {
999+ xrmUtility . getEntityMetadata ( entityName ) . then ( function ( response ) {
1000+ successCallback ( response . EntitySetName ) ;
1001+ } , errorCallback ) ;
1002+ }
1003+ else {
1004+ //make a web api call for Node.js apps
1005+ var resolve = function ( result ) {
1006+ _entityNames = { } ;
1007+ for ( var i = 0 ; i < result . data . value . length ; i ++ ) {
1008+ _entityNames [ result . data . value [ i ] . LogicalName ] = result . data . value [ i ] . EntitySetName ;
1009+ }
10001010
1001- var reject = function ( error ) {
1002- errorCallback ( { message : 'Unable to fetch EntityDefinitions. Error: ' + error . message } ) ;
1003- } ;
1011+ successCallback ( findCollectionName ( entityName ) ) ;
1012+ } ;
10041013
1005- var request = RequestConverter . convertRequest ( {
1006- collection : 'EntityDefinitions' ,
1007- select : [ 'LogicalCollectionName' , 'LogicalName' ] ,
1008- noCache : true
1009- } , 'retrieveMultiple' , config ) ;
1014+ var reject = function ( error ) {
1015+ errorCallback ( { message : 'Unable to fetch EntityDefinitions. Error: ' + error . message } ) ;
1016+ } ;
1017+
1018+ var request = RequestConverter . convertRequest ( {
1019+ collection : 'EntityDefinitions' ,
1020+ select : [ 'EntitySetName' , 'LogicalName' ] ,
1021+ noCache : true
1022+ } , 'retrieveMultiple' , config ) ;
10101023
1011- sendRequest ( 'GET' , request . url , config , null , request . headers , null , resolve , reject , false , request . async ) ;
1024+ sendRequest ( 'GET' , request . url , config , null , request . headers , null , resolve , reject , false , request . async ) ;
1025+ }
10121026}
10131027
10141028function _isEntityNameException ( entityName ) {
0 commit comments