Task-3 : JavaScript and Workflow

Task-3 : JavaScript and Workflow 

User Story: - 

On click of approve button, approve status should be Approved and send a mail to parents. 

Sudo Code: - 

  1. Create an Approve button for custom entity using Ribbon work bench in XRM Toolbox. 

  • Create a solution for custom entity ribbon work bench. 

  • Add the required entity meta data to the solution. 

  • Use this solution in Ribbon work bench to create buttons and publish it the entity. 

  1. Create a workflow which sends an email to Parent field lookup values which is contact. 

  • Workflow should have on demand enabled. 

  • Retrieve the Workflow id from URL. 

  1. Write the JavaScript code to call When the Approve button is clicked. 

  1. In the JavaScript code write code to call the workflow using XMLHttp Request. 

  1. Create a web resource source as Script(JS). 

  1. For the Approve button Add the Action as JavaScript function to trigger when button is clicked by using Ribbon Work bench. 

  1. Select the Web resource and provide the function name. 

  1. Now publish the button. 

 

Task Procedure: - 

  1. Create a workflow which will send email to its parent with on demand enabled. 

  1.  

  1. Copy the workflow id from URL. 

  1.  

 

  1. Create a solution and add the metadata of entity where should the button placed. 

  1. Open XRM toolbox. 

  1. Connect to the organization by providing the Org URL, username and password. 

  1.   


  1. Open Ribbon work bench. 

  1. Select the solution. 

  1.  


  1. Create a button by drag and drop. 

  1.  


  1. Create a JavaScript Web resource, place the given JavaScript code in it then Save and Publish. 

 

function CallingWorkflowUsingJs(primaryControl) {
    var formContext = primaryControl;
    var EntityId = formContext.data.entity.getId().replace("{", "").replace("}", "");
    var entity = { "EntityId": EntityId };


    var WorkflowId = "006F2DD4-47F1-4E35-947E-223BB63BBBD4"

    var Url = formContext.context.getClientUrl() + "/api/data/v9.0/workflows(" + WorkflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";

    var req = new XMLHttpRequest();
    req.open("POST", Url, true);
    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 === 200) {
                formContext.getAttribute("naree_status___").setValue(737110000);
                Xrm.Navigation.openAlertDialog("Success")
            } else {
                Xrm.Navigation.openAlertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));
}

 

 

 

  • Create command for a button and add Action as a JavaScript function, here you can select the published web resource and provide the JavaScript method. 
  • Pass the first parameter as CRM Parameter which is primary control 

 

 

 

 

Comments

Popular posts from this blog

Using Shared Variables in Plug-in and send an email inside the CRM User using Plugin

How to create an Azure AD(Active Directory) and create an application to connect Dynamics 365 from console application

Task-8 : Updating Project Start Date and End Date