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.

 


OutputWhenever you are updating the last name in the contact entity then Job Title and Email and Business Phone will be updated.

 


 

 

Comments

Popular posts from this blog

SSRS User Story-7: Create a SSRS report group by Case, retrieve all work orders and make work orders as sub group, retrieve all bookable resource bookings based on work order. If booking is completed then show the row color as green.

SSRS User Story-6: Prepare a SSRS report on the birthday, In First part, group by with date of birth. In second report, prepare the wishes card with individual students.

SSRS User Story-5: Create a SSRS report with student name, DOB, Date of Admission, parent details. Schedule this report, every day evening 6 pm it need to run and share with managers. (use power automate)