Update multiple fields available in a array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2022 07:22 PM
I am moving data to a different instance (not related to set of our instances) , so there are same users with different sysids.
So , I want to update records with user info from source instance with users in target instance. As , there are multiple fields , its time consuming to write a script for each field.
I created a custom table , to map users from source instance to target instance
I'm thing of the following script to bulk update fields in one script.
var fieldsToBeUpdated = ["assigned_to","closed_by", "opened_by", "firm_lead"];
var arryLength = fieldsToBeUpdated.length;
var grProfile = new GlideRecord('x_ciamp_software_c_sc_profiles');
grProfile.query();
while (grProfile.next()) {
for (var i = 0; i < arryLength; i++) {
var grUserMap = new GlideRecord('x_ciamp_software_c_swc_user_mapping');
grUserMap.addQuery('swc_user_sysid', grProfile.assigned_to); // Need to Replace grProfile.assigned_to with grProfile[arryLength[i]] or something that works
grUserMap.query();
if (grUserMap.next()) {
if (grProfile.assigned_to != '') // // Replace grProfile.assigned_to with grProfile[arryLength[i]] or something that works
grProfile.assigned_to = grUserMap.user_in_target; // Replace grProfile.assigned_to with grProfile[arryLength[i]] or something that works
grProfile.update();
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2022 10:52 PM
Hi Pardhiv,
So, what is the question?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2022 12:13 AM
If the question is about updating fields in an array, following will update all fields in an array. I've moved the if condition to check if the value is null or not to addNotNullQuery() to avoid getting records that does not need to be updated.
var fieldsToBeUpdated = ["assigned_to", "closed_by", "opened_by", "firm_lead"];
for (var i = 0; i < fieldsToBeUpdated.length; i++) {
var grProfile = new GlideRecord('x_ciamp_software_c_sc_profiles');
grProfile.addNotNullQuery(fieldsToBeUpdated[i]);
grProfile.query();
while (grProfile.next()) {
var grUserMap = new GlideRecord('x_ciamp_software_c_swc_user_mapping');
if (grUserMap.get('swc_user_sysid', grProfile.getValue(fieldsToBeUpdated[i]))) {
grProfile.assigned_to = grUserMap.user_in_target; // Replace grProfile.assigned_to with grProfile[arryLength[i]] or something that works
grProfile.update();
}
}
}