Call action by using plugin and JavaScript?
Create a action, with input parameters First Name and Last name, Out put Full name.
create a plugin on custom action call, read the inputs and prepare full name = First name + last Name.
using System?
Send the output to action. Call this action from JavaScript , get the output and set fullname field value.
Trigger the JavaScript on- On save of your custom entity . Create FirstName, LastName and FullName Fields in your custom entity.
Ans.
Step1: For this fist need to create one action with 2 inputs and one output
Step2: After creating action we need to prepare one plugin that combining the both fist name and last name
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
namespace CallActionUsingPlugin
{
public class fullName : 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 tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
string firstName = context.InputParameters.Contains("FirstName") ? (string)context.InputParameters["FirstName"] : "field is not present";
tracing.Trace("first name is {0} ", firstName);//crm_lastname
string lastName = context.InputParameters.Contains("LastName") ? (string)context.InputParameters["LastName"] : "field is not present";
tracing.Trace("last name is {0} ", lastName);
string fullName = firstName + " " + lastName;
tracing.Trace("full name is {0} ", fullName);
context.OutputParameters["FullName"] = fullName;
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
Give the same names what You have given in action, so that we can read the inputs and set the output for parameter.
After Creating plugin, create a JavaScript to call the action by using XRM toolbox
In XRM Tool Box to check Result click on XHR and Inside the XHR Click on Move code later click on Execute code then automatically Result will come as FullName: Given Data (or) FullName: null
function callActionUsingJavascript(executionContext) {
var formContext = executionContext.getFormContext(); //crm_name crm_lastname crm_fullname
var fname = formContext.getAttribute("effizz_name").getValue();
console.log(fname);
var lname = formContext.getAttribute("effizz_lastname").getValue();
console.log(lname);
//entity record guid
var CurrentRecordId = formContext.data.entity.getId();
console.log(CurrentRecordId);
var CurrentRecordIdstrFormat = CurrentRecordId.replace(/[{}]/g, "");
//the record which was came in currentrecordId /[{}]/g with these will replace and send it to ""
//Start Paste
var execute_new_FullNamePlugin_Request = {
// this is var execute_new_FullNamePlugin_Request = is varible declaration
// Parameters
entity: { entityType: "effizz_customjs", id: CurrentRecordIdstrFormat }, // entity
FirstName: fname, // Edm.String
LastName: lname, // Edm.String
getMetadata: function () {
return {
boundParameter: "entity",
parameterTypes: {
entity: { typeName: "mscrm.effizz_customjs", structuralProperty: 5 },
FirstName: { typeName: "Edm.String", structuralProperty: 1 },
LastName: { typeName: "Edm.String", structuralProperty: 1 }
},
operationType: 0, operationName: "new_FullNamePlugin"
};
}
};
//Xrm.WebApi.execute in parameters the variable was taking i.e, execute_new_FullNamePlugin_Request
Xrm.WebApi.execute(execute_new_FullNamePlugin_Request).then(
function success(response) {
if (response.ok) { return response.json(); }
}
).then(function (responseBody) {
var result = responseBody;
console.log(result);
// Return Type: mscrm.new_FullNamePluginResponse
// Output Parameters
var fullname = result["FullName"]; // Edm.String effi_fullname
console.log(fullname);
formContext.getAttribute("effizz_fullname").setValue(fullname);
}).catch(function (error) {
console.log(error.message);
});
//End Paste
}
In XRM tool the code you should take from Xrm.Webapi
After calling action using JavaScript, create web resource and the function on save of the form
After that register the plugin in plugin register tool and call the action in message
.png)
.png)
.png)
.png)

.png)
Comments
Post a Comment