Need assistance to fetch values from database to catalog item

Community Alums
Not applicable

Hi Experts,  I want to check if  depCorrelationID is lies in which array if it found in array1 then in client script i need to setValue of level_1_organization with depsysID, if it found in array2 then in client script i need to setValue of level_2_organization with depsysID  & if it found in array3 then in client script i need to setValue of level_3_organization with depsysID. 

 

means I want to fetch values from database and shows on catalog item. but scripts not working.

 

Script Include: Client callable checked

 

 

 

var testBack2Front = Class.create();
testBack2Front.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    returndepartment: function() {
        var par = this.getParameter("sysparm_catupdte1");
        var gr = new GlideRecord("cmdb_ci_appl");
        var array0 = ['2426A6145EXXXXXA', '9A07A06F5XXXXXD0', 'B814BCE55XXXXX13', '4C9496995XXXXXF4'];
        var array1 = [];
        var array2 = [];
        var array3 = [];

        if (gr.get(par)) {

            var depsysID = gr.getValue('department');
            var depname = gr.department.getDisplayValue();
            var depCorrelationID = gr.department.correlation_id.getDisplayValue();
        }

        var grci1 = new GlideRecord('cmn_department');
        grci1.addEncodedQuery('u_active=true^correlation_idISNOTEMPTY');
        grci1.addEncodedQuery('parent.correlation_idIN' + array0);
        grci1.query();
        while (grci1.next()) {
            array1.push(grci1.correlation_id.toString());
        }

        //correlation IDs from array1
        var grci2 = new GlideRecord('cmn_department');
        grci2.addEncodedQuery('u_active=true^correlation_idISNOTEMPTY');
        grci2.addEncodedQuery('parent.correlation_idIN' + array1.join(','));
        grci2.query();
        while (grci2.next()) {
            array2.push(grci2.correlation_id.toString());
        }
        //correlation IDs from array2
        var grci3 = new GlideRecord('cmn_department');
        grci3.addEncodedQuery('u_active=true^correlation_idISNOTEMPTY');
        grci3.addEncodedQuery('parent.correlation_idIN' + array2.join(','));
        grci3.query();
        while (grci3.next()) {
            array3.push(grci3.correlation_id.toString());
        }

        var answerObj = {
            depsysID: depsysID,
            depname: depname,
            depCorrelationID: depCorrelationID,
            array1: array1,
            array2: array2,
            array3: array3

        };
        return JSON.stringify(answerObj);  //CHECKED IN BG SCRIPT OBJECT IS GETTING CORRECT VALUES
    },

    type: 'testBack2Front'
});

 

 

 

 

My catalog on change client script :

 

 

 

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

    //Type appropriate comment here, and begin script below
    var gainst = new GlideAjax('testBack2Front');
    gainst.addParam('sysparm_name', 'returndepartment');
    gainst.addParam('sysparm_catupdte1', g_form.getValue('appl_name'));
    gainst.getXML(catupdateResponse1);

    function catupdateResponse1(response) {
        var answer2 = response.responseXML.documentElement.getAttribute("answer");
        var data = JSON.parse(answer2);


        if (data.depsysID == null) {
            g_form.setValue('level_3_organization', '', '');
            g_form.setValue('level_2_organization', '', '');
            g_form.setValue('level_1_organization', '', '');

        } else if (data.array3.includes(data.depCorrelationID)) {

          //  g_form.setValue('level_3_organization', '', '');
           // g_form.setValue('level_3_organization', data.depsysID, data.depname);
               g_form.setValue('level_3_organization', data.depsysID);

        } else if (data.array2.includes(data.depCorrelationID)) {

          //  g_form.setValue('level_2_organization', '', '');
          //  g_form.setValue('level_2_organization', data.depsysID, data.depname);
             g_form.setValue('level_2_organization', data.depsysID);

        } else if (data.array1.includes(data.depCorrelationID)) {

           // g_form.setValue('level_1_organization', '', '');
          //  g_form.setValue('level_1_organization', data.depsysID, data.depname);
            g_form.setValue('level_1_organization', data.depsysID);

        }
    }
}

 

 

 

please assist, FYI Script include object is getting correct values. I checked in background script.

1 REPLY 1

Mayur2109
Kilo Sage
Kilo Sage

Hi @Community Alums ,

 

Please check if you are getting correct data in variable data, also check backend name of org level fields. Check for any errors on console, try to put logs and check your are getting correct values.

 

Please check and Mark Helpful and Correct if it really helps you.

Regards,
Mayur Shardul