Java script Task-3: On click approve button, approve status should be approved and send a mail to parents by using Java script and workflow
- If you click on Approve button then Approve status should be Updated and send the mail to parent.
- Create one Button Named as Approved by using Ribbon Work Bench.
- Create one option set value give named as Approved, Reject.
- Create workflow (As an demand) and give step as send email.
- After writing the java script code add this code into web resource and call this web resource under approved button.
- Add Approve button by using ribbon workbench
Workflow: Based on parent (Contact)entity
workflow (As on demand demand) creation:
In the Add step choose send email
Java script Code:
function workFlow(primaryControl)
{
var formContext = primaryControl;
var parent = formContext.getAttribute("effi_parent").getValue();
var status = formContext.getAttribute("effi_approvalstatus").getValue();
var Id = parent[0].id.replace("{", "").replace("}", "");
var entity = {
"EntityId": Id // contactId
}
var WorkflowId = "9723276d-8260-45e4-b34b-9a1465da94e8";
if (status == 658320000)
{
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/workflows(" + WorkflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow", 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) {
Xrm.Utility.alertDialog("Success");
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
}
}
Add above code in Web Resource
Go to Ribbon workbench and choose entity
Click on Approved button
Click on Approve button command
Here click on Add Action
Library: Enter Web Resource name
Function Name: Enter Java script function Name
Click On Add parameter and choose CRM Parameter
Value: choose Primary Control
Output: Whenever you’re click on Approved button in Project entity mail sent to parent
Another way:
Workflow: Based on Project entity
function workFlow(primaryControl)
{
var formContext = primaryControl;
var status = formContext.getAttribute("effi_approvalstatus").getValue();
formContext.getAttribute("effi_approvalstatus").setValue(658320000);
var Id = formContext.data.entity.getId().replace("{", "").replace("}", "");
var entity = {
"EntityId": Id // project entity Id
}
var WorkflowId = "DFE5652D-21BB-4CBC-B163-4025DE9E6268";
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/workflows(" + WorkflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow", 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) {
Xrm.Utility.alertDialog("Success");
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
}
Add above code into web resource
And call this web Resource under Approved button
Note: After saving the record click on Approve button then only workflow will be triggered.
Comments
Post a Comment