Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
6.4 kB
2
Indexable
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CreateAndUpdateCderPlugin
{
    public class UpdateCreateNeuCderPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            var configData = new[]
                {
                    new
                    {
                        fieldname = "neu_eval2walking",
                        values = new[] { 288500000, 288500001, 288500002 }
                    },
                    new
                    {
                        fieldname = "neu_eval3usingwheelchair",
                        values = new[] { 288500000, 288500001, 288500002 }
                    },
                    new
                    {
                        fieldname = "neu_eval4takingprescriptionmedication",
                        values = new[] { 288500000, 288500001, 288500002 }
                    },
                    new
                    {
                        fieldname = "neu_eval5eating",
                        values = new[] { 288500000, 288500001, 288500002, 288500003 }
                    },
                    new
                    {
                        fieldname = "neu_eval6toileting",
                        values = new[] { 288500000, 288500001, 288500002, 288500003 }
                    },
                    new
                    {
                        fieldname = "neu_eval7bladderandbowelcontrol",
                        values = new[] { 288500000, 288500001, 288500002 }
                    },
                    new
                    {
                        fieldname = "neu_eval8personalcare",
                        values = new[] { 288500000, 288500001, 288500002, 288500003 }
                    },
                    new
                    {
                        fieldname = "neu_eval9dressing",
                        values = new[] { 288500000, 288500001, 288500002, 288500003 }
                    },
                    new
                    {
                        fieldname = "neu_safetyawareness",
                        values = new[] { 288500000, 288500001, 288500002 }
                    },
                    new
                    {
                        fieldname = "neu_eval14socialinteraction",
                        values = new[] { 288500000, 288500001 }
                    },
                    new
                    {
                        fieldname = "neu_disruptivesocialbehaviour",
                        values = new[] { 288500000, 288500001, 288500002 }
                    },
                    new
                    {
                        fieldname = "neu_eval16aggressivesocialbehaviour",
                        values = new[] { 288500000, 288500001, 288500002, 288500003 }
                    },
                    new
                    {
                        fieldname = "neu_eval17selfinjuriousbehaviour",
                        values = new[] { 288500000, 288500001, 288500002, 288500003 }
                    },
                    new
                    {
                        fieldname = "neu_eval18destructionofproperty",
                        values = new[] { 288500000, 288500001, 288500002 }
                    },
                    new
                    {
                        fieldname = "neu_runningorwanderingaway",
                        values = new[] { 288500000, 288500001, 288500002, 288500003 }
                    },
                    new
                    {
                        fieldname = "neu_eval20emotionaloutbursts",
                        values = new[] { 288500000, 288500001, 288500002 }
                    }
                };
            if (context.InputParameters.Contains(Constants.Target) && context.InputParameters[Constants.Target] is Entity)
            {
                Entity entity = (Entity)context.InputParameters[Constants.Target];
               
                if (entity.LogicalName != Constants.JsonObjects.Cder.EntityLogicalName)
                    return;
                Entity postImage = context.PostEntityImages.FirstOrDefault().Value;
                // Check if the Consumer (Contact) lookup field is present
                if (postImage.Attributes.Contains(Constants.JsonObjects.Cder.Consumer))
                {
                     
                    var consumerRef = postImage.GetAttributeValue<EntityReference>(Constants.JsonObjects.Cder.Consumer);
                    Entity consumer = service.Retrieve(consumerRef.LogicalName, consumerRef.Id, new ColumnSet(Constants.JsonObjects.Contact.MedicalWaiver, Constants.JsonObjects.Contact.CurrentCder));
                    if (consumer.Contains(Constants.JsonObjects.Contact.MedicalWaiver) && consumer.GetAttributeValue<bool>(Constants.JsonObjects.Contact.MedicalWaiver))
                        return;
                    var consumerCurrentCdr = consumer.GetAttributeValue<EntityReference>(Constants.JsonObjects.Contact.CurrentCder);
                    if(entity.Id!= consumerCurrentCdr.Id)
                        return;

                    var isMedicalNeedtoUpdate = configData.Any(it => postImage.Contains(it.fieldname) && it.values.Contains(postImage.GetAttributeValue<OptionSetValue>(it.fieldname).Value));
                    if (isMedicalNeedtoUpdate)
                    {
                        var consumerToUpdate = new Entity(consumer.LogicalName, consumer.Id);
                        consumerToUpdate[Constants.JsonObjects.Contact.MedicalWaiver] = true;
                        service.Update(consumerToUpdate);
                    }
                   
                }
            }
        }
    }
}
Leave a Comment