Skip to content Skip to sidebar Skip to footer

Run Action In Microsoft Dynamics Crm 2016 Using Javascript

I've created an Action under Process for a custom entity new_enrollment. I've created no I/O argument for that action. Now, by using following code snippet I want to run that custo

Solution 1:

you are using 2016 which supports WebAPI and its very easy using WebAPI. Below is the working example of calling custom action using JS. I have 2 output variables, one get set by custom action and the other one "ptext" by plugin which i have registered on this Action. Hope it will solve your problem.

function CallCAction(context) {       
 var Credit_Limit = Xrm.Page.getAttribute("creditlimit").getValue();
    var Credit_hold = Xrm.Page.getAttribute("creditonhold").getValue();
    if(Credit_hold !=null && Credit_Limit!=null){
        var actionName = "new_Preferred_Check";
        var inputParam = {
            "Credit_Limit": Credit_Limit,
            "Credit_hold": Credit_hold
        };
        Xrm.Page.ui.setFormNotification("Processing...","INFO","processingFId");
        var actionResponse = callPreferredCust(actionName, inputParam);

        if (actionResponse != null) {
            Xrm.Page.getAttribute("new_preferredcust").setValue(actionResponse.Preferred_Cust);
            alert(actionResponse.ptext);
            Xrm.Page.ui.clearFormNotification("processingFId");
        }        
    }    
}
function callPreferredCust(actionName, inputParam) {
    var result = null;
    var req = new XMLHttpRequest();
    var uri = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/";
    try {
        req.open("POST",encodeURI(uri+actionName),false);
        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){
                req.onreadystatechange = null;
                if(this.status==200){
                    result = JSON.parse(this.response);
                }else{
                    var err = JSON.parse(this.response).error;
                    alert(err.message);
                }
            }
        };
        req.send(JSON.stringify(inputParam));
        return result;

    }catch(err){
        alert(err.message);
    }
}

Post a Comment for "Run Action In Microsoft Dynamics Crm 2016 Using Javascript"