Asynchronous GlideAjax call causing issues

Ankita Kolhe
Tera Contributor

Hi Community,

 

As this is asynchronus call, the array value is getting calculated after JSON object and JSONArray initiation which causes array value to be empty and hence 'monthly_fixed_price_to_be_invoiced' is always empty. I thought to use getXMLWait() but that is depreciated in Xanadu version. Is there any alternative to achieve this so that function test should execute first and then code below calling that function should execute?

 

Please see the below code:

function test(ID, months, length, getMonth, getMonth2,allMonths) {
    var existingFixPrice = [];

 
    var monthlyCostDetail = new GlideAjax('global.KDN_ContractManagementAjax');
    monthlyCostDetail.addParam('sysparm_name', 'getFixPrice');
    monthlyCostDetail.addParam('sysparm_sys_id', ID);
    monthlyCostDetail.addParam('sysparm_final_months', months);
    monthlyCostDetail.addParam('sysparm_month_count', length);
    monthlyCostDetail.getXML(getAllMonths);

    function getAllMonths(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert('response: ' + answer);
        for (var b = getMonth - 1; b < getMonth2; b++) {
            var array = JSON.parse(answer);
            array.forEach(function(obj) {


                if (allMonths[b] == obj.month) {
                    alert('2: ' + obj.month);
                    existingFixPrice.push(obj.price);
                }

            });
        }
alert('arr3: '+existingFixPrice);

        return existingFixPrice;
    }


alert('hello');


}
 
 
function fetChMonth() {
    var revised_date = gel('revised_kgs_end_date').value;
    var months = [

        "January", "February", "March", "April", "May", "June",

        "July", "August", "September", "October", "November", "December"
    ];
    var monthNumber = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
    var finalMonths = []
    var getMonth = 0;
    var getMonth2 = 0;

 var array = [];
    var jsonArray = [];

    if (revised_date == '') {
        alert('inside if 1');
        array= test(sysID, finalMonths.join(), finalMonths.length,getMonth,getMonth2,months);
        alert('inside if 4');


    }

alert('5');
    for (var k = 0; k < finalMonths.length; k++) {
        var jsonObj = {
            "month": finalMonths[k],
            "monthly_fixed_price_to_be_invoiced": array[k], // Generate random age between 20 and 70
            "state": ''
        };
        jsonArray.push(jsonObj);
    }
 
}
 
 
Thanks,
Ankita
1 REPLY 1

Bert_c1
Kilo Patron

getXMLWait() is still documented: GlideAjaxAPI getXMLWait yokohama. Is 64 OOB client scripts still using that in my yokohama PDI. query: sys_class_name=sys_script_client^scriptLIKEgetXMLWait(

So I think it is still safe to use. Maybe the script include method 'getFixPrice' needs some work to speed it up.