On submit client script, variable set value is getting cleared.

Community Alums
Not applicable

Hi,

While trying to set a value for the variable using "onsubmit" client script. The alert is getting displayed but after the form is getting submitted. The value gets cleared.

The value has been set via GlideAjax function. But I noticed that when the value is been set outside the function, its getting stored for the variable.

Below is the client script on submit code:-

function onSubmit() {
var currentchoice = g_form.getValue('application_service');
var userdetails = new GlideAjax("Applicationservicechoices");//from Script include returning value as 1 
userdetails.addParam("sysparm_name", "choicelistapp");
userdetails.addParam("sysparm_choice",currentchoice);
userdetails.getXML(isMember1);
}
 
function isMember1(response){
var result = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('variable_details',result);//getting the result value as 1 and the "variable_details" variable type is string.
alert(g_form.getValue('variable_details'));//alert is getting displayed correctly. But after the form submit the value is getting cleared
 
}
 
g_form.setValue('variableDetails',100);//this is a manual declaration of value, if called outside the function, variable value is getting stored.
 
So, the value which is called inside the function for on submit, will it not save for the variable? or is there anyway to return the function to set the variable value?
 
Thanks in advance!!!
1 ACCEPTED SOLUTION

Stefan Georgiev
Tera Guru

Hello @Community Alums ,

I have modified a script that I was using for a similar case, it involves some logic that I have found in the community, but I can not remember the name of the author, basically what you want to do is execute your onSubmit Client Script, execute your async logic, call submit again and return false. 

That is done because the changes from the GlideAjax are going to be available to you after the form is submitted, so what you are seeing in the pop-up is the information you want, but the submission is done. So that is why in the callback function we call again the submission of the form, after we have set the data from the GlideAjax call and we need to cancel the first submission, because in the current iteration of the submit we don't have the data set. 


function onSubmit() {

    

    if (g_scratchpad._ajaxChecked) {

        // We have run our Ajax Checks, so we can continue on

        // and let our form submission continue

        g_scratchpad._ajaxChecked = null;

        return g_scratchpad._returnVal;

    }

var currentchoice = g_form.getValue('application_service');

   

    g_scratchpad._action = g_form.getActionName();

    g_scratchpad._ajaxChecked = false;

 

    var userdetails = new GlideAjax('Applicationservicechoices');

    userdetails .addParam('sysparm_name', 'choicelistapp');

    userdetails .addParam('sysparm_choice', currentchoice);

    userdetails .getXMLAnswer(callback);

 

    function callback(response) {

 

            g_scratchpad._returnVal = true;

        g_form.setValue('variable_details',response)

 

        g_scratchpad._ajaxChecked = true;

                               

        if (typeof g_form.orderNow != 'undefined') {

            // this is a catalog item

            g_form.orderNow();

        } else {

            // this will resubmit the form using the saved

            // ui action that was originally clicked

            g_form.submit(g_scratchpad._action);

        }

 

 

    }

 

    // always return false if we get to this point

    return false;

 

 

}

If this helped you you can Accept the Solution and mark my answer as Helpful.
All the Best,
Stefan

View solution in original post

9 REPLIES 9

Pavankumar_1
Mega Patron

Hi @Community Alums ,

It is an asynchronous call so script ends before receiving the value, so field is submitted without being updated.

Try to use onchange client script or try to achieve or with  before Business rule.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Community Alums
Not applicable

Hi @Pavankumar_1 , The values can be set via only onsubmit. As we are declaring the variables randomly. So, On change will not work for us.

You suggested to use before business rule, how this can be used? Do I need to use a business rule rather than client script? or its different.

Can't we return the output outside the function and store the value in variable? Will this be not possible?

Hi @Community Alums ,

is this is catalog item or normal form?

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Community Alums
Not applicable

Its a catalog item