Retain Old value of a field using Onchange client script

Community Alums
Not applicable

Hi,

There are 2 fields on the form :

test : choice field (yes/no)

test1 : reference field

Case 1 : If no is selected from test, test1 should clear the value if it has any.

Case 2 : If yes is selected in test, then test1 should be mandatory.

Case 3 : In test, if first 'yes' is selected, then no and then again changed to yes, then it should return the old value that it contained in test1.

I want to implement this by using an Onchange client script.

Can someone please assist.

Onchange Issue.PNG

Thanks in Advance

Thanks,

Sunil Raikwar

9 REPLIES 9

Community Alums
Not applicable

Hi Pradeep,



Thanks for your response


Using the above code is helping me clear value when no is selected in test field, but when I select yes, in field test1 it gives me '-'


Onchange Issue2.PNG


It does not retains its old value.


Could you please assist me more on this.



Thanks,


Sunil Raikwar


Hi,



As I mentioned in my previous reply you have to use scratchpad to retain the old value.



Sent from Outlook Mobile<https://aka.ms/blhgte>


Hi Pradeep


My requirement is somewhat similar.


I am having state and substate field on my form . Substate depends on state.My requirement is when state is pending then substate is mandatory which is done by ui policy. Further only the assignee must be able to change the state to IN Progress . I have written an Onchange client script for this.



Problem is that when someone other than assignee changes the state from pending to In Progress   then the state gets set to oldValue i.e. Pending but the substate value is not retained . It becomes ' --None--';



So how to solve this issue .



My CLient Script is as follows:



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


    if (isLoading || newValue === '') {


          return;


    }



  if (oldValue=='-5'){


  if(newValue=='8'){


   


  var assigned = g_form.getValue('assigned_to');


            if(assigned != g_user.userID) {


    g_form.setValue('state',oldValue);


    g_form.setValue('u_substate',g_scratchpad.substate);


    }


           



    }


                                      g_form.setMandatory('work_notes',true);


  }



Business Rule is :


function onDisplay(current, g_scratchpad) {


    //This function will be automatically called when this rule is processed.


    g_scratchpad.substate=current.u_substate;  


}



Thanks


Snehal


Hi  


Did you get any solution ? whether the scratchpad concept working?



Thanks


Snehal


ghsrikanth
Tera Guru

Hi Sunil,



Suggestion given by Pradeep is the best practice, I will advice you to follow that script.


Just in case,



Please find the corrected script -


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


    if (isLoading || newValue == '') {


          return;


    }


    var oldTest1 = '';


    //Type appropriate comment here, and begin script below


  if(newValue == 'no'){


  var oldTest1 = g_form.getValue('test1');


  g_form.setValue('test1','');


  }


  else if(newValue == 'yes'){


  if(oldTest1 != '')


  g_form.setValue('test1',oldTest1);


  g_form.setMandatory('test1');


  }


}




Mark if it is helpful or correct, feedback is appreciated