Requirement to check if the user have access to the knowledge article showing in the related search

EzhilB
Tera Contributor

I got a requirement to show the attach button for the knowledge articles showing in the related search based on user having access to the knowledge article.

 

I have tried with the UI action visibility by using the below script.

 

var kb_article=resource.sys_id;
var requester=current.caller_id;

function kbAccess(kba) {
var kbGr = new GlideRecord('kb_knowledge');
kbGr.get(kba);
return (new KBKnowledge().canRead(kbGr));
}

var userToCheck = requester;
var originalUser = gs.getSession().impersonate(userToCheck );
var hasAccessToKBA = kbAccess(kba);
gs.getSession().impersonate(originalUser );
visible=hasAccessToKBA;
 
But we have used impersonate to check if the user have access to the knowledge article seems it is not working. Please suggest any alternate way to check the access if the requester of the incident have access to the knowledge article getting attached in the incident.
1 ACCEPTED SOLUTION

Hi @EzhilB 
please accept the solution .. and mark as helpful

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

3 REPLIES 3

Ravi Gaurav
Giga Sage
Giga Sage

Hi @EzhilB 

 

 

  • Retrieve the Knowledge Article and Requester:
    Use the sys_id of the knowledge article and the requester's sys_id from the incident.

  • Check Access:
    Use the KBKnowledge API's canRead() method with the user_id parameter to check access.

  • Set Visibility:
    Determine if the Attach button should be shown based on the access check.

    Updated Script :-

var kbArticleSysId = resource.sys_id; // sys_id of the knowledge article
var requesterSysId = current.caller_id; // sys_id of the requester (incident caller)

function hasAccessToKnowledgeArticle(kbSysId, userSysId) {
var kbGr = new GlideRecord('kb_knowledge');
if (kbGr.get(kbSysId)) {
// Check if the user has access to the knowledge article
return new KBKnowledge(kbGr).canRead(userSysId);
}
return false;
}

// Check access
var hasAccess = hasAccessToKnowledgeArticle(kbArticleSysId, requesterSysId);

// Set visibility
visible = hasAccess;

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Hi Ravi,

 

Thanks For your help it works.

Hi @EzhilB 
please accept the solution .. and mark as helpful

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/