How to restrict virtual agent topic visible only for permanent employees

kamer
Kilo Expert

Hi Team,

I have few hr topics which i need to restrict those for only permanent employees.we have custom field created on user table as "Role type" which is a choice field of contract and permanent.when logged in user is permanent employee then only my topic should visible. 

I have added script in condition of the topic to available only for hr portal.Now how to add my script to fetch the user table field and check if logged in user is permanent employee then show this topic

condition:

(function execute() {
if(!vaContext.portal) {
    return false;
}

if(vaContext.portal == 'hr'){
    return true;
}
})()
1 ACCEPTED SOLUTION

Hi Mark,

Below code is working fine now.I did with GlideRecord.may be it didn't get published the topic properly.

(function execute() {
gs.info('>>> 1: ' + vaContext.portal);
var gr=new GlideRecord('sys_user');
gr.addQuery('sys_id',gs.getUserID());
gr.query();
if (gr.next()){
var roletype=gr.getValue('u_person_role_type_code');
gs.info("roletype "+roletype);
}
gs.info('>>> 2: ' + roletype);
if(!vaContext.portal) {
    return false;
}
if(vaContext.portal == 'hrportal' && roletype == 'EMP'){
    return true;
}
})()
 
 
 
Thanks a lot 

View solution in original post

19 REPLIES 19

Mark Roethof
Tera Patron
Tera Patron

Hi there,

You could add a GlideRecord query or getRecord on the current user to obtain that custom field. With that, just add an if condition to check the custom field.

Would that help?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

For example:

var role_type = gs.getUser().getRecord().getValue('u_role_type');

if(role_type == 'something') {
	return true;
}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark ,

Thank you for the quick response,but it is not working for me.please find the below code for your reference.

(function execute() {
var role_type = gs.getUser().getRecord().getValue('u_role_type');
if(!vaContext.portal) {
    return false;
}
if(vaContext.portal == 'hr' && role_type == 'EMPLOYEE'){
    return true;
}
})()

 

Add some debugging to verify where your script stops. Add some debugging to see if the variables used, actually contain the expected values. For example debug: role_type.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn