Client Script Glide Ajax condition check

purdue
Kilo Sage

Hello,

 

 I would like to change the when to run condition on this client script from hardcoded to query incident gliderecord and return true if record matches category and assigned to groups.   The client script is using GlideAjax to check another value.   This part is working I would just like to surround this with another GlideAjax to check condition.  In other words take this 

if (g_form.getValue('category') == 'transmission' && g_form.getValue('assignment_group') == '780847e51ba07910783f43b1b24bcb91' || g_form.getValue('assignment_group') == 'c13ba3c11bd0f1106bf5535b234bcbc3' || g_form.getValue('assignment_group') == '644b63c11bd0f1106bf5535b234bcbfe'). and change into if(answer).

====================================================

Script Include: ITSMAjaxUtils

Function: property_util: function(){
var answer = '';
var inc_id = this.getParameter('sysparm_inc_id');
var incident = new GlideRecord('incident');
var encquery = 'category=transmission^assignment_group=c13ba3c11bd0f1106bf5535b234bcbc3^ORassignment_group=644b63c11bd0f1106bf5535b234bcbfe^ORassignment_group=780847e51ba07910783f43b1b24bcb91';
incident.addQuery("sys_id", inc_id);
incident.addEncodedQuery(encquery);
incident.query();
if (incident.next())
{
answer = "true";
}else{
answer = " ";
}
return answer;

},
 
++++++++++++++++++
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//if (g_form.getValue('category') == 'transmission' && g_form.getValue('assignment_group') == '780847e51ba07910783f43b1b24bcb91' || g_form.getValue('assignment_group') == 'c13ba3c11bd0f1106bf5535b234bcbc3' || g_form.getValue('assignment_group') == '644b63c11bd0f1106bf5535b234bcbfe')

var curSysid2 = g_form.getUniqueValue();
var ga = new GlideAjax('ITSMAjaxUtils');
ga.addParam('sysparm_name', 'property_util');
ga.addParam('sysparm_inc_id', curSysid2);
ga.getXML(callback);
function callback(answer){
var answer2 = response.responseXML.documentElement.getAttribute("answer");

if (answer2)
{
var curSysid = g_form.getUniqueValue();
var ga = new GlideAjax('ITSMAjaxUtils');
ga.addParam('sysparm_name', 'getIncImpactedCountries');
ga.addParam('sysparm_inc_id', curSysid);
ga.getXML(handleResponse);

function handleResponse (response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == '0')
{
 
 
alert('Please attach all impacted countries to the incident form before updating the incident state');
return false;
 
}
}
}}
//Type appropriate comment here, and begin script below
 
}
 
Thanks for any assistance.
Chad
4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Hi Chad,

I'm not following exactly why on earth you would want to do this, especially since it means a call to the server on every change of whatever field, even though it might not be needed.  In any event, keep it simple with one trip to the server and back.  Have the first function call the second function before returning an answer.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    
    var curSysid = g_form.getUniqueValue();
    var ga = new GlideAjax('ITSMAjaxUtils');
    ga.addParam('sysparm_name', 'property_util');
    ga.addParam('sysparm_inc_id', curSysid);
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == '0') {
            alert('Please attach all impacted countries to the incident form before updating the incident state');
            //return false;  //not applicable in onChange script
        }
    }
 }
var ITSMAjaxUtils = Class.create();
ITSMAjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    property_util: function() {
        var answer = '';
        var inc_id = this.getParameter('sysparm_inc_id');
        var incident = new GlideRecord('incident');
        var encquery = 'category=transmission^assignment_group=c13ba3c11bd0f1106bf5535b234bcbc3^ORassignment_group=644b63c11bd0f1106bf5535b234bcbfe^ORassignment_group=780847e51ba07910783f43b1b24bcb91';
        incident.addQuery("sys_id", inc_id);
        incident.addEncodedQuery(encquery);
        incident.query();
        if (incident.next()) {
			answer = getIncImpactedCountries(inc_id);
        }
		
		return answer;
	},
	
	getIncImpactedCountries: function(inc_id) {
		//your existing working function, including the return - which will be returned to the previous function in this case instead of back to the client.
	},
    type: 'ITSMAjaxUtils'
});

Hello Brad,

Appreciate the response.  I tried the code but did not prompt the alert.  The alert should fire when Incident state changes, Category is Transmission, and is assignment group is one of three groups.  See attached screenshots.

 

Thanks,

Chad

Hi,

Try printing the answer first in alert, then based on the received response you can do the rest things.

 

alert(answer); // this will give you whatever you have received from server.

 

Thanks.

purdue
Kilo Sage

Hello,

We are going to go another direction so this is no longer needed.   Appreciate all the responses.

Thanks,

Chad