How to store and reuse the value in catalog client scripts

rajeev_kumar
Mega Contributor

We have a catalog item. In this catalog item we have a input field "Field1".

Now onChange of this field we check if this value is already exist on the server by using GlideAjax.   We want to to same thing onSubmit as well, but we do not want to make server call.

One way of doing it is save the result in a variable on onChange event (by GlideAjax) and get the value of this variable on onSubmit and take appropriate action.

Second way is to make GlideAjax call again on onSubmit.

Is there any other way to achieve this without variable/GlideAjax ?

Thanks,

Rajeev.

9 REPLIES 9

You need to declare the variable outside the onChange function. If still its not working then you could create a another function in onChange script to return value and then call that function in onsubmit script .



OnChange Script:


var test = '' ;


function onChange(){


test   =   "testValue" ;


}


function getValueOfTest(){


        return test ;


}



OnSubmit Script:


function onSubmit(){


var test2 = getValueOfTest();


}


This works:


OnChange Script:


test="test1";


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


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


          return;


    }


test ="test2";


    //Type appropriate comment here, and begin script below


 


}



OnSubmit Script:


function onSubmit() {


    //Type appropriate comment here, and begin script below


    alert(test);


      return false;


}



Regards,


Mujtaba Bhat


Yes this one is working.


Actually the problem was with the keyword "var". If we declare the variable with var then it would not be global.


Thanks.


bernyalvarado
Mega Sage

Hi Rajeev, you can do the following steps:



a) You can create a variable in your Catalog Item as a catalog item variable


b) Through a UI catalog policy you can manage the visible of this catalog variable. I guess you will like to make not visible.


c) You can simply set that variable on your client onchange catalog script and then use that variable in your onSubmit catalog script.



Thanks,


Berny


Thanks Berny.


Indeed we adopted this approach only.