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:

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

Regards,
Clofly