How to copy choice list values from one field to another field

Hareesha
Tera Contributor

I have 2 fields which are of choice list, Now requirement is to copy one field values to another field and also by eliminating duplicates.

2 ACCEPTED SOLUTIONS

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @Hareesha,

Please run this script in your background script:

 

var project = new GlideRecord('your table name');

project.addNotNullQuery('second field'); //query to check if the second field has empty value

project.query();

while (project.next())

{

project.second field = project.first field;

project.update();

}

 

Please mark it as correct based on impact.
BR,
Nayan

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

View solution in original post

Hello,

 

Please use the below script in background or fix script. I have already tried on my PDI works fine. Correctly replace the fieldname. Assuming you will copy values from fieldname1 to fieldname2. Also I hope fieldname1 and fieldname2 are referring to the same table

 

var gr = new GlideRecord('pm_project');
gr.addEncodedQuery('addyourquery');
gr.query();
while (gr.next()) {
var value = [];
value = gr.replacefieldname1.split(',');
value.push(gr.replacefieldname2.split(','));
var uniqueValue = new ArrayUtil().unique(value);
gr.replacefieldname2 = uniqueValue.toString();
gr.update();
}

 

Please mark my answer as correct based on Impact.

View solution in original post

8 REPLIES 8

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Please share screenshot so it would be easier to assis you.

 

Thanks.

Hi Saurav my requirement is not to get same field values from another table. My requirement is i have 2 fields in project form and there are some existing records. Now they are asking me to remove one field and copy whatever data is in the field to another field by eliminating duplicates. Both fields are of type list

Hello,

 

Please use the below script in background or fix script. I have already tried on my PDI works fine. Correctly replace the fieldname. Assuming you will copy values from fieldname1 to fieldname2. Also I hope fieldname1 and fieldname2 are referring to the same table

 

var gr = new GlideRecord('pm_project');
gr.addEncodedQuery('addyourquery');
gr.query();
while (gr.next()) {
var value = [];
value = gr.replacefieldname1.split(',');
value.push(gr.replacefieldname2.split(','));
var uniqueValue = new ArrayUtil().unique(value);
gr.replacefieldname2 = uniqueValue.toString();
gr.update();
}

 

Please mark my answer as correct based on Impact.

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @Hareesha,

Please run this script in your background script:

 

var project = new GlideRecord('your table name');

project.addNotNullQuery('second field'); //query to check if the second field has empty value

project.query();

while (project.next())

{

project.second field = project.first field;

project.update();

}

 

Please mark it as correct based on impact.
BR,
Nayan

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.