Catalog Client Script to populate variable

BR Admin
Tera Contributor

Hello All, 

 

I'm trying to write a simple message in a single line text variable based on if a record is true or false. I'm using this within an onLoad catalog client script. I know I'm missing something very basic and likely writing this all wrong. Any assistance is greatly appreciated. 

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   
   //set name of notification
   var notifname = 'b6ff52e947513d9086861192e36d43eb';
   
   //look up notification based on notif name
   var notifon = new GlideRecord('sysevent_email_action');
   notifon.addQuery ('sys_id' , notifname);
   notifon.addQuery ('active' , 'true');
   notifon.query;

   var notifoff = new GlideRecord('sysevent_email_action');
   notifoff.addQuery ('sys_id' , notifname);
   notifoff.addQuery ('active' , 'false');
   notifoff.query;


if(notifon.next()) {
	g_form.setValue('is_short_staffed_message_on', "Short Staffed msg is ON");
	}
else if (notifoff.next()) {
	g_form.setValue('is_short_staffed_message_on', "Short Staffed msg is OFF");
	}

}

 

1 ACCEPTED SOLUTION

Ashutosh C J
Tera Guru

Hi @BR Admin 
The problem here is, you cannot do server side operation in client script. You should use GlideAjax for this
Please use this 
Create a client callable script include and use the following script:

 

 

 

getNotificationFlag: function() {
        var notifname = "b6ff52e947513d9086861192e36d43eb";
        var notifon = new GlideRecord('sysevent_email_action');
        notifon.addQuery('sys_id', notifname);
        notifon.addQuery('active', 'true');
        notifon.query();
		if(notifon.next()){
			return "true";
		}
		else
		return "false";
    },

 

 

 

AshutoshCJ_1-1714409370888.png

 

Modify your client script like this

 

 

 

function onLoad() {
    var ga = new GlideAjax('TestAjaxScriptInclude'); //Please use your script include
    ga.addParam('sysparm_name', 'getNotificationFlag');
    ga.getXMLAnswer(function(response) {
        if (response == "true") {
            g_form.setValue('is_short_staffed_message_on', "Short Staffed msg is ON");
        } else if(response == "false")
            g_form.setValue('is_short_staffed_message_on', "Short Staffed msg is OFF");
    });

}

 

 

 

 



View solution in original post

1 REPLY 1

Ashutosh C J
Tera Guru

Hi @BR Admin 
The problem here is, you cannot do server side operation in client script. You should use GlideAjax for this
Please use this 
Create a client callable script include and use the following script:

 

 

 

getNotificationFlag: function() {
        var notifname = "b6ff52e947513d9086861192e36d43eb";
        var notifon = new GlideRecord('sysevent_email_action');
        notifon.addQuery('sys_id', notifname);
        notifon.addQuery('active', 'true');
        notifon.query();
		if(notifon.next()){
			return "true";
		}
		else
		return "false";
    },

 

 

 

AshutoshCJ_1-1714409370888.png

 

Modify your client script like this

 

 

 

function onLoad() {
    var ga = new GlideAjax('TestAjaxScriptInclude'); //Please use your script include
    ga.addParam('sysparm_name', 'getNotificationFlag');
    ga.getXMLAnswer(function(response) {
        if (response == "true") {
            g_form.setValue('is_short_staffed_message_on', "Short Staffed msg is ON");
        } else if(response == "false")
            g_form.setValue('is_short_staffed_message_on', "Short Staffed msg is OFF");
    });

}