I am using this script in workflow , but i'm getting an error please check it?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 02:33 AM
Script :
function ifScript() {
try {
var ritmGR = new GlideRecord("sc_req_item");
if (!ritmGR.get(current.sys_id)) {
gs.error("Unable to get sc_req_item record with sys_id: " + current.sys_id);
return 'error';
}
var user = ritmGR.variables.requested_for;
// Check if the user has the 'itil' role
var gr = new GlideRecord('sys_user');
if (!gr.get(user)) {
gs.error("Unable to get sys_user record for user: " + user);
return 'error';
}
if (gr.hasRole('itil')) {
return 'yes';
} else {
return 'no';
}
} catch (e) {
gs.error("An error occurred: " + e);
return 'error';
}
}
// Call the function and store the result in the 'answer' variable
var answer = ifScript();
// Log the result to the script log
gs.info('Result: ' + answer);
5 REPLIES 5
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 02:36 AM
Hi @1dusjhyahnt ,
What is the error which u getting?
Any snip or can u paste the error here please.
Thanks,
Danish
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 02:36 AM
Hi Dushyant,
Could you please let me know your business requirement here.
Thanks.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 02:40 AM
Please replace your code with the below code. Mark helpful if this helped.
Code:
Code:
function ifScript() {
var user = ritmGR.variables.requested_for;
var gr = new GlideRecord('sys_user');
gr.get('sys_id', user);
if (gr.hasRole('itil')) {
return 'yes';
} else {
return 'no';
}
}
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 04:52 AM
Hi @1dusjhyahnt ,
Please try below code:
function ifScript() {
try {
var ritmGR = new GlideRecord("sc_req_item");
if (!ritmGR.get(current.sys_id)) {
gs.error("Unable to get sc_req_item record with sys_id: " + current.sys_id);
return 'error';
}else{
var user = ritmGR.variables.requested_for;
// Check if the user has the 'itil' role
var gr = new GlideRecord('sys_user');
if (!gr.get(user)) {
gs.error("Unable to get sys_user record for user: " + user);
return 'error';
}
if (gr.hasRole('itil')) {
return 'yes';
} else {
return 'no';
}
}
} catch (e) {
gs.error("An error occurred: " + e);
return 'error';
}
}
// Call the function and store the result in the 'answer' variable
var answer = ifScript();
// Log the result to the script log
gs.info('Result: ' + answer);
Thanks,
Danish
