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

Hi @Nivedita9 ,

I have done following modifications in your script include code

1) return statement was in the while bracket, it should be outside the while block in the function block.

2) choice object should be created inside the while block.

3) commented out the infoMessage code.

 

Here is the modified code below.

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 choiceArr = [];
        var choiceGr = new GlideRecord('sys_choice');
        choiceGr.addEncodedQuery('name=dmn_demand^element=u_sub_objective^inactive=false');
        choiceGr.query();
        while (choiceGr.next()) {
			var choice = {};
			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').toString();
                    choice['Civil'] = choiceGr.getValue('value').toString();
                } else if (capexObj == '1') {
                    choice['Topline'] = choiceGr.getValue('value').toString();
                }

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

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

		return JSON.stringify(choiceArr); // Return a stringified array to the client
    },
    type: 'TestAJAX'
});

 

Thanks,

Let me know if it worked.

Hi,
I did try this. It is giving me output from script include but not adding adequate options.
Can you let me know in script include is that right way to set label and value and accessing it at client side?

Hi @Nivedita9 

In line no 20 of your client Script, you are trying to access "value" and "label" property from the objects in the array, these properties are not present in the object. 

Because in the script include where you set the object and array you are setting properties like "Efficiency" "Civil" "Fridges" "Topline" and "Fire safety". "value" or "label" is never set. 

Kindly pay attention to that and make the necessary changes.

 

Thanks.

Yes.You got it right. I need your help here.
I need to send response dynamic if capexFun is production and capexobj is efficiency I want to add 2 options - equipement and civil.If capexObj is topline the I want to add Top lline option.
How can I build this?

Hi @Nivedita9 ,

you can use something like this in the client script addOption logic.

There is a placeholder "fields_backend_name" : add the backend name of the field here.

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++) {
                if(choiceArray[i].Efficiency){
                    g_form.addOption('fields_backend_name', choiceArray[i].Efficiency, choiceArray[i].Efficiency, i);
                    g_form.addOption('fields_backend_name', choiceArray[i].Civil, choiceArray[i].Civil, i + 1);
                }
                else if(choiceArray[i].Topline){
                   g_form.addOption('fields_backend_name', choiceArray[i].Topline, choiceArray[i].Topline, i);
                }
                else{
                   g_form.addOption('fields_backend_name', choiceArray[i].Fridges, choiceArray[i].Fridges, i);
                }
                

            }
        }
    }

}