4. Call the plugin using JavaScript and set the approved status
User Story: Call the plugin using JavaScript and set the approved
status
Requirement: JavaScript, Action and Plugin
1.
Create a custom button with the name of Plugin
approve by using XRM Tool box
2.
Create one Status field with option set datatype
3.
Create an action for student entity without
arguments and steps
4.
Generate a code from Rest Builder using created
action
5.
Write JavaScript function to trigger action and
place it in web resource
6.
Call JavaScript function from Plugin approve
button using XRM Toolbox
7.
Write a plugin code to update status field as
approved
· By
using Entity Reference retrieve the entity
· Set
the value of Status reason as Approval.
· By
using service.Update update the status Reason Value.
8.
In plugin registration tool
·
Create a new assembly
·
For that assembly create a new step
·
Message: Action Name
·
Primary Entity: crb76_Student
·
Execute Pipeline: Post Operation
·
Execution Mode: Synchronous
Create a custom button with the name of Plugin approve by using XRM Tool box and Create one Status field with option set datatype
Generate a code using Rest
Builder
var parameters = {};
var entity = {};
entity.id = "";
entity.entityType = "crb67_student";
parameters.entity = entity;
var new_EffActionforpluginRequest = {
entity:
parameters.entity,
getMetadata:
function() {
return {
boundParameter: "entity",
parameterTypes: {
"entity": {
"typeName": "mscrm.crb67_student",
"structuralProperty": 5
}
},
operationType: 0,
operationName: "new_EffActionforplugin"
};
}
};
Xrm.WebApi.online.execute(new_EffActionforpluginRequest).then(
function
success(result) {
if (result.ok)
{
//Success
- No Return Data - Do Something
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
Write JavaScript function to trigger action and place it in web resource
function Action_plugin(primaryControl)
{
var
formContext = primaryControl;
var id =
formContext.data.entity.getId().replace("{", "").replace("}", "");
var
parameters = {};
var entity
= {};
entity.id = id;
entity.entityType = "crb67_student";
parameters.entity = entity;
var
new_EffActionforpluginRequest = {
entity: parameters.entity,
getMetadata: function () {
return {
boundParameter: "entity",
parameterTypes: {
"entity": {
"typeName": "mscrm.crb67_student",
"structuralProperty": 5
}
},
operationType: 0,
operationName: "new_EffActionforplugin"
};
}
};
Xrm.WebApi.online.execute(new_EffActionforpluginRequest).then(
function success(result) {
if
(result.ok) {
Xrm.Utility.alertDialog("Success");
}
},
function (error) {
Xrm.Utility.alertDialog(error.message);
}
);
}
Call JavaScript function from
Plugin approve button using XRM Toolbox
Write a plugin code to update
status field as approved
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
namespace Student
{
public class Plugin_class : 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));
if
(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is
EntityReference)
{
try
{
EntityReference
entityReference=(EntityReference)context.InputParameters["Target"];
Entity entity = new Entity(entityReference.LogicalName);
entity.Id =
entityReference.Id;
entity["crb67_statuss"] = new
OptionSetValue(518320000);
service.Update(entity);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
}
Go to Plugin Registration Tool:
Click on Register and choose Register New Assembly
Out Put:
Comments
Post a Comment