We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Error "move function declaration to function body root" in client script.

imkhan
Tera Contributor

I have an onchange client script where i have a function that gets the error "Move function declaration to function body root."

Getting an error in the client script for the function " SvcCatalogGetDateDiffParse"

Can you please check the client script and suggest how I can remove the error?

 

script include

--------------------

var SvcCatalogGetDateDiff = Class.create();
SvcCatalogGetDateDiff.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDateDiff : function() {
var start = this.getParameter('sysparm_start');

var create = this.getParameter('sysparm_created');

var currentdate = new GlideDateTime(gs.nowDateTime());
var schedule_business = new GlideSchedule();
schedule_business.load('659ca1f61b84734067b5ed7cee4bcb85');// loads "cadent business days" schedule
if(!create){
var duration_nocreate = schedule_business.duration(currentdate,new GlideDateTime(start));////When change is not created this will trigger
var numvalue = duration_nocreate.getNumericValue()/1000; //calculating time in seconds.

return (numvalue);

}
else{
var deploytime = new GlideDateTime("2019-03-26 14:00:00");

if (new GlideDateTime(create)<deploytime)
{
return gs.dateDiff(create,start,true);
}
else
{
var durationcreate = schedule_business.duration(new GlideDateTime(create), new GlideDateTime(start));//When change is created this will trigger
var numvalue1 = durationcreate.getNumericValue()/1000; //calculating time in seconds.
return (numvalue1);
}
}
}
});

---------------------------

client script

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
if (newValue) {
if (g_form.getValue('category') != 'Migrated Data') {
var data = g_form.getValue('type');
var dep= g_form.getValue('urgency');
var ga1 = new GlideAjax('SvcCatalogGetDateDiff'); //Name of the Script Include
ga1.addParam('sysparm_name', 'getDateDiff'); //Name of the function in the script include
ga1.addParam('sysparm_created', g_form.getValue('sys_created_on'));
ga1.addParam('sysparm_start', g_form.getValue('start_date'));
ga1.getXML(SvcCatalogGetDateDiffParse);

function SvcCatalogGetDateDiffParse(response) {

answer = response.responseXML.documentElement.getAttribute("answer");

if ((data == "Comprehensive" && dep == '1')) {

if (answer <= 10 * 24 * 60 * 60)

{
alert("There should be atleast 10 business days ");
g_form.setValue('start_date', '');
return;
}
}
}

if (answer == 'true') {


if ((data == "Standard" && dep== '1')) {
if (answer <= 5 * 24 * 60 * 60)

{
alert("There should be atleast 5 business days ");
g_form.setValue('start_date', '');
return;
}
}

}
}

}
}

2 REPLIES 2

Saurav11
Kilo Patron

Hello,

 

It is actually just a warning which you can ignore but if you want to get rid of it just move your function outside the onchange brackets

 

Use the below script in Onchange client script:-

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    if (newValue) {
        if (g_form.getValue('category') != 'Migrated Data') {
            var data = g_form.getValue('type');
            var dep = g_form.getValue('urgency');
            var ga1 = new GlideAjax('SvcCatalogGetDateDiff'); //Name of the Script Include
            ga1.addParam('sysparm_name', 'getDateDiff'); //Name of the function in the script include
            ga1.addParam('sysparm_created', g_form.getValue('sys_created_on'));
            ga1.addParam('sysparm_start', g_form.getValue('start_date'));
            ga1.getXML(SvcCatalogGetDateDiffParse);

          
            if (answer == 'true') {


                if ((data == "Standard" && dep == '1')) {
                    if (answer <= 5 * 24 * 60 * 60)

                    {
                        alert("There should be atleast 5 business days ");
                        g_form.setValue('start_date', '');
                        return;
                    }
                }

            }
        }

    }
}

  function SvcCatalogGetDateDiffParse(response) {

                answer = response.responseXML.documentElement.getAttribute("answer");

                if ((data == "Comprehensive" && dep == '1')) {

                    if (answer <= 10 * 24 * 60 * 60)

                    {
                        alert("There should be atleast 10 business days ");
                        g_form.setValue('start_date', '');
                        return;
                    }
                }
            }

 

Please mark answer correct/helpful based on Impact.

imkhan
Tera Contributor

Hi saurav, client script is not working. It basically compares the current date to the planned start date.