Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Script Include and On change Client script using JS methods browser issue

DPrasna
Tera Contributor

Hi All,

 

I have a script include that returns the following result exactly as shown below with comma

 

"Development , Production"

Client Script (not working on Portal)

 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		g_form.setVisible('column1',false);
		g_form.setVisible('column2',false);
		g_form.setVisible('column3',false);
		
		return;
	}	

	var prod = g_form.getReference('product');
	//alert(JSON.stringify(prod));
	//alert(prod.u_application_name);

	var ga = new GlideAjax('<scriptincludename>');
	ga.addParam("sysparm_name", "getCI");
	ga.addParam("sysparm_app_name", prod.u_application_name);
	ga.addParam("sysparm_sysid", newValue);
	//alert(newValue);
	//alert(newValue.getDisplayValue());
	ga.getXML(getResponse);

	function getResponse(response) {
		var res = response.responseXML.documentElement.getAttribute("answer");
		alert(res.toString());		
		
		//var res1 = res.toString();
		if(res.includes('Development')){	
			g_form.setVisible('column1',true);
			g_form.setVisible('column2',true);
			
		}
		if(res.includes('Production')){		
			g_form.setVisible('column2',true);
			g_form.setVisible('column3',true);
			
		}

	}

}

 

"

 

Now I need to have a client script to check the matches of the above returned results which is Dev/ Prod. I have the below script which works on Native UI but not on the Portal. It displays an error "There is a JavaScript error in your browser console 

I need help to make it compatible and make it work on the Portal as expected.

 

@Danish Bhairag2 @Sandeep Rajput @Ankur Bawiskar @Peter Bodelier @Chuck Tomasi 

 

 

11 REPLIES 11

DPrasna_0-1698757081236.png

 

Danish Bhairag2
Tera Sage

@DPrasna ,

 

Also g_form.getReference does not work in Service Portal.

Please refer this article on how to use it on portal

 

https://www.servicenow.com/community/developer-forum/g-form-getreference-is-not-working-in-service-p...

 

Thanks,

Danish

 

Ankur Bawiskar
Tera Patron
Tera Patron

@DPrasna 

Ensure UI type is ALL and use getXMLAnswer

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		g_form.setVisible('column1',false);
		g_form.setVisible('column2',false);
		g_form.setVisible('column3',false);

		return;
	}	

	var prod = g_form.getReference('product');
	//alert(JSON.stringify(prod));
	//alert(prod.u_application_name);

	var ga = new GlideAjax('<scriptincludename>');
	ga.addParam("sysparm_name", "getCI");
	ga.addParam("sysparm_app_name", prod.u_application_name);
	ga.addParam("sysparm_sysid", newValue);
	ga.getXMLAnswer(function(answer){

		var res = answer;
		alert(res.toString());		
		//var res1 = res.toString();
		if(res.includes('Development')){	
			g_form.setVisible('column1',true);
			g_form.setVisible('column2',true);

		}
		if(res.includes('Production')){		
			g_form.setVisible('column2',true);
			g_form.setVisible('column3',true);
		}

	});

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

I tried the same using "

getXMLAnswer

However the error remains the same

"

Hi @DPrasna,

 

Do you have logging in your script include as well? 
Does it complete up until the return?

 

As mentioned before, getReference doesn't work with a callback but I haven't seen a corrected script here.

 

Try this:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        g_form.setVisible('column1', false);
        g_form.setVisible('column2', false);
        g_form.setVisible('column3', false);

        return;
    }

    var prod = g_form.getReference('product', getRef);

    function getRef(prod) {
        //alert(JSON.stringify(prod));
        //alert(prod.u_application_name);

        var ga = new GlideAjax('<scriptincludename>');
        ga.addParam("sysparm_name", "getCI");
        ga.addParam("sysparm_app_name", prod.u_application_name);
        ga.addParam("sysparm_sysid", newValue);
        //alert(newValue);
        //alert(newValue.getDisplayValue());
        ga.getXML(getResponse);

    }

    function getResponse(response) {
        var res = response.responseXML.documentElement.getAttribute("answer");
        alert(res.toString());

        //var res1 = res.toString();
        if (res.includes('Development')) {
            g_form.setVisible('column1', true);
            g_form.setVisible('column2', true);

        }
        if (res.includes('Production')) {
            g_form.setVisible('column2', true);
            g_form.setVisible('column3', true);

        }

    }

}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.