Script to find a replace one text with another from description field of multiple records

Gary22
Tera Contributor

Hi All 

i have a typical requirement for which i need to write a script . 

i have to find all incidents ( table name : incident ) where short description ( short_description) contails the word " test environment " and replace it with "dev environment" .

Please note that i dont have to replace the whole short description field , but only one particular text with another . 

Can someone please help me with the script for this requirement .

Thanks a lot in advance 

Regards 

GARY

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

Hello ,

you can try this 

var inc =new GlideRecord("incident");
inc.addEncodedQuery("short_descriptionCONTAINStest environment");
inc.query();
while(inc.next())
{
if(inc.short_description.indexOf('test environment')>-1)
{
var replaced =inc.short_description.replace("test environment", "Dev environment");
inc.short_description =replaced;
inc.update();
}

}

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hi Gary,

You can use regular expressions:

replace("old", "new"); -> this will replace only first occurrence of old to new.

Example : 

(function executeRule(current, previous /*null when async*/) {

	current.body = current.body.replace(/mailto:itaddress@/g, 'mailto:hraddress@');
	
})(current, previous);

Just replace body with "Short Description" field name for your requirement.

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

Harish KM
Kilo Patron
Kilo Patron

Hi you can use the below script


var gr =new GlideRecord("incident");
gr.addQuery("short_description",'CONTAINS',"test environment");
gr.query();
while(gr.next())
{
gs.info(gr.number);

gr.short_description = gr.short_description.replace("test environment", "Dev environment");
gr.update();
}

Regards
Harish

Mohith Devatte
Tera Sage
Tera Sage

Hello ,

you can try this 

var inc =new GlideRecord("incident");
inc.addEncodedQuery("short_descriptionCONTAINStest environment");
inc.query();
while(inc.next())
{
if(inc.short_description.indexOf('test environment')>-1)
{
var replaced =inc.short_description.replace("test environment", "Dev environment");
inc.short_description =replaced;
inc.update();
}

}

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU