Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

How to assign KBA to the Case entity based on Case title using Plugin ?

(0) ShareShare
ReportReport
Posted on by 40

Hi cloflyMao,

I have four KBA in my CRM and I want to populate the required KBA according to the Case title (using word matching criteria between Case title and KBA title) to the Case Entity using Plugin.

I am new to this please guide me step by step.

Regards,

Sheershendu

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: How to assign KBA to the Case entity based on Case title using Plugin ?

    Hi Sheershendu,

    It is very glad that the problem has been resolved.

    Regards,

    Clofly

  • Sheershendu Profile Picture
    Sheershendu 40 on at
    RE: How to assign KBA to the Case entity based on Case title using Plugin ?

    Hi Clofly

    Today morning I resolved the issue.According to the tracing log I just kept the business logic of try block inside if(context.Depth > 2) and registered the plugin in the Post Operation Asynchronous mode.

    Thank You so much for your help.

    Regards,

    Sheershendu

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: How to assign KBA to the Case entity based on Case title using Plugin ?

    Hi Sheershendu,

    May I know which auto creation method you are using?

    In legacy web client, it is actually workflow driven. While in UCI configuration, the auto creation rule is associated with flow.

    If you are using the previous one, what we need to concern is collision between workflow and our plugin, the situation might be similar to discussion of following thread:

    https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/236422/plugin-is-not-getting-triggered-when-creating-automatic-case-from-incoming-emails/649169

    If so, I'm not sure whether a custom workflow activity would be better option for your requirement.

    However, if you are using flow, then plugin is not necessary and we can do customization within the flow.

    (My case creation rule is based on flow, and my plugin executes at Post operation in Asynchronous way, the relevant knowledge article could be associated with the new case.)

    Regards,

    Clofly

  • Sheershendu Profile Picture
    Sheershendu 40 on at
    RE: How to assign KBA to the Case entity based on Case title using Plugin ?

    Hi Clofly,

    I checked with the Plugin Tracing log and found that it's not going into the try block mentioned in the screenshot when I am resigtering it in the Post-operation Asynchronously (because I want to assign the KA in the automatic case creation). For that I have to resigter it in the Post operation Asynchronously but I checked with Tracing log that it's not going into the try block.But it's working fine in the Post operation Synchronously in the manual Case creation.Please have a look into it.

    Regards,

    Sheershendu

    Screenshot-_2800_55_2900_.png

  • Sheershendu Profile Picture
    Sheershendu 40 on at
    RE: How to assign KBA to the Case entity based on Case title using Plugin ?

    Hi Clofly,

    Yes, I am using automatic case creation rule i.e whenever  a mail comes a Case is created automatically. So, in that case KA is not populating automatically.I think for that Plugin should be register in the Post-Operation of Asynchronous mode.But how to do that ?

    I want that if any of the word of Case title matches with the keyword of any KA then it should populate that KA to the Case.

    Regards,

    Sheershendu

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: How to assign KBA to the Case entity based on Case title using Plugin ?

    Hi Sheershendu,

    1. Are you using case creation rules of UCI?

    https://docs.microsoft.com/en-us/dynamics365/customer-service/automatically-create-update-records

    2. Have you check keyword value of the random populated KA? In my code, it will just pick the top 1 record of query result, so if there is other KA with same keyword, the field will be populated with a random record.

    Could you let me know which conditions would you like to apply to query to get the desired record instead of random record?

    Regards,

    Clofly

  • Sheershendu Profile Picture
    Sheershendu 40 on at
    RE: How to assign KBA to the Case entity based on Case title using Plugin ?

    Hi Clofly,

    It is working in the Post Operation synchronously when I am creating Case manually but it's not working for automatic case creation. Also it is populating random article from KA in the manual case creation.

    Regards,

    Sheershendu

  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: How to assign KBA to the Case entity based on Case title using Plugin ?

    Hi Sheershendu,

    Thanks for your screenshot, it let me know that what you want to do is associating case with related knowledge article.

    Please try following code for test:

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using System;
    
    namespace DynamicsPlugins
    {
        public class plugin3 : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                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"];
                    if (entity.LogicalName != "incident")
                    {
                        return;
                    }
                    if (context.Depth > 2)
                    {
                        tracingService.Trace("Context depth is: {0}", context.Depth);
                        return;
                    }
                    try
                    {
                        if (entity.Attributes.Contains("title"))
                        {
                            String caseTitle = (String)entity.Attributes["title"];
    
                            string[] caseWords = caseTitle.Split(null);
    
                            for (int i = 0; i < caseWords.Length; i  )
                            {
                                if (findKBA(service, caseWords[i], entity) == true)
                                {
                                    return;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        tracingService.Trace("Error of Plugin: {0}", ex.ToString());
                        throw new InvalidPluginExecutionException("An error occured for SetState plugin "   ex.Message   ex.InnerException); ;
                    }
                }
    
            }
    
            private static Boolean findKBA(IOrganizationService orgService, String caseWord, Entity contextEntity)
            {
                var query = new QueryExpression("knowledgearticle") { ColumnSet = new ColumnSet("title", "keywords") };
                query.Criteria = new FilterExpression();
                query.Criteria.AddCondition("keywords", ConditionOperator.Like, "%"   caseWord   "%");
    
                EntityCollection result = orgService.RetrieveMultiple(query);
    
                if (result.Entities.Count > 0)
                {
                    // Create a new knowledge article incident
                    Guid kAIncident_id = new Guid();
    
                    Entity kAIncident = new Entity("knowledgearticleincident");
    
                    kAIncident["knowledgearticleid"] = new EntityReference("knowledgearticle", result.Entities[0].Id);
    
                    kAIncident["incidentid"] = new EntityReference("incident", contextEntity.Id);
    
                    kAIncident_id = orgService.Create(kAIncident);
    
                    //Associate the knowledge article record with the Case record.         
                    EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
    
                    relatedEntities.Add(new EntityReference("knowledgearticleincident", new Guid(kAIncident_id.ToString())));
    
                    Relationship newRelationship = new Relationship("knowledgearticle_incidents");
    
                    orgService.Associate("incident", contextEntity.Id, newRelationship, relatedEntities);
    
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
        }
    }
    

    The association process is special, you can refer to this article for more details about it:

    https://www.inogic.com/blog/2018/04/how-to-associate-knowledge-article-with-incident-case-programmatically-in-dynamics-365/

    Result:

    pastedimage1609829391602v1.png

    In addition, the plugin should execute at PostOperation stage instead.

    pastedimage1609829442370v2.png

    Regards,

    Clofly

  • Sheershendu Profile Picture
    Sheershendu 40 on at
    RE: How to assign KBA to the Case entity based on Case title using Plugin ?

    Hi Clofly,

    I am using CRM online version.I am attaching the code of 2nd function.I switched to the replacement entity.Context entity name is Associated_Articles which is the schema name of the associated knowledge record of Case entity i.e where I want to populate the Knowledge Article after matching.

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using System;
    
    namespace KBAToCase
    {
        public class KBAPlugin : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                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"];
                    if (entity.LogicalName != "incident")
                    {
                        return;
                    }
                    if (context.Depth > 2)
                    {
                        tracingService.Trace("Context depth is: {0}", context.Depth);
                        return;
                    }
                    try
                    {
                        if (entity.Attributes.Contains("title"))
                        {
                            String caseTitle = (String)entity.Attributes["title"];
    
                            string[] caseWords = caseTitle.Split(null);
    
                            for (int i = 0; i < caseWords.Length; i  )
                            {
                                if (findKBA(service, caseWords[i], entity) == true)
                                {
                                    return;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        tracingService.Trace("Error of Plugin: {0}", ex.ToString());
                        throw;
                    }
                }
    
            }
    
            private static Boolean findKBA(IOrganizationService orgService, String caseWord, Entity contextEntity)
            {
                var query = new QueryExpression("knowledgearticle") { ColumnSet = new ColumnSet("title", "keywords") };
                query.Criteria = new FilterExpression();
                query.Criteria.AddCondition("keywords", ConditionOperator.Like, "%"   caseWord   "%");
    
                EntityCollection result = orgService.RetrieveMultiple(query);
    
                if (result.Entities.Count > 0)
                {
                    contextEntity["Associated_Articles"] = new EntityReference("knowledgearticle", result.Entities[0].Id);
                    return true;
                }
                else
                {
                    return false;
                }
    
            }
    
        }
    }
    

    After matching the Knowledge Article should populate here in the screenshot mentioned.

    5554.Screenshot-_2800_53_2900_.png

    Regards,

    Sheershendu

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: How to assign KBA to the Case entity based on Case title using Plugin ?

    Hi Sheershendu,

    Could you share steps so that I am able to reproduce your issue?

    In addition, please check if log file is available to download in business process error dialog for diagnostics.

    Last but not least, what CRM version you are using? Due to my environment is online: Knowledge Base Article entity is deprecated and replace with knowledge Article. So I only tested KA.

    If you are also working with the online, you may need to switch to the replacement entity instead.

    Regards,

    Clofly

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,307 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans