Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Getting error "Move function declaration to function body root." in client script.

VIKAS MISHRA
Tera Contributor

I have written one onchange client script where i have declared one function also but getting warning for that function says "Move function declaration to function body root.".

Kindly check the below code where i am getting error for the function "checkValidMatchCommodityParse".

Please suggest how may i resolve that warning coming.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue == 'NDA') {
g_form.clearValue('x_amex_sr_commodity_level_1');
g_form.clearValue('x_amex_sr_commodity_level_2');
g_form.clearValue('x_amex_sr_commodity_level_3');
g_form.clearValue('x_amex_sr_commodity_level_4');
g_form.clearValue('x_amex_sr_pu_commodity_code');
g_form.setDisplay('x_amex_sr_commodity_level_1', 'false');
g_form.setDisplay('x_amex_sr_commodity_level_2', 'false');
g_form.setDisplay('x_amex_sr_commodity_level_3', 'false');
g_form.setDisplay('x_amex_sr_commodity_level_4', 'false');
g_form.setDisplay('x_amex_sr_pu_commodity_code', 'false');
} else if (newValue != 'NDA') {
g_form.setDisplay('x_amex_sr_commodity_level_1', 'true');
g_form.setDisplay('x_amex_sr_commodity_level_2', 'true');
g_form.setDisplay('x_amex_sr_commodity_level_3', 'true');
g_form.setDisplay('x_amex_sr_commodity_level_4', 'true');
g_form.setDisplay('x_amex_sr_pu_commodity_code', 'true');

if ((g_form.getValue(x_amex_sr_contract_id)) != '' && (g_form.getValue(x_amex_sr_pu_commodity_code)) == '') {
var ga = new GlideAjax('SourcingEngagementUtils_2');
ga.addparam('sysparam_name', 'checkValidMatchCommodity');
ga.addParam('sysparam_contractid', g_form.getValue('contractid'));
ga.getXML(checkValidMatchCommodityParse);
}

function checkValidMatchCommodityParse(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('x_amex_sr_commodity_level_1', answer);
g_form.setValue('x_amex_sr_commodity_level_2', answer);
g_form.setValue('x_amex_sr_commodity_level_3', answer);
g_form.setValue('x_amex_sr_commodity_level_4', answer);
g_form.setValue('x_amex_sr_pu_commodity_code', answer);
}
//Type appropriate comment here, and begin script below
}
}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

it's a warning which you can ignore

If you wish you can update it as this; i.e. move the function outside onChange

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	if (newValue == 'NDA') {
		g_form.clearValue('x_amex_sr_commodity_level_1');
		g_form.clearValue('x_amex_sr_commodity_level_2');
		g_form.clearValue('x_amex_sr_commodity_level_3');
		g_form.clearValue('x_amex_sr_commodity_level_4');
		g_form.clearValue('x_amex_sr_pu_commodity_code');
		g_form.setDisplay('x_amex_sr_commodity_level_1', 'false');
		g_form.setDisplay('x_amex_sr_commodity_level_2', 'false');
		g_form.setDisplay('x_amex_sr_commodity_level_3', 'false');
		g_form.setDisplay('x_amex_sr_commodity_level_4', 'false');
		g_form.setDisplay('x_amex_sr_pu_commodity_code', 'false');
	} else if (newValue != 'NDA') {
		g_form.setDisplay('x_amex_sr_commodity_level_1', 'true');
		g_form.setDisplay('x_amex_sr_commodity_level_2', 'true');
		g_form.setDisplay('x_amex_sr_commodity_level_3', 'true');
		g_form.setDisplay('x_amex_sr_commodity_level_4', 'true');
		g_form.setDisplay('x_amex_sr_pu_commodity_code', 'true');

		if ((g_form.getValue(x_amex_sr_contract_id)) != '' && (g_form.getValue(x_amex_sr_pu_commodity_code)) == '') {
			var ga = new GlideAjax('SourcingEngagementUtils_2');
			ga.addparam('sysparam_name', 'checkValidMatchCommodity');
			ga.addParam('sysparam_contractid', g_form.getValue('contractid'));
			ga.getXML(checkValidMatchCommodityParse);
		}
		//Type appropriate comment here, and begin script below
	}
}

function checkValidMatchCommodityParse(response){
	var answer = response.responseXML.documentElement.getAttribute("answer");
	g_form.setValue('x_amex_sr_commodity_level_1', answer);
	g_form.setValue('x_amex_sr_commodity_level_2', answer);
	g_form.setValue('x_amex_sr_commodity_level_3', answer);
	g_form.setValue('x_amex_sr_commodity_level_4', answer);
	g_form.setValue('x_amex_sr_pu_commodity_code', answer);
}

regards
Ankur

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

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

it's a warning which you can ignore

If you wish you can update it as this; i.e. move the function outside onChange

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	if (newValue == 'NDA') {
		g_form.clearValue('x_amex_sr_commodity_level_1');
		g_form.clearValue('x_amex_sr_commodity_level_2');
		g_form.clearValue('x_amex_sr_commodity_level_3');
		g_form.clearValue('x_amex_sr_commodity_level_4');
		g_form.clearValue('x_amex_sr_pu_commodity_code');
		g_form.setDisplay('x_amex_sr_commodity_level_1', 'false');
		g_form.setDisplay('x_amex_sr_commodity_level_2', 'false');
		g_form.setDisplay('x_amex_sr_commodity_level_3', 'false');
		g_form.setDisplay('x_amex_sr_commodity_level_4', 'false');
		g_form.setDisplay('x_amex_sr_pu_commodity_code', 'false');
	} else if (newValue != 'NDA') {
		g_form.setDisplay('x_amex_sr_commodity_level_1', 'true');
		g_form.setDisplay('x_amex_sr_commodity_level_2', 'true');
		g_form.setDisplay('x_amex_sr_commodity_level_3', 'true');
		g_form.setDisplay('x_amex_sr_commodity_level_4', 'true');
		g_form.setDisplay('x_amex_sr_pu_commodity_code', 'true');

		if ((g_form.getValue(x_amex_sr_contract_id)) != '' && (g_form.getValue(x_amex_sr_pu_commodity_code)) == '') {
			var ga = new GlideAjax('SourcingEngagementUtils_2');
			ga.addparam('sysparam_name', 'checkValidMatchCommodity');
			ga.addParam('sysparam_contractid', g_form.getValue('contractid'));
			ga.getXML(checkValidMatchCommodityParse);
		}
		//Type appropriate comment here, and begin script below
	}
}

function checkValidMatchCommodityParse(response){
	var answer = response.responseXML.documentElement.getAttribute("answer");
	g_form.setValue('x_amex_sr_commodity_level_1', answer);
	g_form.setValue('x_amex_sr_commodity_level_2', answer);
	g_form.setValue('x_amex_sr_commodity_level_3', answer);
	g_form.setValue('x_amex_sr_commodity_level_4', answer);
	g_form.setValue('x_amex_sr_pu_commodity_code', answer);
}

regards
Ankur

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