How to copy values from one field to another field with referring to different tables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2024 12:52 AM
Hi Team,
Am try to copy the values from one list field to another list field. one list field is referencing to sys_user table and another list field referencing to custom table. The sys_user table and custom table having same records but different sys_ids.
The script i used was copying values correctly but not matching the sys_id.
how to match sys_ids ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2024 01:01 AM
Hi @Divya57,
If you are trying to copy the data based the sys_user table then you should do a gliderecord query based on userID/email instead of matching sys_id between the two tables. Try this way and let me know if you need more help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2024 01:17 AM
Another important point you can always remember, sys_id of two different table records will never match. sys_id is unique ID for each record.
Refer the docs to know more about the sys_id in ServiceNow:
Unique record identifier (sys_id)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2024 01:18 AM
Hi @Divya57
To match the sys_ids when copying values from one list field (referencing the sys_user table) to another list field (referencing a custom table), you'll need to map the sys_id of the sys_user record to the sys_id in your custom table.
Here’s an approach to achieve this in ServiceNow:
Step-by-Step Solution
-
Create a Mapping: Create a mapping between sys_ids in sys_user and sys_ids in your custom table. You can do this by querying both tables and using a common field, such as name or email, to match records.
-
Script for Mapping: The following example assumes you’re using a common field (user_name) to match between sys_user and the custom table records:
// Initialize the mapping object var userMapping = {}; // Query the custom table to map names to sys_ids var customTableGR = new GlideRecord('your_custom_table'); customTableGR.query(); while (customTableGR.next()) { userMapping[customTableGR.user_name.toString()] = customTableGR.sys_id.toString(); } // Now, query the sys_user table and copy matching sys_ids to the new list field var userGR = new GlideRecord('sys_user'); userGR.addQuery('sys_id', 'IN', sourceListField); // assuming sourceListField is the list of sys_user sys_ids userGR.query(); var targetSysIds = []; while (userGR.next()) { var customSysId = userMapping[userGR.user_name.toString()]; if (customSysId) { targetSysIds.push(customSysId); } } // Set the target list field to the matching sys_ids in the custom table targetListField = targetSysIds.join(',');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-28-2024 11:14 PM
@Ravi Gaurav
With few changes above script is working fine. Now, how to update these values in list field ?