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

Hi @Community Alums ,

you can use before create or update business rule on sc_req_item table and give condition as Item as your catalog item(Item is reference field available on RITM table).

use 

current.variables.start_date //you can get catalog variable values like this and set values using before update or create Business rule

Screenshot 2023-12-12 at 1.36.40 PM.png

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

Gurpreet07
Mega Sage

Your code requires value from only one variable 'application_service'.  So if you add it as an onChange client script on 'application_service'  then it should work as expected.

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

Community Alums
Not applicable

Hi @Stefan Georgiev ,

The script which you shared it worked after small changes 🎉. Just I want to know why "_ajaxChecked " did not checked for other conditions that is after entered into the callback function and can we call the g_scratchpad in the client side scripting? and is there any negatives when we call at client side?

Thanks.

Hello @Community Alums ,

the script that I provided was a bit sketchy because I was in a hurry, I just modified some things that I was using and there were some code leftovers that forgot to remove, my case was different. 

 "_ajaxChecked ", is just a name that is used for the scratchpad, you can change it, I might have just used the name that I found in the article that I used for my implementation.

g_scratchpad is just an object that you can pass values with, you can try login it in your catalog client script 

You might try passing the data with the g_form, but i need to test that, not sure if it is going to work.

 

There should not be any negative effects for using the g_scratchpad, chances for errors even if you dont clean your data from it are really small, and something to work with the scratchpad and to have the same name. Clear your data and there are not going to be any problems. And if you are talking for performance this is just one transaction, it is not going to impact the performance.

Hope that this helps you!

If the provided information answers your question, please consider marking it as Helpful and Accepting the Solution so other community users can find it faster.

All the Best,
Stefan