To remove apostrophe on the leading number

Nur1
Tera Contributor

Hi Community,

 

Currently I'm facing issue where some of the employee number having apostrophe at front of the number e.g '1234

But some of employee number there's no apostrophe.

 

How to write a script in background script to remove the apostrophe at the leading number ?

 

Thanks.

2 REPLIES 2

Jake Sadler
Kilo Sage

Hi @Nur1,

 

This script will check the user table for users with an employee number that starts with '

It then loops through and removes the apostrophe from the start of the number and updates the record.

 

Please make sure to test this in a sub prod environment

 

var gr = new GlideRecord("sys_user");

gr.addEncodedQuery("employee_numberSTARTSWITH'");

gr.query();

 

while(gr.next()){

gr.employee_number = gr.employee_number.substring(1);

gr.update();

}

Robbie
Kilo Patron
Kilo Patron

Hi @Nur1,

 

Please find the below script that can be used in both a background script as well as perhaps an after Business Rule to prevent this from happening moving forward and after the update,

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

 

//var encQ = "user_name=abel.tuter^employee_numberSTARTSWITH'"; //A test parameter to test against a single user before running against all records
var encQ = "employee_numberSTARTSWITH'";
var userGR = new GlideRecord('sys_user');
userGR.addEncodedQuery(encQ);
userGR.query();
while(userGR.next()){
	//gs.print('BEFORE: ' + userGR.employee_number);
	userGR.employee_number = userGR.employee_number.replace(/'/,'');
	//gs.print('AFTER: ' + userGR.employee_number);
    userGR.setWorkflow(false); //comment this line if you don't want to trigger other business rules or logic associated with a User record update
	userGR.update();
}