- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2020 03:15 AM
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 ?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2020 04:14 AM
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();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2020 04:35 AM
Ah right mate.
yea I missed that + in the 2nd line.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2020 06:53 AM
Thanks Willem. thanks for clarification
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2020 04:20 AM
Have you tried the scripts I provided?
So combined all my suggestions:
var userName = "David Faroon";
userName = userName.trim().replace(/\s+/g,' ');
var usergr = new GlideRecord('sys_user');
usergr.addQuery('name', userName);
usergr.query();
if (usergr.next()) {
var rp = new GlideRecord('resource_plan');
rp.initialize();
rp.user_resource = usergr.getUniqueValue();
rp.setWorkflow(false);
rp.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2020 03:35 AM
Are you sure your issue is with getting the user?
Inserting without the mandatory fields is normally prevented. You can add setWorkflow(false):
var userName = "David Faroon"
var usergr = new GlideRecord('sys_user');
usergr.addQuery('name', userName);
usergr.query();
if (usergr.next()) {
var rp = new GlideRecord('resource_plan');
rp.initialize();
rp.user_resource = usergr.getUniqueValue();
rp.setWorkflow(false);
rp.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2020 03:36 AM
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();
}