Fix script.

Thalavai m
Tera Contributor

Hi All,

we are having a requirement that we have to create a new field and copy records for old field to the new field and make the old field Inactive we have tried to copy the records using Fix script but its not copying the records can anyone help me to create a script please. 

this is the script we have tried. 

 

var gr = new GlideRecord('cmdb_ci_business_app');
gr.addEncodedQuery('u_ug_rtoISNOTEMPTY^u_ug_rpoISNOTEMPTY');
gr.query();
while (gr.next()) {
        gr.setValue('u_rto', 'u_ug_rto');
    gr.setValue('u_rpo', 'u_ug_rpo');
        gr.update();
    }

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Thalavai m ,

 

Please try below code. Hope both field type are same in ur table

var gr = new GlideRecord('cmdb_ci_business_app');
gr.addEncodedQuery('u_ug_rtoISNOTEMPTY^u_ug_rpoISNOTEMPTY');
gr.query();
while (gr.next()) {
        gr.u_rto = gr.u_ug_rto; \\ this line will copy the value from 1 field to another
    gr.u_rpo = gr.u_ug_rpo;
gr.u_ug_rto = ''; \\post copying it will make the other field empty
gr.u_ug_rpo = '';
        gr.update();
    }

 

Thanks,

Danish

View solution in original post

5 REPLIES 5

Muhammed Medni
Tera Guru

Hi @Thalavai m 

var gr = new GlideRecord('cmdb_ci_business_app');
gr.addEncodedQuery('u_ug_rtoISNOTEMPTY^u_ug_rpoISNOTEMPTY');
gr.query();

while (gr.next()) {
    var oldValue = gr.getValue('your_old_field');
    gr.setValue('your_new_field', oldValue);
    gr.update();
}

var oldField = 'your_old_field';
var field = new GlideRecord('sys_dictionary');
field.addQuery('name', 'your_table_name');
field.addQuery('element', oldField);
field.query();
if (field.next()) {
    field.setValue('inactive', true);
    field.update();
}