Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Plugin not getting triggered at close event of incident entity

Posted on by 100
Hi ,
I am trying to restrict the closing of parent case when a associated  child case is open . I am aware that this can be done using parent child case setting in service management. But due to some requirement issue i am not using it. So for this i am trying to write a pluign . I tried two approaches :-
1) First i wrote a plugin at close message of incident entity. Yet somehow , this pluign is never getting triggered.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
 
namespace CloseCase
{
    public class ParentCase : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
 
 
            IOrganizationServiceFactory serviceFactory =
                  (IOrganizationServiceFactory)serviceProvider.GetService
                  (typeof(IOrganizationServiceFactory));
            IOrganizationService service =
               serviceFactory.CreateOrganizationService(context.UserId);
 
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
 
                QueryExpression qe2 = new QueryExpression("incident");
                ColumnSet col2 = new ColumnSet("parentcaseid");
                qe2.Criteria.AddCondition("parentcaseid", ConditionOperator.Equal, entity.Id);
                qe2.Criteria.AddCondition("statuscode", ConditionOperator.Equal, 1);
                EntityCollection coll = service.RetrieveMultiple(qe2);
 
                if (coll.Entities.Count > 0)
                {
                    throw new Exception("child record not closed");
                }
 
            }
        }
    }
}

2)  In 2nd approach i wrote a plugin on create of incidentResolution entity , and i am able to achieve my desired functionality i.e restricting the closing of parent case.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
 
namespace CloseCase
{
    public class ParentCase : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
 
 
            IOrganizationServiceFactory serviceFactory =
                  (IOrganizationServiceFactory)serviceProvider.GetService
                  (typeof(IOrganizationServiceFactory));
            IOrganizationService service =
               serviceFactory.CreateOrganizationService(context.UserId);
 
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
 
                QueryExpression qe2 = new QueryExpression("incident");
                ColumnSet col2 = new ColumnSet("parentcaseid");
                qe2.Criteria.AddCondition("parentcaseid", ConditionOperator.Equal, entity.GetAttributeValue<EntityReference>("incidentid").Id);
                qe2.Criteria.AddCondition("statuscode", ConditionOperator.Equal, 1);
                EntityCollection coll = service.RetrieveMultiple(qe2);
 
                if (coll.Entities.Count > 0)
                {
                    throw new Exception("child record not closed");
                }
 
            }
        }
    }
}

But somehow i am confused why my 1st approach is not working , maybe the code is not right but at least it should hit the execute method. I am not getting it , that on resolving the case , why close event is not triggering.

Please help me to understand if i am missing something.

Thanks
 

*This post is locked for comments

  • Suggested answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: Plugin not getting triggered at close event of incident entity

    If you are unsure whether the plugin is triggering or not, then simply throw new InvalidPluginExecutionException in first line of plugin & deploy. Then do repro steps, which is close incident. If exception pops out then triggering.

    Do add trace logs using ITracingService for better develop:debug cycle. Use profiler & replay/debug to see the issue.

  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: Plugin not getting triggered at close event of incident entity

    As per the previous post, you don't get a Target InputParameter for messages for state changes, so your code is probably not running as expected. Also, for some of the specific state changes, you may need to register the plugin on the SetState or SetStateDynamicEntity event as well, or instead of IncidentClose

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Plugin not getting triggered at close event of incident entity

    Hi,

    to trigger a plugin when status changed, you should use "EntityMoniker" instead of "Target" .

    Refer below article for how to achieve it.

    thomasthankachan.wordpress.com/.../plugin-usage-of-setstate-and-statestatedynamicentity-message

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans