Performance issue!! client script to Populate value takes lot of time

Bhavana Reddy
Mega Guru

Hello Guys,

We are facing the performance issue, The client script runs very slowly and it takes lot of time to load the value on the form.

var ga = new GlideAjax('ACUXValues');
ga.addParam('sysparm_name', 'getCustomerOrder');
ga.addParam('sysparm_custOrd', ID);
ga.getXML(populateDetails);

function populateDetails(response) {

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

// if (answer) {
var returndata = JSON.parse(answer);

function setQuote() {
g_form.setValue("svo", returndata.svo_no);   // wait untill other script loads the choices dynamically and then set the value
}
if (g_form.getValue('type_of_change') == 'Technical Data Change') {
g_form.setValue("opportunity_id", returndata.opportunity_no);
window.setTimeout(setQuote, 2000); // we are dynamically populating the options based on the Opportunity ID and hence we need to wait untill the other script loads the choices dynamically 
}
if (returndata.Service_id != '') {
g_form.setValue("service_id", returndata.Service_id);
}
if (returndata.CSE_notes != '') {
g_form.setValue("cse_notes", returndata.CSE_notes);
}

g_form.setValue("is_pm_required", returndata.is_pm_required);

g_form.setValue("modem_type", returndata.Modem_Type);

if (returndata.Customer_Location != '') {
g_form.setValue("customer_location", returndata.Customer_Location);
}
if (returndata.colo == 'Yes') {
g_form.setValue("collocation", 'yes');
}
if (returndata.colo == 'No') {
g_form.setValue("collocation", 'no');
}

similarly we are setting more than 25 fields, sometimes the screen hangs and we manually need to refresh the form and window.setTimeout(setQuote, 2000); doesnot always work as expected

 

Please suggest if there any better approach

1 ACCEPTED SOLUTION

Something like below. I've just created a stub Script Include.

Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    try {
        var ID = g_form.getValue('customer_order_no');
        var ga = new GlideAjax('ACUXValues');
        ga.addParam('sysparm_name', 'getCustomerOrder');
        ga.addParam('sysparm_custOrd', ID);
        ga.getXMLAnswer(function(answer) {
			alert(answer);
            var returndata = JSON.parse(answer);
			var acuxResult = returndata.acuxResult;  // get data for acux
			var quotesResult = returndata.quotesResult; /// get data for quotes
            if (g_form.getValue('type_of_change') == 'Technical Data Change') {
                g_form.setValue("opportunity_id", returndata.opportunity_no);
                var oppr = g_form.getValue('opportunity_id');
                var answerArray = quotesResult.split(',');
                g_form.clearOptions('svo');
                g_form.addOption('svo', '', '--None--');
                for (var i = 0; i < answerArray.length; i++) {
                    g_form.addOption('svo', answerArray[i], answerArray[i]); // dynamically adding the choices based on the opportunity field
                }
            }

            g_form.setValue("svo", returndata.svo_no); // since this line executes early the value is not set, 
            if (acuxResult.Service_id != '') {
                g_form.setValue("service_id", acuxResult.Service_id);
            }
            if (acuxResult.CSE_notes != '') {
                g_form.setValue("cse_notes", acuxResult.CSE_notes);
            }
            g_form.setValue("is_pm_required", acuxResult.is_pm_required);
            g_form.setValue("modem_type", acuxResult.Modem_Type);
            if (acuxResult.Customer_Location != '') {
                g_form.setValue("customer_location", acuxResult.Customer_Location);
            }
            if (acuxResult.colo == 'Yes') {
                g_form.setValue("collocation", 'yes');
            }
            if (acuxResult.colo == 'No') {
                g_form.setValue("collocation", 'no');

            }
        });
    } catch (e) {
        alert(e.message);
    }
}

Script Include: ACUXValues. this is just a stub to return values. This is client callable

var ACUXValues = Class.create();
ACUXValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCustomerOrder: function() {
        var result = {};
        var acuxResult = {};

        acuxResult['Service_id'] = 'service id';
        acuxResult['CSE_notes'] = 'cse notes';
        acuxResult['is_pm_required'] = true;
        acuxResult['Modem_Type'] = 'modem type';
        acuxResult['Customer_Location'] = 'Customer location';
        acuxResult['colo'] = 'Yes';
        var opno = 'opno';
        acuxResult['opportunity_no'] = opno;

        result['acuxResult'] = acuxResult;  / set acux result
        result['quotesResult'] = new QuoteDisplay().getQuotedetails(opno);  // get and set quotes data. passing parameter 'opno'
        return JSON.stringify(result);  // convert to json and return it
    },
    type: 'ACUXValues'
});

find_real_file.png

Script Include: QuoteDisplay. This is not client callable

var QuoteDisplay = Class.create();
QuoteDisplay.prototype = {
    initialize: function() {
    },
    getQuotedetails: function(oppr) {
        var quotesResult = 'option1,option2,option2';
		return quotesResult;
    },
    type: 'QuoteDisplay'
};

find_real_file.png

Execution result:

find_real_file.png

View solution in original post

9 REPLIES 9

Why not combine the 2 script include into 1? The only parameter that's being set in the 2nd ajax call is "opportunity_id" that's returned from the 1st ajax call.

Have the second script include call the first script include at the start to get value of opportunity_id and return the combined result.

The first script include does a different job( gets the values entered by the user in the previous customer order) and the second script include is used to fetch the choices dynamically from different table and these 2 script include function Job is totally independant of each other.

First choices should be set from different script then the field should be set.

 

Something like below. I've just created a stub Script Include.

Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    try {
        var ID = g_form.getValue('customer_order_no');
        var ga = new GlideAjax('ACUXValues');
        ga.addParam('sysparm_name', 'getCustomerOrder');
        ga.addParam('sysparm_custOrd', ID);
        ga.getXMLAnswer(function(answer) {
			alert(answer);
            var returndata = JSON.parse(answer);
			var acuxResult = returndata.acuxResult;  // get data for acux
			var quotesResult = returndata.quotesResult; /// get data for quotes
            if (g_form.getValue('type_of_change') == 'Technical Data Change') {
                g_form.setValue("opportunity_id", returndata.opportunity_no);
                var oppr = g_form.getValue('opportunity_id');
                var answerArray = quotesResult.split(',');
                g_form.clearOptions('svo');
                g_form.addOption('svo', '', '--None--');
                for (var i = 0; i < answerArray.length; i++) {
                    g_form.addOption('svo', answerArray[i], answerArray[i]); // dynamically adding the choices based on the opportunity field
                }
            }

            g_form.setValue("svo", returndata.svo_no); // since this line executes early the value is not set, 
            if (acuxResult.Service_id != '') {
                g_form.setValue("service_id", acuxResult.Service_id);
            }
            if (acuxResult.CSE_notes != '') {
                g_form.setValue("cse_notes", acuxResult.CSE_notes);
            }
            g_form.setValue("is_pm_required", acuxResult.is_pm_required);
            g_form.setValue("modem_type", acuxResult.Modem_Type);
            if (acuxResult.Customer_Location != '') {
                g_form.setValue("customer_location", acuxResult.Customer_Location);
            }
            if (acuxResult.colo == 'Yes') {
                g_form.setValue("collocation", 'yes');
            }
            if (acuxResult.colo == 'No') {
                g_form.setValue("collocation", 'no');

            }
        });
    } catch (e) {
        alert(e.message);
    }
}

Script Include: ACUXValues. this is just a stub to return values. This is client callable

var ACUXValues = Class.create();
ACUXValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCustomerOrder: function() {
        var result = {};
        var acuxResult = {};

        acuxResult['Service_id'] = 'service id';
        acuxResult['CSE_notes'] = 'cse notes';
        acuxResult['is_pm_required'] = true;
        acuxResult['Modem_Type'] = 'modem type';
        acuxResult['Customer_Location'] = 'Customer location';
        acuxResult['colo'] = 'Yes';
        var opno = 'opno';
        acuxResult['opportunity_no'] = opno;

        result['acuxResult'] = acuxResult;  / set acux result
        result['quotesResult'] = new QuoteDisplay().getQuotedetails(opno);  // get and set quotes data. passing parameter 'opno'
        return JSON.stringify(result);  // convert to json and return it
    },
    type: 'ACUXValues'
});

find_real_file.png

Script Include: QuoteDisplay. This is not client callable

var QuoteDisplay = Class.create();
QuoteDisplay.prototype = {
    initialize: function() {
    },
    getQuotedetails: function(oppr) {
        var quotesResult = 'option1,option2,option2';
		return quotesResult;
    },
    type: 'QuoteDisplay'
};

find_real_file.png

Execution result:

find_real_file.png

This solution works very well but initially there is a delay of 4s then it loads all the value for the fields, This might be because of the script include 

Do you have any suggestions for this?

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi  Bhavana,

First, is this a onChange() script?

I'm not understanding the reason for windows.setTimeout(). I'm not able to find other script loading the choices dynamically.

Also, try hardcoding a sample response from GlideAjax() and skip the call. If performance improves, performance problem may be with the Script Include. Can also try running the logic in Script Include as a Script Background to see how much it takes to get a result.

Following probably won't make too much of a performance difference but as a note.

Using .getXML() and JSON.stringify() in Script Include doesn't make too sense. If using JSON.stringify() to send result back, use .getXMLAnswer(). .getXML() will convert the reply to xml while .getXMLAnswer() will just sent it back as a String.

That is, if using .getXML(), try using it without JSON.stringify() the result to send back because it will essentially convert the result to a JSON and put that in xml document.

FYR,

https://community.servicenow.com/community?id=community_article&sys_id=1c10a1fedbbd4890feb1a851ca961...