Add functionality on opening of a knowledge article using the addOnResultOpened API

addOnResultOpened Client API Reference:

Use Case:

// A namespace defined for New

var New = window.New || {};



New.KBSearchOnResultOpened = function (executionContext) {

try {

//Getting the Form Context Object

var formContext = executionContext.getFormContext();

//Getting the knowledge base search control

var myKBSearchControl = formContext.getControl('Account_KBArticles');

//Calling addOnResultOpened function

myKBSearchControl.addOnResultOpened(New.OnResultOpened);

}

catch (err) {

console.log("Inside Catch -> " + err.message);

}

}



//Function that need to be called when knowledge article is opened from knowledge search result

New.OnResultOpened = function () {

var confirmStringsMessage = { text: "Do you require any assistance?", title: "Confirmation Dialog" };

var confirmMessageOptions = { height: 200, width: 450 };



Xrm.Navigation.openConfirmDialog(confirmStringsMessage, confirmMessageOptions).then(

function (success) {

if (success.confirmed) {

//Getting logged in user's userid

var userID = Xrm.Page.context.getUserId().replaceAll("{", "").replaceAll("}", "");

//getting username of logged in user

var userName = Xrm.Page.context.getUserName();

//getting the current record ID

var currentRecordID = Xrm.Page.data.entity.getId().replaceAll("{", "").replaceAll("}", "");

console.log("userID : " + userID + " and userName : " + userName);



//creating data object which is used to passed as parameter while creating email activity

var data = {

"description": "Knowledge Base Article has beed viewed by user - " + userName, ///Email Body

"regardingobjectid_account@odata.bind": "/accounts(" + currentRecordID + ")",

"subject": "Knowledge Base Search Article bas been viewed by somenone",

"email_activity_parties": [

{

"partyid_systemuser@odata.bind": "/systemusers(" + userID + ")",

"participationtypemask": 1 //From Email

},

{

"partyid_account@odata.bind": "/systemusers(6a6b321f-e897-ec11-b400-000d3aca14a7)",

"participationtypemask": 2 //To Email

}

]

};



//Creating the Email activity record

Xrm.WebApi.createRecord("email", data).then(

function success(result) {

console.log("Email has been created with ID : " + result.id);

},

function Error(e) {

console.log("New.OnResultOpened -> " + e.message);

}

)

}

else {

console.log("Confirmation dialog has been closed by close button.");

}

});

}

Demonstration:

Conclusion:

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store