Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

Plugin to Populate Case Closed Date

$
0
0

Hi all,

I'm creating a simple plugin to autopopulate Case closed date when a Case is resolved. For this I register my plugin in "SetState" and "SetStateDynamicEntity" of Incident entity. I created a new date field "new_closeddate" to contain the value.

For some reason my plugin never fires even though I registered it several times and did IISReset. Could anyone please guide me in the right direction? Thank you much for your help.

Here is my code below.

Thanks,

-elisabeth

public override void OnExecute(IServiceProvider serviceProvider, IPluginExecutionContext context)
        {
            var trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // The InputParameters collection contains all the data passed in the message request.
            var targetReference = context.GetParameterCollection<EntityReference>(context.InputParameters, "EntityMoniker");

            if (targetReference == null)
                throw new InvalidPluginExecutionException(OperationStatus.Failed, "Target Entity cannot be null");

            var state = (OptionSetValue)context.InputParameters["State"];

            /* Only proceed if Incident is being set to Resolved */
            if (state == null || state.Value != 1)
                return;      

            var postImage = context.PostEntityImages["PostImage"];

            if (postImage == null)
                throw new InvalidPluginExecutionException(OperationStatus.Failed, "Post Image is required");

            var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            var service = serviceFactory.CreateOrganizationService(context.UserId);

            Entity CaseIncident = new Entity("incident");
            CaseIncident.Attributes = new AttributeCollection();
            CaseIncident.Attributes.Add("new_closeddate", DateTime.Now);

        }

        protected override bool CanExecute(IPluginExecutionContext context)
        {
            return context.PreConditionCheck(
                0,
                new List<int> { MessageProcessingStage.AfterMainOperationInsideTransaction },
                new List<string> { MessageName.Update },
                new List<string> { Schema.Incident.EntityLogicalName });
        }

  

Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>