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
Mega Sage
Mega Sage

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

Laszlo Balla
Mega Sage
Mega Sage

Hi,

 

If I understand your requirement correctly, the MACD filed should not be changed when state is Pending, because if they change that, it changes classification and assigned to fields as well, subsequently. Is that right? If so, why don't you just create UI Policy and make the MACD field read-only when state is Pending?

Yes the requirement is correct. We dont want to make the field readonly.

Instead if tehy are changing we need to populate the error message and the previous values should be restored as usual.

Is there a way to do that?

 

Allen Andreas
Administrator
Administrator

Hello,

As has already been called out, it would make sense to make the field read-only. For some reason you're saying you don't want to do that, not sure why, but anyways...

You have access to these within your onChange client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

With that said, you'd want to utilize: oldValue to set those fields back to what they were previously.

So under this line where you are adding in your error message:

g_form.addErrorMessage("Do not change the MACD when the SRCHG is in Pending state");

You can then use more lines like:

g_form.setValue('field_name', oldValue);

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


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

This is not working Allen.