- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2023 08:03 AM
How to set "Assigned To" field in CMDB_CI_Computer based on Username mentioned in Custom field of same Table?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-21-2023 08:45 PM
Issue Resolved.
Please find below Code:-
Script Include:-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-21-2023 06:40 AM
See below, change the 'owned_by' to the name of your custom field. You can remove the 'alert()' line I used to debug.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-21-2023 08:37 AM
the above will work if your custom Username field is defined as a Reference field to the sys_user table. If not, but will match a record in the sys_user table 'name' field, then the client script changes to use GlideAjax, and a script include with logic to search the sys_user table based on the 'name' field matching the custom Username value will be needed. It will return the sys_id value of the record that the client script will then set the 'assigned_to' value on the cmdb_ci_computer table. The solution here depends on what is contained in the custom username field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-21-2023 10:37 AM
Hi @VIKAS45 ,
Hope you are doing great.
To set the "Assigned To" field in the CMDB_CI_Computer table based on the username mentioned in a custom field:
-
Create a Business Rule or Script in ServiceNow.
-
Use a server-side script to retrieve the username from the custom field.
-
Query the User table to find the corresponding User record based on the username.
-
Set the "Assigned To" field in the CMDB_CI_Computer record to the User record you found.
Below is reference script for same :
(function() {
// Get the current CMDB_CI_Computer record
var computerRecord = new GlideRecord('CMDB_CI_Computer');
computerRecord.get(current);
var username = computerRecord.getValue('custom_username');
// Query the User table to find the User record
var userRecord = new GlideRecord('sys_user');
if (userRecord.get('user_name', username)) {
// Set the 'Assigned To' field to the User record
computerRecord.setValue('assigned_to', userRecord.sys_id);
}
computerRecord.update();
})();
Regards,
Riya Verma