Saturday, July 4, 2020

calling Action from button click using Javascript

In this post , we will see how to call the action by click the ribbon button in crm. Using javascript we can call the action , based on your requirement action condition can specified. Check the below code to call the action using javascript. Below javascript is for when click the button (flyover button) it will display the three button. Based on button click we are assigning the record to particular team.

Action name : new_AssignRecordtoTeams

Ribbon button execution content : primaryControl

Parameter : buttonname

function AssignRecordToTeam(primaryControl, buttonname) {
    var formContext = primaryControl;
    var currentRecordIdString = formContext.data.entity.getId();
    var currentRecordId = currentRecordIdString.replace("{", '').replace("}", '');
    var teamId = null;

    if (buttonname.toLowerCase() == "Team1".toLowerCase()) {
        teamId = "71AB7444-98A9-EA11-A812-000D3A5A1CF8";
    }
    else if (buttonname.toLowerCase() == "Team2".toLowerCase()) {
        teamId = "0F3B2555-98A9-EA11-A812-000D3A5A1CF8";
    }
    else if (buttonname.toLowerCase() == "Team3".toLowerCase()) {
        teamId = "3C5F5BF8-C9AE-EA11-A812-000D3A5A1BFB";
    }
    else {
        teamId = null;
    }

    if (teamId != null) {
        ////using action 
        var parameters = {};
        var team = {};
        team.teamid = teamId;
        team["@odata.type"] = "Microsoft.Dynamics.CRM.team";
        parameters.Teams = team;

        var req = new XMLHttpRequest();
        req.open("POST", Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.1/emails(" + currentRecordId + ")/Microsoft.Dynamics.CRM.new_AssignRecordtoTeams", false);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 204) {
                    var URL = Xrm.Utility.getGlobalContext().getCurrentAppUrl() + "&pagetype=entityrecord&etn=email&id=" + currentRecordId + "";
                    window.top.location.assign(URL);
Xrm.Utility.alertDialog(Assigned record To Team");
                    //formContext.data.entity.save();
                } else {
                    alert(this.statusText);
                }
            }
        };
        req.send(JSON.stringify(parameters));
    }
}

No comments:

Post a Comment