How to associate Knowledge Article with Incident (Case) Programmatically in Dynamics 365

By | April 12, 2018

Introduction:

Microsoft has introduced a knowledge management system for Dynamics CRM 365. This enables users to create rich knowledge articles, which also supports embedding external multimedia content like images and videos in the form of links. It also allows translations and versioning for the articles.

In this blog will discuss about how to associate Knowledge Article with Incident (Case) programmatically.

Recently, we came across below scenario,

Where User has dropdowns of “Knowledge Article” and “Incident” and one custom button.

If user select values form dropdowns and clicked on the custom button then “Knowledge Article” should associate with selected “Incident”. But we cannot Associate Knowledge Article with Incident (Case) directly without creating custom relationships.

After some research and play around we found the below solution.

Solution:

We used OOB relationships to achieve this. In Dynamics CRM there is one intermediary entity called “Knowledge Article Entity” between Incident (case) and Knowledge Article.

“Knowledge Article Entity” entity have following 2 OOB relationships.

  1. Knowledge Article Entity -> Knowledge Article (N:1)
  2. Knowledge Article Entity -> Case (N:1)

How to associate Knowledge Article with Incident (Case) Programmatically in Dynamics 365

To perform above action, follow the below-mentioned steps:

Steps 1:

Create a new record or used existing record of Knowledge Article and Incident (Case) entity.

Steps 2:

Create new record on “Knowledge Article Entity” entity with following two value.

  1. Knowledge Article Lookup Value (step 1)
  2. Case Lookup Value (step 1)

Steps 3:

Associate newly created “Knowledge Article Entity” (created in step 2) entity record with Incident (case).

Refer the code below;

JavaScript Code:

//Object of “Knowledge Article Entity”
var kAIncident = new Object();

//Knowledge Article Lookup
kAIncident["knowledgearticleid@odata.bind"] = "/knowledgearticles(" + Knowledge Article ID+ ")";
//Case Lookup
kAIncident["incidentid@odata.bind"] = "/incidents(" + Case ID + ")";

crmAPI.Create("knowledgearticleincidents", kAIncident).then(function (result) 
{ 
if (isValid(result)) 
{
crmAPI.Associate("incidents", caseID, "knowledgearticle_incidents", "knowledgearticleincidents", result)
              }
     	});

Note : “knowledgearticle_incidents” is a N:1 of relationship between Incident (case) and Knowledge Article Entity

 C# Code:

Guid kAIncident_id = new Guid();
//Object of “Knowledge Article Entity”
Entity kAIncident = new Entity("knowledgearticleincident");

//Knowledge Article Lookup
kAIncident["knowledgearticleid"] = new EntityReference("knowledgearticle", new Guid(“Knowledge Article ID”));

//Case Lookup 
kAIncident["incidentid"] = new EntityReference("incident", new Guid("Case ID"));

kAIncident_id = _service.Create(kAIncident);


EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
        
relatedEntities.Add(new EntityReference("knowledgearticleincident", new Guid(kAIncident_id.ToString())));

Relationship newRelationship = new Relationship("knowledgearticle_incidents"); 

//Associate the knowledge article record with the Case record.         
_service.Associate("incident", new Guid("Case ID"), newRelationship, relatedEntities);

 

Conclusion:

Using solution described above the user can Associate Knowledge Article with Incident (Case) programmatically using OOB relationships in Dynamics 365.

Export Dynamics CRM Reports

2 thoughts on “How to associate Knowledge Article with Incident (Case) Programmatically in Dynamics 365

    1. Inogic

      Hi Sheershendu,

      You can use JavaScript code in Web resources like HTML, JavaScript and invoke the same on Load, on Save event of Form, and onchange event of fields.

      You can use C# code in a plugin, API (Azure Functions), Custom Action, and workflow assembly.

      Hope this answers your query,
      Thanks!

Comments are closed.