Last active
December 10, 2018 19:10
-
-
Save NickPl/57c66d933254180ed045bf6ccfa56658 to your computer and use it in GitHub Desktop.
Utility Javascript class for Dynamics CRM 2016
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* jshint unused: false */ | |
/* globals Xrm */ | |
var XrmUtil = (function () { | |
// Private / Global var | |
var odataEndPoint = Xrm.Page.context.getClientUrl() + '/api/data/v8.2/'; | |
// Private / Global function | |
var ErrorHandler = function (e, origin) { | |
try { | |
var message = 'There was no error message'; | |
if (e.response) { | |
var parsed = JSON.parse(e.response); | |
if (parsed.error.message) { | |
message = parsed.error.message; | |
} | |
} | |
console.log('Error - ' + origin + ': ' + message); | |
} catch (exception) { | |
console.log('Error while trying to display the error message'); | |
} | |
}; | |
var SetGenericRequestHeader = function (req) { | |
req.setRequestHeader("Accept", "application/json"); | |
req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); | |
req.setRequestHeader("OData-MaxVersion", "4.0"); | |
req.setRequestHeader("OData-Version", "4.0"); | |
return req; | |
}; | |
// Public | |
return { | |
/** | |
* Calls a workflow Action | |
* @param {string} entityType Type of the entity to use | |
* @param {string} entityId Id of the entity to use | |
* @param {string} actionName Name of the action to call | |
* @param {array} actionParams Array of parameter, parameters depends on the action | |
* @return {object} Result of the request | |
*/ | |
ConsumeAction: function (entityType, entityId, actionName, actionParams, async) { | |
var result = null; | |
if (!async && async !== false) { | |
async = true; | |
} | |
var req = new XMLHttpRequest(); | |
req.open("POST", odataEndPoint + entityType + 's(' + this.TrimGuid(entityId) + ')/Microsoft.Dynamics.CRM.' + actionName, async); | |
req = SetGenericRequestHeader(req); | |
req.onreadystatechange = function () { | |
if (this.readyState == 4) { | |
req.onreadystatechange = null; | |
if (this.status == 200) { | |
if (this.response) { | |
result = JSON.parse(this.response); | |
} | |
} else if (this.status == 204) { | |
// Everything then well | |
result = true; | |
} | |
else { | |
ErrorHandler(this, 'ConsumeAction'); | |
} | |
} | |
}; | |
if (actionParams) { | |
req.send(window.JSON.stringify(actionParams)); | |
} | |
else { | |
req.send(); | |
} | |
return result; | |
}, | |
/** | |
* Trim and lower case a guid to normalize it | |
* @param {string} guid | |
* @return {string} The guid trimed | |
*/ | |
TrimGuid: function (guid) { | |
if (guid) | |
return guid.replace('{', '').replace('}', '').toLowerCase(); | |
else | |
return ''; | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment