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

Although this would not give an error, it would actually return the user, not the u_role_type. Just try like I mentioned, = tested, works.

Is this also your fullcode? Doesn't feel like it. I would expect also 

(function execute() {
 
}
 
Or are you testing this in background script? Then ofcourse this will not work. vaInputs is Virtual Agent specifically.
 
If my answer helped you in any way, please then mark it as helpful.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Hi Mark,

Please find my below code.

(function execute() {
gs.info('>>> 1: ' + vaContext.portal);
//var roletype = gs.getUser().getRecord().getValue('u_person_role_type_code');
var roletype = vaInputs.user.u_person_role_type_code;

gs.info('>>> 2: ' + roletype);
if(!vaContext.portal) {
    return false;
}
if(vaContext.portal == 'hrportal' && roletype == 'EMP'){
    return true;
}
})()

Code looks the same here, I'm only testing with a different custom field.

So with this code in your Topic condition you are getting the vaInputs error while opening the Virtual Agent?

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

I understand i have provided Test custom field previously.Now this is my exact code i am working on 

i tried with glide record also:

(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;
}
 
})()
 
@@@@@@@@@@@@@@@@
When i tried in background : It is giving correct roletype
 
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);
}

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