Issue in g_scratchpad(Using on submit client script)

Venky Kshatriy2
Tera Contributor

Hi Team,

 

below mentioned code i am used to call the script include to on submit client script and setting value based on that restrict the user
front of the service portal it's working fine i am getting value and it's set  to variable but
If i used the in catalog item we have a TRYIT UI action if use this button and test click the order button  i am getting the bellow error
onSubmit script error: ReferenceError: g_scratchpad is not defined:
function () { [native code] }
 
function onSubmit() {

    if (g_scratchpad.validForm) {
        return;
    }

    var reason1 = g_form.getValue('reason1');
    var reason2 = g_form.getValue('reason2');

    var mrvs = g_form.getValue('risk_rating_from_country');

    var vk = new GlideAjax("Decision_table_risk_rating");
    vk.addParam('sysparm_name', 'getriskratings');
    vk.addParam('sysparm_from', g_form.getValue('from'));
    vk.addParam('sysparam_to', g_form.getValue('to'));
    vk.addParam('sysparam_mrvs', mrvs);

    vk.getXML(function risk(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert('risk rating' + answer);
        g_form.setValue('risk_rating', answer);
        if (answer == 1 && reason1 == '' && reason2 == '') {
            g_form.setVisible('reason1', true);
            g_form.setVisible('reason2', true);
            g_form.setMandatory('reason1', true);
            g_form.setMandatory('reason2', true);
            g_scratchpad.validForm = false;
        } else {
            g_scratchpad.validForm = true;
            g_form.submit();
        }
    });
   
    return false;


}
 
VenkyKshatriy2_0-1721803949456.png

Front of portal it's setting the variable value in below image

VenkyKshatriy2_3-1721804027756.png

 

 

could pls help to resolve this error

 

Advance thanks.


 

1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron

As far as I know g_scratchpad is not supported in CMS - only in Portal.

A workaround could be "simulating" it, something like:

function onSubmit () {
	var scratchpad = getExistingScratchpad() || getCustomScratchpad();

	if (scratchpad.validForm) {
		return;
	}

	var reason1 = g_form.getValue('reason1');
	var reason2 = g_form.getValue('reason2');

	var mrvs = g_form.getValue('risk_rating_from_country');

	var vk = new GlideAjax("Decision_table_risk_rating");

	vk.addParam('sysparm_name', 'getriskratings');
	vk.addParam('sysparm_from', g_form.getValue('from'));
	vk.addParam('sysparam_to', g_form.getValue('to'));
	vk.addParam('sysparam_mrvs', mrvs);

	vk.getXML(function risk (response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");

		alert('risk rating' + answer);

		g_form.setValue('risk_rating', answer);

		if (answer == 1 && reason1 == '' && reason2 == '') {
			g_form.setVisible('reason1', true);
			g_form.setVisible('reason2', true);
			g_form.setMandatory('reason1', true);
			g_form.setMandatory('reason2', true);
			scratchpad.validForm = false;
		}
		else {
			scratchpad.validForm = true;
			g_form.submit();
		}
	});

	return false;

	function getCustomScratchpad () {
		return NOW.g_scratchpad = NOW.g_scratchpad || {};
	}

	function getExistingScratchpad () {
		if (typeof g_scratchpad != 'undefined')
			return g_scratchpad;
	}
}

 

View solution in original post

11 REPLIES 11

-O-
Kilo Patron
Kilo Patron

As far as I know g_scratchpad is not supported in CMS - only in Portal.

A workaround could be "simulating" it, something like:

function onSubmit () {
	var scratchpad = getExistingScratchpad() || getCustomScratchpad();

	if (scratchpad.validForm) {
		return;
	}

	var reason1 = g_form.getValue('reason1');
	var reason2 = g_form.getValue('reason2');

	var mrvs = g_form.getValue('risk_rating_from_country');

	var vk = new GlideAjax("Decision_table_risk_rating");

	vk.addParam('sysparm_name', 'getriskratings');
	vk.addParam('sysparm_from', g_form.getValue('from'));
	vk.addParam('sysparam_to', g_form.getValue('to'));
	vk.addParam('sysparam_mrvs', mrvs);

	vk.getXML(function risk (response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");

		alert('risk rating' + answer);

		g_form.setValue('risk_rating', answer);

		if (answer == 1 && reason1 == '' && reason2 == '') {
			g_form.setVisible('reason1', true);
			g_form.setVisible('reason2', true);
			g_form.setMandatory('reason1', true);
			g_form.setMandatory('reason2', true);
			scratchpad.validForm = false;
		}
		else {
			scratchpad.validForm = true;
			g_form.submit();
		}
	});

	return false;

	function getCustomScratchpad () {
		return NOW.g_scratchpad = NOW.g_scratchpad || {};
	}

	function getExistingScratchpad () {
		if (typeof g_scratchpad != 'undefined')
			return g_scratchpad;
	}
}

 

Thanks for your ans @-O- 

 

But if i use this script in the last function it's showing one message like can't find the g_scratchpad

VenkyKshatriy2_1-1721813410027.png

 

VenkyKshatriy2_2-1721813505770.png

This kind of message i am getting

After using your script its working but in TRY IT UI action it's showing one error message bro

VenkyKshatriy2_4-1721814029330.png

 

So when you are using the Try It button on the Catalog Item form, it opens the Catalog Item in the old legacy CMS environment.

Not Portal.

And those environments are vastly different when it comes to supported APIs.

Thus g_form.submit() is valid in Portal, but not in CMS (Content Management System).

Vice-versa g_form.orderNow() is valid in CMS, but not in Portal.

 

The right solution for this is to create separate onSubmit Catalog Client Scripts per environment.

One does that by setting field "UI Type" on the "Catalog Client Script" form:

2024-07-24_13-02-31.png

If UI Type is "Desktop" or "All" the Catalog Client Script will be executed in CMS.

If UI Type is "Mobile / Service Portal" or "All" the Catalog Client Script will be executed in Portal.

So in the "Desktop" variant of the onSubmit Catalog Client Script you would use

g_form.orderNow()

but in the "Mobile / Service Portal" variant of the onSubmit Catalog Client Script you would use

g_form.submit()