- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2022 02:57 AM
Hi,
I was testing this code in Bg Script and it was not executing getting the error ,
com.glide.script.RhinoEcmaError: "getRoles" is not defined.
Basically trying to pass user defined values to a function and check whether that record is present in the table or not.
var role_new = 'it_executive';
var user_new = '46efa3701b14f010a240ebd56e4bcbd4';
gs.info(getRoles(role_new,user_new));
getRoles: function(role,user)
{
var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('role.name='+role+'^user='+user);
gr.query();
if(gr.hasNext())
{
return "found";
}
}
Any help on what is wrong is appreciated. Thanks:)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2022 03:03 AM
Use
function getRoles(role,user) { var gr = new GlideRecord('sys_user_has_role'); gr.addEncodedQuery('role.name='+role+'^user='+user); gr.query(); if(gr.hasNext()) { return "found"; } }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2022 03:03 AM
Use
function getRoles(role,user) { var gr = new GlideRecord('sys_user_has_role'); gr.addEncodedQuery('role.name='+role+'^user='+user); gr.query(); if(gr.hasNext()) { return "found"; } }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2022 03:04 AM
Hi
in an background script you have to define a function first before using it.
Try this version:
function getRoles(role,user) {
var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('role.name='+role+'^user='+user);
gr.query();
if(gr.hasNext()) {
return "found";
}
}
var role_new = 'it_executive';
var user_new = '46efa3701b14f010a240ebd56e4bcbd4';
gs.info(getRoles(role_new,user_new));
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2022 03:12 AM
what is the diff in the syntax? Do I have to use the same syntax like function function-name() in script include also. But I have seen in script include functions getting defined as,
function-name : function(){
}
What is the difference?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2022 03:26 AM
Hi
in a Script Include the notation has to follow the object pattern:
{
key1 : value,
key2 : value2
}
And also functions are a special kind of values.
In a "normal" JavaScript you have to use the notation I have provided:
function getRoles(role,user) {
}
Kind regards
Maik