How to copy values from one field to another field with referring to different tables

Divya57
Tera Expert

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 ?

5 REPLIES 5

Rajesh Mushke
Mega Sage
Mega Sage

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



Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

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)

 



Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

Ravi Gaurav
Giga Sage
Giga Sage

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

  1. 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.

  2. 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(',');
--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

@Ravi Gaurav 
With few changes above script is working fine. Now, how to update these values in list field ?