Background script to replace a value in incident table

Ravi kiran1
Tera Contributor

There are 2 tables u_organization (old table) and  security_organization (new table). Now incident are referring old table and need to change to new table as reference but old incident organization field value will be empty after changing reference table, is there a way we can update value for old incidents. Old table and new table value are almost same example old table has INDIA and new table IND as organization field value.

 

4 REPLIES 4

Saurabh Gupta
Kilo Patron
Kilo Patron

Can you have any relationship that can tell us for an old record which one is new record?


Thanks and Regards,

Saurabh Gupta

there are 2 different tables 

Yes,
If you want to replace the old values (old table)with new values (new table).

You should be knowing the relationship between the two tables so that accordingly script can be written to update the incidents.

 

 

 


Thanks and Regards,

Saurabh Gupta

jaheerhattiwale
Mega Sage
Mega Sage

@Ravi kiran1 As the reference field is nothing but a string field in backend. you can write the background script first and update the records. As still the field is referencing to old table you will see empty in that field. After moving change the reference of that field to new table.

 

Use below script:

var inc = new GlideRecord("incident");
inc.addQuery("<FIELD REFERNCING OLD TABLE HERE>ISNOTEMPTY");
inc.query();

while(inc.next()){
    var newTable = new GlideRecord("<NEW TABLE NAME HERE>");
    newTable.addQuery("<NEW TABLE FIELD NAME>", inc.<FIELD REFERNCING OLD TABLE HERE>.<OLD TABLE FIELD>.toString());
    newTable.query();

    if(newTable.next()){
        inc.<FIELD REFERNCING OLD TABLE HERE> = newTable.sys_id.toString();
        inc.update();
    }
}
 
Please mark as correct answer if this solves your issue.

 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023