Pass value from Client Script to Processing script UI Pages

Surbhi Srivasta
Tera Expert

Hi,

I have an UI page where on click of a button , field value needs to change. This is working and I am doing this using a Processing script in my UI Page.

Problem is, I need to make the processing script wait until client script confirms.

 

Basically, how can I pass value from client script to my processing script, so that my processing script does not get trigger instantly.

This is what I have tried so far

 

1. I have declared the Hidden variable similar to yours.

2. I have a processing script which is getting executed, but I need to check a certain value which I need to pass from my client script and then only Processing script should get executed.

 

HTML:

<input type="hidden" id="jvar_answer" name="jvar_answer" value=""/>
<g:dialog_buttons_ok_cancel ok_text="${gs.getMessage('Send')}" ok="actionOK();" cancel="return cancel();"/>

Client Script:

function actionOK() {
    var getMessage = new GlideAjax('SI Name');
    getMessage.addParam('sysparm_name', 'Method Name');
    getMessage.addParam('sysparm_recordID', g_form.getUniqueValue());
    getMessage.getXMLAnswer(getMessageInfo);
}

function getMessageInfo(answer) {
	alert(answer); //getting the alert successfully here

    if (answer != '') {
        if (confirm(getMessage(answer)) == true) {
            gel('jvar_answer').value = "confirmed";
        } else {
            gel('jvar_answer').value = "cancel";
        }

    }
}

Process Script:

gs.info("value of Hidden variable is " + jvar_answer);

 

I am getting the message as blank in the info statement put in processing script.

 

Regards,

Surbhi

7 REPLIES 7

Arun_S1
Tera Guru
Tera Guru

@Surbhi Srivasta, I was able to achieve this, not sure if this is what you wanted. Kindly check.

HTML:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<g:ui_form>
	<input id="jvar_answer" name="jvar_answer" value=""/>
	<g:dialog_buttons_ok_cancel ok_text="Interested" cancel_text="Not Interested" ok="actionOK();return true" cancel="cancel();"/>
	</g:ui_form>
</j:jelly>

Client Script:

function actionOK() {
    gel('jvar_answer').value = "Confirmed";
}

function cancel() {
    gel('jvar_answer').value = "Not Confirmed";
}

Processing Script:

gs.addErrorMessage(jvar_answer);

 

Please see the results

Arun_S1_0-1686314649724.png

Arun_S1_1-1686314683324.png

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.

 

@Arun_S1 

Thanks for your response. My use case is slightly different.

In client script, I need to do a glideAJax call and wait for the response to come back and then I need to set the value as Confirmed or Not confirmed.

 

This value I need to pass to processing script and then only processing script should be executed. It is not waiting for the result to come back and the processing script is getting executed.

 

Regards,

Surbhi

@Surbhi Srivasta  Just realized what you are looking for.

 

While invoking AJAX call there are 2 methods available in ServiceNow to parse the response from the script.

1. getXML() - this is asynchronous, which means ServiceNow will not wait until the script execution is complete and will work on other things in parallel.

2. getXMLWait() - this is synchronous, which means ServiceNow will wait for the script execution is completed (this is not recommended)

 

Modify your script as below and check if this is working as per your expectation

function actionOK() {
    var getMessage = new GlideAjax('SI Name');
    getMessage.addParam('sysparm_name', 'Method Name');
    getMessage.addParam('sysparm_recordID', g_form.getUniqueValue());
    getMessage.getXMLWait();
    getMessageInfo(getMessage.getAnswer());
}

function getMessageInfo(answer) {
	alert(answer); //getting the alert successfully here

    if (answer != '') {
        if (confirm(getMessage(answer)) == true) {
            gel('jvar_answer').value = "confirmed";
        } else {
            gel('jvar_answer').value = "cancel";
        }

    }
}

 

I have changed my UI Page client script to below and it is working as expected.

function actionOK() {
    var ga = new GlideAjax('test_ajax');
    ga.addParam('sysparm_name', 'ui_page_response');
    ga.addParam('sysparm_response_provided', 'accept');
    ga.getXMLWait();
	myCallBack(ga.getAnswer());
}

function myCallBack(response) {
    alert('test12345');
	var answer=response;
    //var answer = response.responseXML.documentElement.getAttribute('answer');
    alert(answer); //getting the alert successfully here

    if (answer != '') {
        if (answer == 'true') {
            alert('123--' + answer);
            gel('jvar_answer').value = "confirmed";
        } else {
            gel('jvar_answer').value = "cancel";
        }
    }
    return true;
}

function cancel() {
    gel('jvar_answer').value = "Not Confirmed";
}

Arun_S1_0-1686331074364.png

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.

Ankur Bawiskar
Tera Patron
Tera Patron

@Surbhi Srivasta 

please use <g:ui_form> tag so that the control can pass from client script to processing script

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader