Secure and Unsecure Configurations By using Json
User story: Whenever you are updating the last name in the contact entity then Job Title and Email and Business Phone will be update.
In the visual studio first, we need to install Newtonsoft.Json from Manage NuGet Packages.
Code:
using Microsoft.Xrm.Sdk;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Shared_Variables
{
public class abc
{
public string phonenum;
public string jobTitle;
public string email;
}
public class jsonsecure : IPlugin
{
public readonly string _unsecure;
public readonly string _secure;
public jsonsecure(string unsecurevalue, string securevalue)
{
_unsecure = unsecurevalue;
_secure = securevalue;
}
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.Depth > 1)
{
return;
}
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
Entity contactEntity = (Entity)context.InputParameters["Target"];
var jsoninfo = JsonConvert.DeserializeObject<abc>(_unsecure);
var jsonemail= JsonConvert.DeserializeObject<abc>(_secure);
contactEntity["jobtitle"] = jsoninfo.jobTitle;
contactEntity["telephone1"] = jsoninfo.phonenum;
contactEntity["emailaddress1"] = jsonemail.email;
service.Update(contactEntity);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException (ex.Message);
}
}
}
}
}
· After writing the code in the visual studio click on build
· Open the Plugin Registration Tool and add the step
· In this scenario plugin will trigger on update the last name in the contact entity
· Choose filter attribute as last name
· Choose Event pipeline as Pre-Operation and Execute mode as Synchronous.
· Pass the values in the form of Json in the Unsecure and secure configuration area.
Output: Whenever you are updating the last name in the contact entity then Job Title and Email and Business Phone will be updated.
Comments
Post a Comment