How to query user name

Sironi
Kilo Sage

Hi,

Some one help me in GlideQuery.

var gr= "David Faroon"

var rp = new GlideRecord('resource_plan');
rp.initialize();
var usergr = new GlideRecord('sys_user');
usergr.addQuery('name', gr);
usergr.query();
if (usergr.next()) {
rp.user_resource = usergr.sys_id;
}
rp.insert();

 

is it right way to compare name with User table, if yes then ResourcePlan not created . any suggestions ?

1 ACCEPTED SOLUTION

okay.

So try it like this.

 

var gr = "David   Faroon";
gr = gr.trim().replace(/\s+/g,' ');
var rp = new GlideRecord('resource_plan');
rp.initialize();
var usergr = new GlideRecord('sys_user');
usergr.addQuery('name',gr);
usergr.query();
  if (usergr.next()) {
    gs.info("Entered into your if condition");
    rp.user_resource = usergr.getValue("sys_id");
  }
rp.setWorkflow(false);
rp.insert();

View solution in original post

32 REPLIES 32

Hi

If there are mandatory fields, you cannot do the insert without filling them.

Try to run yoru query in background script and check the output. you might get some errors.

Also, add this line

usergr.setWorkflow(false) before insert line and test again.

i m trying from Scheduled Job.

can we use "Contains" because sometimes there may be some space in value of variable 

is it right format ?

 

var gr= "David Faroon"

var usergr = new GlideRecord('sys_user');
usergr.addQuery('name', 'CONTAINS', gr);
usergr.query();
if (usergr.next()) {
var rp = new GlideRecord('resource_plan');
rp.initialize();
rp.user_resource = usergr.getUniqueValue();
rp.insert();
}

Yes, the usergr.addQuery('name', 'CONTAINS', gr); is in the right format.

 

Reference:

https://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/

variable getting user name from another source. in this case

some times variable value may contains double space like "David   Faroon" , so how to query this time? 

any suggestions please.

Hello Sironi,

Can you tell me your complete requirement?

is David Faroon a static or in scheduled job you are fetching from any other table?