On change client script not calling script Include

Nivedita9
Tera Contributor

Hi ,
I have to add choices to a particular field on change of its dependent field. I wrote on change and script include but on change is not triggering it however I'm able to get desired result from background script. I have deactivated as of now but it's not working when it is active
Below is screenshot:

Nivedita9_0-1718120810190.png


Nivedita9_1-1718120830776.png

 

 

1 ACCEPTED SOLUTION

Yes, Use the script include code that I have already provided earlier.

View solution in original post

11 REPLIES 11

Robbie
Kilo Patron
Kilo Patron

@Nivedita9,

 

So I can quickly debug this on my PDI, can you copy and paste both the onLoad script and Script Include (not screen shots) please.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.


Thanks, Robbie

Nivedita9
Tera Contributor
var TestAJAX = Class.create();
TestAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 getOptions: function() {
        gs.addInfoMessage('script include triggered');
       capexFun = this.getParameter('sysparm_val');
        capexObj = this.getParameter('sysparm_sys_id');
        gs.log(capexFun + capexObj, '144');
        var choice = {};
        var choiceArr = [];
        var choiceGr = new GlideRecord('sys_choice');
        choiceGr.addEncodedQuery('name=dmn_demand^element=u_sub_objective^inactive=false');
        choiceGr.query();
        while (choiceGr.next()) {
			gs.print(capexFun +capexObj);			 
            if (capexFun == "Production" || capexFun == "Logistics and Distribution") {
                if (capexObj == "5") {
                    gs.log("Coming in the loop", '144');
                    choice['Efficiency'] = choiceGr.getValue('value');
                    choice['Civil'] = choiceGr.getValue('value');
                } else if (capexObj == '1') {
                    choice['Topline'] = choiceGr.getValue('value');
                }

            } else if (capexFun == 'D&T_capex') {

                choice['Fridges'] = choiceGr.getValue('value');
                choice['Fire Safety'] = choiceGr.getValue('value');
            }
            choiceArr.push(choice);

            return JSON.stringify(choiceArr); // Return a stringified array to the client

        }
    },
    type: 'TestAJAX'
});
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        //return;
    }
    var capexfunction = g_form.getValue('u_capex_function');
    var capexObjective = g_form.getValue('u_capex_objective');

    var ga = new GlideAjax('global.TestAJAX');
    //Pass the name of the method and the parameter
    ga.addParam('sysparm_name', 'getOptions');
    ga.addParam('sysparm_val', capexfunction);
    ga.addParam('sysparm_sys_id', capexObjective);
    ga.getXMLAnswer(setChoices);

    function setChoices(answer) {
        if (answer) {
            alert("Answer--" + answer);
            var choiceArray = JSON.parse(answer);
            for (i = 0; i < choiceArray.length; i++) {
                g_form.addOption(targetChoiceField, choiceArray[i].value, choiceArray[i].label, i + 1);

            }
        }
    }

}

HrishabhKumar
Kilo Sage

Hi @Nivedita9 ,

Try doing a modification in line number 8 of your client script where using GlideAjax. 

Maybe you're using wrong name in GlideAjax API. Use the below line instead.

// var ga = new GlideAjax('TestAJAX');

 

Thanks,

Hope this helps you, let me know!

Hi
I tried it.But not working