locked
CRM 2016 Deactivate a record via Plugin not working RRS feed

  • Question

  • Hello,

    System Platform: CRM 2016 On-Premise

    I'm trying to deactivate a custom entity record based on a field's value change by using plugin. after register the plugin, change the value of the field, it looks nothing happen. I'm not sure where the issue comes from. since I can only use plugin profiler to debug the plugin, but when I run the process, profiler doesn't catch any exceptions. Here is my code and plugin steps. Please help, thanks.

    if (context.InputParameters.Contains("EntityMoniker")
                         && context.InputParameters["EntityMoniker"] is EntityReference
                         && context.PrimaryEntityName.Equals(commons.RequestField.EntityName))
                    {
                        if (helper.IsMsgSetState() || helper.IsMsgSetStateDynamicEntity())
                        {
                            EntityReference entity = (EntityReference)context.InputParameters["EntityMoniker"];
                            Entity targetEntity=helper.OrgSrv.Retrieve(entity.LogicalName,entity.Id,new ColumnSet(new String[]{commons.RequestField.StateCode,commons.RequestField.StatusCode,commons.RequestField.Status}));
    
                            if (entity != null && targetEntity != null)
                            {
                                int requeststatusValue = 0;
                                if (targetEntity.Attributes.Contains(commons.RequestField.Status))
                                {
                                    var requeststatus = targetEntity.GetAttributeValue<OptionSetValue>(commons.RequestField.Status);
                                    requeststatusValue = (requeststatus == null ? 0 : requeststatus.Value);
                                    if ((requeststatusValue == 800000010)
                                        || (requeststatusValue == 800000011)
                                        || (requeststatusValue == 800000012)
                                        || (requeststatusValue == 800000013))
                                    {
                                        int statecodevalue = ((OptionSetValue)context.InputParameters[commons.RequestField.StateCode]).Value;
                                        if (statecodevalue == 0)
                                        {
                                            SetStateRequest requestObj = new SetStateRequest()
                                            {
                                                EntityMoniker = new EntityReference(targetEntity.LogicalName, targetEntity.Id),
                                                State = new OptionSetValue(1),
                                                Status = new OptionSetValue(2)
                                            };
                                            helper.OrgSrv.Execute(requestObj);
                                        }
                                    }
                                }
                            }
                        }
                    }

    SetStateDynamicEntity Step

    SetState Step


    • Edited by howexg9 Friday, October 21, 2016 10:31 PM
    Friday, October 21, 2016 10:23 PM

Answers

  • Hello,

    Are you aware that step you registered and code you written expects not for Update of a field but change of state?


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Saturday, October 22, 2016 5:57 AM
    Moderator
  • As per my understanding you want to deactivate the record if the attribute value of that entity set to some value.

    To do so, first get the entity context and check for an attribute exists then compare the value. If matches , create SetStateRequest and use execute method to set the status. Register your plugin on update of entity "srms_request". Click on filtering attributes select only the attribute which you want to execute this logic.

    Hope this resolves your issue.

    • Marked as answer by howexg9 Thursday, November 17, 2016 7:12 PM
    Thursday, October 27, 2016 11:46 AM

All replies

  • Hello,

    Are you aware that step you registered and code you written expects not for Update of a field but change of state?


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Saturday, October 22, 2016 5:57 AM
    Moderator
  • As per my understanding you want to deactivate the record if the attribute value of that entity set to some value.

    To do so, first get the entity context and check for an attribute exists then compare the value. If matches , create SetStateRequest and use execute method to set the status. Register your plugin on update of entity "srms_request". Click on filtering attributes select only the attribute which you want to execute this logic.

    Hope this resolves your issue.

    • Marked as answer by howexg9 Thursday, November 17, 2016 7:12 PM
    Thursday, October 27, 2016 11:46 AM