Replace string using regex

carlocsa
Kilo Expert

Hi ServiceNow Gurus,

I have a reference field which we will call Field A. The field value is always "Digital" + "Value provided by requester". This will be saved to a table.

On load, I want to remove the word "Digital" and save it on a separate text field, called Field B.


Example:Field A: Digital Technology Advertisement

Field B: Technology Advertisement

My catalog client script is displaying the Field A sys_id to Field B.

function onLoad() {

var str = g_form.getValue('u_field_a');   // get value from Field A

var regex = str.toString().replace('Digital ', '');

g_form.setValue('u_field_b',regex); // save new value to Field B

}

Please help!

Thank you.


Carlo

1 ACCEPTED SOLUTION

My bad, i didn't notice that it's returning the sys_id. Can you please something like this. Let's see if that helps.



function onLoad() {


g_form.setValue('u_field_b', g_form.getDisplayBox('u_field_a').value.substring(8));


}


View solution in original post

4 REPLIES 4

Shishir Srivast
Mega Sage

please try with,



var regex = str.substring(8);


My bad, i didn't notice that it's returning the sys_id. Can you please something like this. Let's see if that helps.



function onLoad() {


g_form.setValue('u_field_b', g_form.getDisplayBox('u_field_a').value.substring(8));


}


Works perfectly!


Thank you!


Mihir Mohanta
Kilo Sage

Seems like field A is a reference type of field. Thats why sys_id is returning while you are using getValue method for field A.



In that case you have to use GlideAjax, GlideRecord or a getReference method to get the display value of field A.



Please go through the below link for an example.



Client Script returning sys_id instead of actual value.