Unable to save the form due to client script

sasi
Tera Contributor

Hi team,

 

i am unable to save the form due to agent is mandatory and setting back to none after saving the form.

My requirement is to add/remove options based on group. when ABC group is selected there are other two fields called agent and target based on agent selected target has to update the options.

i have created script include and client script but after saving it returning to none

 

script include:

returnIncGroup: function() {
var caseId = this.getParameter('group_id');
var gr = new GlideRecord('table_name');
gr.get(caseId);
if (caseId == gs.getProperty('propertyofgroupname')) {
return true;
} else {
return false;
}
},

 

onchange client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var grp = g_form.getValue('assignment_group');
    var case_clasif = g_form.getValue('u_target');
    var scr = new GlideAjax('scriptincludename');
    scr.addParam('sysparm_name', 'returnIncGroup');
    scr.addParam('group_id', grp);
    scr.getXML(callback);
 
    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer=='true'){
g_form.clearOptions('u_target');
        var agnt = g_form.getValue('u_agent');
        if (agnt == '1') {
            g_form.addOption('u_target', '', '-- None --');
            g_form.addOption('u_target', 'Non Associate');
        } else if (agnt == '2') {
         
            g_form.addOption('u_target', '', '-- None --');
            g_form.addOption('u_target', 'Centrally Block Prescriber', 'Block');
            g_form.addOption('u_target', 'Request');
        } else if (agnt == '3') {
       
            g_form.addOption('u_target', '', '-- None --');
            g_form.addOption('u_target', 'Facility');
        }
}
    }
}
4 REPLIES 4

Peter Bodelier
Giga Sage

Hi @sasi,

 

Are these values also available as choices for the field?

Consider switching around your logic. Remove the values from the list you don't want to show by client script. And leave the ones that should be there.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

yes..but there plenty options to remove that is the reason i have choosed this method.

I understand, unfortunately the easiest option is not always the best. 😉


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Kavita Bhojane2
Tera Expert

 

Here is my script. First remove all the options, and then adds back what is needed depending on the Agent selected.

 

script include:

returnIncGroup: function() {
var caseId = this.getParameter('group_id');
var gr = new GlideRecord('table_name');
gr.get(caseId);
if (caseId == gs.getProperty('propertyofgroupname')) {
return true;
} else {
return false;
}
},

 

onchange client script:

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

if (newValue != oldValue) {

g_form.removeOption('u_target','Non Associate');

g_form.removeOption('u_target','Block');

g_form.removeOption('u_target','Facility');

g_form.removeOption('u_target','xyz');

.

.

.

.

g_form.removeOption('u_target','xyz');

}

    var grp = g_form.getValue('assignment_group');
    var case_clasif = g_form.getValue('u_target');
    var scr = new GlideAjax('scriptincludename');
    scr.addParam('sysparm_name', 'returnIncGroup');
    scr.addParam('group_id', grp);
    scr.getXML(callback);
 
    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer=='true'){
g_form.clearOptions('u_target');
        var agnt = g_form.getValue('u_agent');
        if (agnt == '1') {
            g_form.addOption('u_target', '', '-- None --');
            g_form.addOption('u_target', 'Non Associate');
        } else if (agnt == '2') {
         
            g_form.addOption('u_target', '', '-- None --');
            g_form.addOption('u_target', 'Centrally Block Prescriber', 'Block');
            g_form.addOption('u_target', 'Request');
        } else if (agnt == '3') {
       
            g_form.addOption('u_target', '', '-- None --');
            g_form.addOption('u_target', 'Facility');
        }
}
    }
}

Please mark my answer correct if you find it helpful.

 

Thanks,

Kavita Bhojane