How to get the previous value in onchange of a field in client script?

Bala13
Kilo Guru

Hello,

I have a requirement like, whenever a change request is in pending state, enginner should not edit MACD field which is a refernce field,

Once the MACD selected, classification, assigned to fields will be changed accordingly.

So, i need to restrict the users when they are changing the MACD field if the change request is in pending state.

For this i have written a Onchange client script.

It is throwing me the error message when they are changing the MACD. But once the message displayed, automaticllay it should set the old values for MACD, classification and assigned to. 

How to acheive that?

Client script onchange of MACD field:

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

var test = g_form.getBooleanValue("u_is_this_related_to_requested_item");
if (test === true) {
alert("insiden 1st if");
if (g_form.getValue('state') == '-5') {
g_form.addErrorMessage("Do not change the MACD when the SRCHG is in Pending state");
return false;
}


}
}

1 ACCEPTED SOLUTION

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

It also worth pointing out that 'oldValue' only stores the "pre-change" value of the one field the onChange client script is created for. So with that approach, you would need to have one onChange client script for each field.

Alternatively, you could use a display business rule here and populate the g_scratchpad object. Please note though that the display business rule is processed right before the form is presented to the user, so it will only store the values that existed in the database before form load. Assuming that's OK, you could use something like:

g_scratchpad.macd = current.getValue('u_your_macd_field');
g_scratchpad.assigned_to= current.getValue('assigned_to');
g_scratchpad.classification= current.getValue('u_your_classification_field');

in a display business rule, which you can then access in your client script

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

var test = g_form.getBooleanValue("u_is_this_related_to_requested_item");
if (test === true) {
alert("insiden 1st if");
if (g_form.getValue('state') == '-5') {
g_form.addErrorMessage("Do not change the MACD when the SRCHG is in Pending state");
g_form.setValue('u_your_macd_field',g_scratchpad.macd);
g_form.setValue('assigned_to',g_scratchpad.assigned_to);
g_form.setValue('u_your_classification_field',g_scratchpad.classification);
return false;
}


}
}

 

Let me know if it helps.

View solution in original post

9 REPLIES 9

Hi,

Can you give more specifics?

If you have this onChange client script on the same field that populates the data elsewhere, then using oldValue...will go back to what the value was on form load...and subsequently set the values for those other fields.

Per what you mentioned above, that's why you are limiting this one field because it controls other fields.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

It also worth pointing out that 'oldValue' only stores the "pre-change" value of the one field the onChange client script is created for. So with that approach, you would need to have one onChange client script for each field.

Alternatively, you could use a display business rule here and populate the g_scratchpad object. Please note though that the display business rule is processed right before the form is presented to the user, so it will only store the values that existed in the database before form load. Assuming that's OK, you could use something like:

g_scratchpad.macd = current.getValue('u_your_macd_field');
g_scratchpad.assigned_to= current.getValue('assigned_to');
g_scratchpad.classification= current.getValue('u_your_classification_field');

in a display business rule, which you can then access in your client script

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

var test = g_form.getBooleanValue("u_is_this_related_to_requested_item");
if (test === true) {
alert("insiden 1st if");
if (g_form.getValue('state') == '-5') {
g_form.addErrorMessage("Do not change the MACD when the SRCHG is in Pending state");
g_form.setValue('u_your_macd_field',g_scratchpad.macd);
g_form.setValue('assigned_to',g_scratchpad.assigned_to);
g_form.setValue('u_your_classification_field',g_scratchpad.classification);
return false;
}


}
}

 

Let me know if it helps.

Hello Balla,

I have tried this script, but this is not going inside the if condition. It is always through this alert message only.

alert("insiden 1st if");

Which means this if condition it is not going inside. Kindly let me know where is the issue. But if i am removing the set values then it is going inside if condition.

if (g_form.getValue('state') == '-5') {
g_form.addErrorMessage("Do not change the MACD when the SRCHG is in Pending state");
g_form.setValue('u_your_macd_field',g_scratchpad.macd);
g_form.setValue('assigned_to',g_scratchpad.assigned_to);
g_form.setValue('u_your_classification_field',g_scratchpad.classification);
return false;
}

Hi,

It's worth pointing out that the information given in the original post made it sound like the one field: MACD would change the values of classification and assigned to (whether that's through some sort of dot-walk or other client script, etc.). So with that information originally given...that is why I suggested going with oldValue for the MACD field. Which would achieve changing that field back to what it was originally loaded with (which then should have changed the rest of the other fields back). The focus all seemed to be on the MACD field, so...there ya go.

Best of luck with this!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen,

 

You are absolutely right - I forgot about that 🙂 In that case your solution should indeed work.