Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Background Script to remove character at end

purdue
Kilo Sage

Hello,

 

I need to write a background script to remove _) from end of serial number in alm_hardware.   Any assistance is appreciated.

Thanks,

Chad

 

1 ACCEPTED SOLUTION

Jyoti Jadhav9
Tera Guru

Hi @purdue ,

 

You can try below background scripts:

Script 1:

var al = new GlideRecord("alm_hardware");
al.addEncodedQuery("serial_numberENDSWITH_)");
al.query();
while(al.next()){
gs.print("Before Update:"+al.getValue("serial_number"));
var str = al.getValue("serial_number").toString();
var str1 = str.split('_');
gs.print("After Update:"+str1[0]);
al.setValue('serial_number',str1[0]);
al.setWorkflow(false);
al.autoSysFields(false);
al.update();
}

 

OR

 

Script 2:

var al = new GlideRecord("alm_hardware");
al.addEncodedQuery("serial_numberENDSWITH_)");
al.query();
while(al.next()){
gs.print("Before Update:"+al.getValue("serial_number"));
var str = al.getValue("serial_number").toString();
var str1 = str.replace(/[^a-zA-Z0-9]/g, '')
gs.print("After Update:"+str1);
al.setValue('serial_number',str1);
al.setWorkflow(false);
al.autoSysFields(false);
al.update();
}

 

Please hit the like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

 

Thanks & Regards

Jyoti Jadhav

View solution in original post

5 REPLIES 5

Hello Jyoti,

 

I tried number 2 and it works.   May I ask str.replace (/[^a-zA-Z0-9]/g, '').  This looks like regex.   Can you explain what it is doing?  Thanks a bunch.  Chad