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

user name fetching from another table. that table field is string field. sales team will enter the user name. but some times there is a change to add double space in name while entering name by mistakenly .

so while coming to script to compare that name with user table. first need to remove that double space, add single space only and compare name, create Resource Plan. 

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();

could you please help me what is different between

.replace(/\s\s/g,' ');

.replace(/\s+/g,' ');
 

\s\s we are checking for double space and replacing with single space.

your 2nd one replacing single space again with single space 🙂

 

@asifnoor that is incorrect.

Correct is:

.replace(/\s\s/g,' ');  Replaces exactly 2 spaces with 1.

.replace(/\s+/g,' '); Replaces 1 or more spaces with 1 space. So could be triple space or quadruple or any number of spaces and will replace it with 1.