Sample Code with Different Types of input parameters -
// Function to initiate Guest Visit Action Calls .................................................................................
function InitiateGuestPassRequest(PassBeginDate, PassDays) {
Shared.WriteToConsole("Action Call ......................................");
var parameters = GetGlobalContext().getQueryStringParameters();
Shared.WriteToConsole("Entity ID =: " + parameters.id);
Shared.WriteToConsole("Entity Name =: " + parameters.entityTypeName);
var InitiatingUserID = Xrm.Utility.getGlobalContext().userSettings.userId;
Shared.WriteToConsole("InitiatingUserID =: " + InitiatingUserID);
Shared.WriteToConsole("PassBeginDate =: " + PassBeginDate);
PassBeginDate = new Date(PassBeginDate.setUTCHours(23, 59, 59));
Shared.WriteToConsole("PassBeginDate =: " + PassBeginDate);
Shared.WriteToConsole("PassDays =: " + PassDays);
var PassExpireDate = new Date();
PassExpireDate.setDate(PassBeginDate.getDate() + PassDays);
PassExpireDate = new Date(PassExpireDate.setUTCHours(23, 59, 59));
Shared.WriteToConsole("PassExpireDate =: " + PassExpireDate);
window.parent.Xrm.Page.ui.setFormNotification("Guest Pass allocation in Progress.", "INFORMATION", "GuestPassProcessNotification");
//get the current organization name
var serverURL = window.parent.Xrm.Page.context.getClientUrl();
//query to send the request to the global Action
var actionName = "abc_IssueGuestPass"; // Global Action Unique Name
//set the current loggedin userid in to _inputParameter of the
var InputParameterValue = window.parent.Xrm.Page.context.getUserId();
//Pass the input parameters of action
var InputParameters = {
"Opportunity": { "@odata.type": "Microsoft.Dynamics.CRM.opportunity", "opportunityid": parameters.id },
"IssuedBy": { "@odata.type": "Microsoft.Dynamics.CRM.systemuser", "systemuserid": InitiatingUserID },
"PassBeginDate": new Date(PassBeginDate),
"PassExpireDate": new Date(PassExpireDate),
"PassIssuedDays": parseInt(PassDays),
"PassRemainingDays": parseInt(PassDays)
};
var req = new XMLHttpRequest();
//Post the WEB API Request
req.open("POST", serverURL + "/api/data/v9.1/" + actionName, true);
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");
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200 || this.status == 204) {
//You can get the output parameter of the action with name as given below
result = JSON.parse(this.response);
if (result.Error == false) {
window.parent.Xrm.Page.ui.clearFormNotification("GuestPassProcessNotification");
window.parent.Xrm.Page.ui.setFormNotification("Guest Pass allocation Process Successfully Executed.", "INFORMATION", "GuestPasSuccessNotification");
window.parent.Xrm.Page.data.refresh();
setTimeout(function () {
window.parent.Xrm.Page.ui.clearFormNotification("GuestPasSuccessNotification");
}, 3000);
}
else {
window.parent.Xrm.Page.ui.clearFormNotification("GuestPassProcessNotification");
Shared.UCIIFrameNotifications(result.ErrorMsg, "WARNING", "GuestPassValidationWarning");
}
}
else {
var error = JSON.parse(this.response).error;
window.parent.Xrm.Page.ui.clearFormNotification("GuestPassProcessNotification");
Shared.UCIIFrameNotifications("Please contact IT Service Desk.Error in Action: " + error.message, "ERROR", "GuestPassErrorNotification");
}
}
};
//Execute request passing the input parameter of the action
req.send(window.JSON.stringify(InputParameters));
}