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

Yes.

Have you checked manually searching for the user using the User Interface? So go to the table and enter =David Faroon in the filter.

Do you get the user that way?

yes , there is "David Faroon" .

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

any suggestions please.

You can remove the duplicate spaces using:

var gr = "David   Faroon";

gr = gr.trim().replace(/\s+/g,' ')

 

  • Trim removes spaces from start and end.
  • replace(/\s+/g,' ') replaces any number of spaces with a single space.

 

If there are spaces stored in your name field in the database, then you need to find right name and compare.

If you are using a static name "David Faroon" then check in user table find the right name and do the comparison.

If this is going to be dynamic, then from where are you fetching this name? Try to fetch it from the user table only from there as well.

gr = gr.trim().replace(/\s+/g,' ')

' ' - directing double space?