Want to remove a prefix from a field

Poorva Bhawsar
Mega Sage

Hi Community,

 

I want to remove 'ROCG' keyword from a field which contains values as ROCGTPO12344 etc in this field. 

 

I have written a BR but its throwing some error.

var record = new GlideRecord('xyz');
record.query();
while (record.next()) {
 
        record.abc= record.abc.replace('ROCG', '');
        record.setWorkflow(false); //Do not run business rules
        record.autoSysFields(false); //Do not update system fields
        record.update();
    }
 
But its throwing error when i am replacing it.
 
Thanks
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Poorva Bhawsar 

use this

var record = new GlideRecord('xyz');
record.query();
while (record.next()) {

record.abc= record.abc.toString().replace('ROCG', '');
record.setWorkflow(false); //Do not run business rules
record.autoSysFields(false); //Do not update system fields
record.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Chaitanya ILCR
Kilo Patron

Hi @Poorva Bhawsar ,
replace method doesn't exist on the Object type 
it is available on String type you can you .toString() or .getValue() from the GlideRecord these methods return string type

var record = new GlideRecord('xyz');
record.query();
while (record.next()) {

    record.abc = record.getValue('abc').replace('ROCG', '');
    record.setWorkflow(false); //Do not run business rules
    record.autoSysFields(false); //Do not update system fields
    record.update();
}

Regards,
Chaitanya

Runjay Patel
Giga Sage

Hi @Poorva Bhawsar ,

 

Code looks okay to me, i checked it with running background script and working fine.

var record = new GlideRecord('xyz');
record.query();
while (record.next()) {
    // Ensure the field is not null and is a string
    if (!gs.nil(record.abc) ) {
        record.abc = record.abc.replace('ROCG', ''); // Replace 'ROCG' with an empty string
        record.setWorkflow(false); // Skip business rules
        record.autoSysFields(false); // Skip system fields updates
        record.update();
    }
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

rambo1
Tera Guru

@Poorva Bhawsar 

Try below line of code in your code:

record.abc = record.abc.toString()replace(/ROCG/g, '');