PreImage and PostImage

 Plugin Code for Pre Image and Post Image:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Tooling;


namespace CheckImageQuery

{

    public class checkImage : 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));


            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

            {

                try

                {

                    if (context.Depth == 1)

                    {

                        Entity entity = (Entity)context.InputParameters["Target"];


                        Entity preImage = (Entity)context.PreEntityImages["preImagee"];

                        Entity postImage = (Entity)context.PostEntityImages["PostImage"];


                        string preContactImage = preImage.GetAttributeValue<string>("telephone1");

                        string postContactImage = postImage.GetAttributeValue<string>("telephone1");                     


                        tracing.Trace("Pre-image phone number: {0} ", preContactImage);

                        tracing.Trace("Post-image number: {0}", postContactImage);

                    }

                }

                catch(InvalidPluginExecutionException ex)

                {

                    throw new InvalidPluginExecutionException(ex.Message.ToString());

                }

               

            }


                                               *(or)*

             // 

            //if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

            //{

            //    try

            //    {

            //        if (context.Depth == 1)

            //        {


            //            Entity entity = (Entity)context.InputParameters["Target"];


            //            Entity preImage = (Entity)context.PreEntityImages["preImagee"];

            //            Entity postImage = (Entity)context.PostEntityImages["PostImage"];



            //            Entity updateContact = new Entity("contact");

            //            updateContact.Id = entity.Id;

            //            updateContact["mobilephone"] = preImage.Attributes["telephone1"];

            //            updateContact["fax"] = postImage.Attributes["telephone1"];

            //            service.Update(updateContact);

            //        }

            //    }

            //    catch (InvalidPluginExecutionException ex)

            //    {

            //        throw new InvalidPluginExecutionException(ex.Message.ToString());

            //    }

            //}


        }

    }

}


Follow the YouTube Link To Get Clarityhttps://youtu.be/BNpqbt8NDhw?si=JMLV1fOHE1ZUpzoQ

Comments

Popular posts from this blog

If any case created, then check for the same user how many cases are created with in 30 days, if more than 2 and less than 5 send a mail to team lead, if more than 5 and less than 9 then send a mail to manager using power automate.

Create approve & reject ribbon buttons, once click on approve it should change the status field to approve.If clicked on reject it should change to Reject. Based on status field change trigger work flow to send a email to stating request is approved/Rejected.

How to get and set values in plugins?