Java Script Task-4: Call the plugin using java script and set the approved status
User story: If you click on the approve button then approve status should get updated achieve this scenario by using plugin and java script
1) Create one Action (Here am created Action on Project entity)
· Note down the Action Unique Name
2) Open CRM Rest Builder and choose Primary entity as Project then in Action field it will show all related Actions based on your provided Entity.
3) write Java script code by using CRM Rest builder provided code.
· In CRM Rest builder provided code need to be give the entity Id as dynamically.
· Create One Web Resource and add this code in to this web resource.
4) create one Approve Button by using Ribbon Work Bench.
· Add above Web Resource into this Approved Button.
5) Write Plugin Code
· By using Entity Reference retrieve the entity
· Set the value of Status reason as Approval.
· By using service.Update update the status Reason Value.
6) Open Plugin Registration Tool and add new assembly àAdd New step
· In New Step Choose Message as Action Name
· Choose Post Operation and choose Execute mode as synchronous
Action creation:
In CRM Rest Builder
End point : Choose Web Api
Output Format: Xrm.WebApi
Primary Entity: Choose Entity Name
Action: Choose Action name (here automatically reflected what are the actions belongs to this entity)
Click on Create Request
Below code is generated by CRM Rest Builder:
var parameters = {};
var entity = {};
entity.id = "";
entity.entityType = "effi_project";
parameters.entity = entity;
var new_pluginactionRequest = {
entity: parameters.entity,
getMetadata: function() {
return {
boundParameter: "entity",
parameterTypes: {
"entity": {
"typeName": "mscrm.effi_project",
"structuralProperty": 5
}
},
operationType: 0,
operationName: "new_pluginaction"
};
}
};
Xrm.WebApi.online.execute(new_pluginactionRequest).then(
function success(result) {
if (result.ok) {
//Success - No Return Data - Do Something
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
In Above code we have to do some modifications
After modifications java script code:
function callActionstatus(primaryControl)
{
var formContext = primaryControl;
var Id = formContext.data.entity.getId().replace("{", "").replace("}", "");
var parameters = {};
var entity = {};
entity.id = Id;
entity.entityType = "effi_project";
parameters.entity = entity;
var new_pluginactionRequest = {
entity: parameters.entity,
getMetadata: function () {
return {
boundParameter: "entity",
parameterTypes: {
"entity": {
"typeName": "mscrm.effi_project",
"structuralProperty": 5
}
},
operationType: 0,
operationName: "new_pluginaction"
};
}
};
Xrm.WebApi.online.execute(new_pluginactionRequest).then(
function success(result) {
if (result.ok) {
//Success - No Return Data - Do Something
console.log("success");
}
},
function (error) {
Xrm.Utility.alertDialog(error.message);
}
);
}
Add above code in Web Resource click ok and Publish
Click on Plugin Approve button In ribbon Work Bench
Click on Button Command
Click on Add Action:
Library: Enter Web Resource Name
Function Name: Enter Java script Function Name
Click on Add Parameter: choose CRM parameter
Value: Choose Primary Control
Plugin Code:
namespace plugin
{
public class approvedstatus : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
tracer.Trace("outer if");
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{
tracer.Trace("entering into if");
try
{
EntityReference projectentityReference = (EntityReference)context.InputParameters["Target"];
Entity projectentity =new Entity(projectentityReference.LogicalName);
projectentity.Id = projectentityReference.Id;
tracer.Trace("entering in try");
projectentity["effi_pluginapprovalstatus"] = new OptionSetValue(1);
service.Update(projectentity);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
}
Go to Plugin Registration Tool
Click on Register and choose Register New Assembly
Click on Register New step
Go to CRM
Open new record enter Name and click on save
after clicking on save click on Plugin Approve Button
Then status should be updated as Approved
Comments
Post a Comment