Client scripts working in form view, but not in the workspace.

C_dric Chen
Tera Contributor

Hello, people. It is I, the n00b who encounters all kinds of peculiar situations.

 

In an app that I'm developing, I have a custom table with fields "Alpha", "Beta", and "Caesar". Two onChange client scripts are supposed to update Caesar whenever Alpha or Beta changes. The issue is: These client scripts work fine when I try to create a new record in the bare form view, but not when I try to create a new record in the workspace.

 

These two client scripts have the same codes. One is triggered by the change in "Alpha", the other by the change in "Beta".

 

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

	var alpha = g_form.getValue('u_Alpha');
	var beta = g_form.getValue('u_Beta');
	var caesar = alpha + "-" + beta;
	g_form.setValue('u_Caesar', caesar);
   
}

 

 

Does anyone have any idea why this would happen?

 

Thank you in advance!

4 REPLIES 4

Tushar
Kilo Sage
Kilo Sage

Hi @C_dric Chen 

 

The issue you're encountering with your onChange client scripts not working as expected in the workspace could be due to the different behavior of forms and events in the workspace compared to the standard form view.

The workspace introduces a different user experience, and events might behave differently.

 

Please try below -

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

   var alphaField = g_form.getField('u_Alpha');
   var betaField = g_form.getField('u_Beta');
   var caesarField = g_form.getField('u_Caesar');

   var alpha = alphaField.getValue();
   var beta = betaField.getValue();
   var caesar = alpha + "-" + beta;

   caesarField.setValue(caesar);
}

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

sanna3
Tera Contributor

Have you checked that the Clients scripts are set to "UI Type = All"?
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0827632 

manjusha_
Kilo Sage

@C_dric Chen 

 

1.Set UI Type = All" on client script

2.Set the "Isolate Script" field on the client script to "True" and that should help resolve the issue.

Mark my answer as correct/Helpful if it helps

Thanks,

Manjusha Bangale

eric_wesley
Tera Contributor

One reason this happens for client scripts or UI Policies, since they are client side, is that the fields that are read in the script must be visible on the details tab of the workspace. If they are hidden, only server side code (e.g. Business Rules) can read them.