Error in Background script

RFJ1
Tera Contributor

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:)

1 ACCEPTED SOLUTION

Community Alums
Not applicable

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"; } }

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

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"; } }

Maik Skoddow
Tera Patron
Tera Patron

Hi @RFJ 

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

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?

 

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