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 come across below scenario,
Where, User have dropdowns of “Knowledge Article” and “Incident” and one custom button.
If user select values form dropdowns and clicked on custom button than “Knowledge Article” should be 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.
- Knowledge Article Entity -> Knowledge Article (N:1)
- Knowledge Article Entity -> Case (N:1)
To perform above action, follow the below mentioned steps:
Steps 1:
Create 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.
- Knowledge Article Lookup Value (step 1)
- 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 user can Associate Knowledge Article with Incident (Case) programmatically using OOB relationships in Dynamics 365.