g_form.setValue works in classic form view but not work in workspace

DevpNew
Tera Contributor

I would like to add a new field in a hr case under workspace. The field can only accept exactly 8 integer numbers. A alert will pop up and then clear the field input value if input value doesn't pass validation.

Therefore, I created a new field 'new_position_number' in table 'sn_hr_core_case'. The type of field is String with max length=8. And created a client script as below to restrict the user to input 8 integer numbers. 

 

I tested the client script in Workspace and it works for the first time. Once user input a value that didn't pass the validation, an alert popped up and then field value was cleared. The case in workspace changed the state to 'You have unsaved changes in workspace'. However, when I tested the script again, the alert could pop up but system didn't clear the field value. While this client script always works in classic form view.

How do I modify the script to make it work in Workspace? thanks.

 

client script:

UI Type is All

Isolate script is checked

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   var regExTest = /(^[0-9]{8}$)/;

	if(!regExTest.test(newValue)){
		alert("Please enter exactly 8 numbers");
		g_form.setValue('new_position_number', ' ');
	}
}
 
5 REPLIES 5

saurabh_dubey
Kilo Sage

If it is Tokyo release, 

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1220159 this is the support article this might help.

Yes, checked Ui type all and checked isolate script.

chetanb
Tera Guru

@DevpNew  Can you please confirm you are on which version as this is existing problem PRB1632777 going to fix in Vancouver version.

 

As a workaround

 

you can use -

setTimeout(function () { g_form.setValue('new_position_number', ' '); }, 100);

 

instead of g_form.setValue('new_position_number', ' ');

 

if my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Regards,

CB

DevpNew
Tera Contributor

Hi, I am using

Build name: Vancouver
Build date: 01-09-2024_1214
Build tag: glide-vancouver-07-06-2023__patch4-hotfix1a-12-29-2023

 

I tried setTimeout(function () { g_form.setValue('new_position_number', ' '); }, 100); it didn't work in workspace. Alert could pop up but the field value never cleared, even for the first time.

However if I use g_form.setValue('new_position_number', ' '); the field value could be cleared for the first time, but didn't work afterwards.

Thanks.