The CreatorCon Call for Content is officially open! Get started here.

Run fix script only on translated version

Diogo Luz
Tera Expert

Hi all,

 

Recently, I've found out that we've a typo in the description of several catalog items (they all contain a specific word in the name). The typo only occurs in the translated version (our instances only have two versions, dutch and english, and in english there's nothing to change). I've tried to run the following fix script but it doesn't seem to work. 

 

var records = new GlideRecord('sc_cat_item');
records.addQuery('name', 'CONTAINS', 'this word');
records.query();
while (records.next()) {
    var name = records.getValue('name');
    var description = records.getValue('description');
    var updatedDescription = description.replace('this', 'that');
    records.setValue('description', updatedDescription);
    records.update();
}

 What am I doing wrong?

 

Thanks in advance 🙂

6 REPLIES 6

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @Diogo Luz - That looks like it should work, but "name" appears to be unused in the while loop...so you can probably get rid of that line. Have you tried running it as a background script? Something like this:

var records = new GlideRecord('sc_cat_item');
records.addQuery('name', 'CONTAINS', 'this word');
records.query();

gs.print('Found ' + records.getRowCount() + ' records.');

while (records.next()) {
    var description = records.getValue('description');
    
    // Check if the word "this" exists in the description
    if (description && description.includes('this')) {
        gs.print('Original description: ' + description);
        
        // Replace "this" with "that"
        var updatedDescription = description.replace('this', 'that');
        records.setValue('description', updatedDescription);
        records.update();
        
        gs.print('Updated description: ' + updatedDescription);
    } else {
        gs.print('The word "this" was not found in the description for record: ' + records.sys_id);
    }
}

 

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

I should mention, if its the translated text you need to change, you would find the records in sys_translated_text and make the changes there.

Diogo Luz
Tera Expert

Thank you for a quick answer @Sheldon  Swift . Unfortunately your script (I tested as a background script) does not give me any better results. I also had a look on the sys_translated_text but didn't get what I want.

This translation part is what is really bothering me because I don't understand which table and fields should I change 😕

 

Thanks

The script would only change the original (English) version. Were you able to find the records in sys_translated_text? I believe the query would be something like like: /now/nav/ui/classic/params/target/sys_translated_text_list.do%3Fsysparm_query%3Dfieldname%253Ddescription%255Etablename%253Dsc_cat_item%255Elanguage%253Dnl

 

You would then update your script accordingly:

var records = new GlideRecord('sys_translated_text');
records.addEncodedQuery('fieldname=description^tablename=sc_cat_item^language=nl');
records.query();

gs.print('Found ' + records.getRowCount() + ' records.');

while (records.next()) {
    var description = records.getValue('value');
    
    // Check if the word "this" exists in the description
    if (description && description.includes('this')) {
        gs.print('Original description: ' + description);
        
        // Replace "this" with "that"
        var updatedDescription = description.replace('this', 'that');
        records.setValue('description', updatedDescription);
        records.update();
        
        gs.print('Updated description: ' + updatedDescription);
    } else {
        gs.print('The word "this" was not found in the description for record: ' + records.sys_id);
    }
}

 

If that doesn't work, there is a detailed community post you might find useful here: https://www.servicenow.com/community/international-localization/everything-you-ever-wanted-to-know-a...