- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2024 08:54 AM
We have some assets that have email addresses in a text field "assignmentPlaceholder". I'm trying to create a script to loop though these assets, search the text email in sys_user.email, and apply the user record to "assigned to". I've tried the following script, which seems to not query the sys_user table, as it is applying the text "sys_user" as the assigned to:
var asset = new GlideRecord('alm_hardware');
//Query for specific asset for testing
asset.addQuery('serial_number','123456');
asset.query();
while(asset.next()){
if (asset.assigned_to.nil()){
var user = new GlideRecord('sys_user');
user.addQuery('email', asset.assignmentPlaceholder);
user.query();
asset.assigned_to = user;
asset.update();
}
}
The issue seems to be how/when I'm searching for the user. Using 'user.sys_id' fails completely and doesn't update the record, and using 'user' applied the text "sys_user" to assigned to:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2024 10:28 AM - edited ‎10-01-2024 10:29 AM
Update the script as below and check if it works:
var asset = new GlideRecord("alm_hardware");
//Query for specific asset for testing
asset.addQuery("serial_number", "123456");
asset.query();
while (asset.next()) {
if (asset.assigned_to.nil()) {
var user = new GlideRecord("sys_user");
user.addQuery("email", asset.assignmentPlaceholder);
user.query();
if (user.next()) {
asset.assigned_to = user.getUniqueValue();
asset.update();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2024 10:38 AM
The assigned_to assignment value should definitely be
asset.assigned_to = user.sys_id;
You are not returning a record in the user GlideRecord, so after user.query() you should have
if (user.next()) {
asset.assigned_to = user.sys_id;
asset.update();
}
}
}
You may also still not be returning the intended user record. assignmentPlaceholder is not the name of an out of the box field on the alm_hardware table. If you have added this field in your environment, check the column name - perhaps it is u_assignmentplaceholder? If a GlideRecord addQuery cannot resolve, it will be ignored, so a random user record will be returned instead of the one with a matching email.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2024 10:28 AM - edited ‎10-01-2024 10:29 AM
Update the script as below and check if it works:
var asset = new GlideRecord("alm_hardware");
//Query for specific asset for testing
asset.addQuery("serial_number", "123456");
asset.query();
while (asset.next()) {
if (asset.assigned_to.nil()) {
var user = new GlideRecord("sys_user");
user.addQuery("email", asset.assignmentPlaceholder);
user.query();
if (user.next()) {
asset.assigned_to = user.getUniqueValue();
asset.update();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2024 10:38 AM
The assigned_to assignment value should definitely be
asset.assigned_to = user.sys_id;
You are not returning a record in the user GlideRecord, so after user.query() you should have
if (user.next()) {
asset.assigned_to = user.sys_id;
asset.update();
}
}
}
You may also still not be returning the intended user record. assignmentPlaceholder is not the name of an out of the box field on the alm_hardware table. If you have added this field in your environment, check the column name - perhaps it is u_assignmentplaceholder? If a GlideRecord addQuery cannot resolve, it will be ignored, so a random user record will be returned instead of the one with a matching email.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2024 11:08 AM
Ah okay, I assumed since there would only be a single record, it would have worked. Make sense because how would it know one vs many was the result.
Also, you're correct with the field name. "assignedPlaceholder" isn't the actual name of the field, and the real one does start with "u_". What our field was named was confusing, so I used a clearer name in this example.