How do I Pull a single variable from Script Include to Client Script?

Leo Ruggiero
Tera Contributor

Hey guys! I was typing up a client script to find how many tickets were open before the creation of the new ticket so that I can give our business a queue number. This number won't be changing or updating so I just need to let the users know how many tickets are in front of them once, during the opening of the ticket. So I used an onSubmit client script to find the count and then add that number to a field (so that I can use it in our email notifications to the user when they create a ticket).

function onSubmit() {
    var count=0;
    var queue = new GlideRecord('u_creative_services_ticketing');
    queue.addEncodedQuery("active=true^stateNOT IN5,4");
    queue.query();
    while(queue.next()){
        count++;
    }
    //gs.log(count);
    g_form.setValue('u_original_q_pos',count,true);
}

However I ended up getting an error on the ticket creation due to the addEncodedQuery, I supposed that is server sided only and not runnable on client.

So I want to make a Script Include instead. I'm super new to script includes but this is what I've typed up;

var getCSTQueueAjax = Class.create();
getCSTQueueAjax.prototype = {
    getCount: function() {
        var count = 0;
        var gr = new GlideRecord('u_creative_services_ticketing');
        gr.addEncodedQuery("active=true^stateNOT IN5,4");
        gr.query();
            while(gr.next()){
                count++;
            }
    },
    type: 'getCSTQueueAjax'
};

From here, how do I GlideAjax that information over to my OnSubmit client script so that I can set that field value to the count it finds? This is what I have so far based off of the research I've been able to do, but I'm getting pretty confused and lost after this.

function onSubmit() {
	
	//Script Include
	var ga = new GlideAjax('CST_Queue_Position_Counter');
	
	//Function calld upon
	ga.addParam('sysparm_name', 'getCount');
	
}

I'm just needing to grab that count value I set in the Script Include and g_form.setValue it.

Thank you in advance for your assistance I greatly appreciate it.

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Leo,

Shouldn't calling ajax in onSubmit. If needed, use before business rule instead.

View solution in original post

13 REPLIES 13

Aoife
Tera Guru
var getCSTQueueAjax = Class.create();

getCSTQueueAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  getCount: function() {
    var count = 0;
    var gr = new GlideRecord('u_creative_services_ticketing');
    gr.addEncodedQuery("active=true^stateNOT IN5,4");
    gr.query();
    while(gr.next()){ count++; }
    var results = { "count": count };
    return JSON.encode(results);
  }, 
  type: 'getCSTQueueAjax' 
};
function onSubmit() {
  var gs = new GlideAjax('getCSTQueueAjax');
  ga.addParam('sysparm_name', 'getCount');
  gs.getXML(parseResult);
}

function parseResult(response) {
  var answer = response.responseXML.documentElement.getAttribute('answer');
  if (answer) {
    var returnData = answer.evalJSON(true);

    g_form.setValue('u_original_q_pos', returnData.count);

  }
}

 

Aoife

This doesn't seem to work unfortunately. I had to also fix a syntax error at the end of the Script Include. The last line was }; but I changed it to }); since the open bracket at the top had not been closed. Perhaps where I'm closing the bracket is wrong.

Check the variable names on the onSubmit, looks like I had some typos there.  should all be ga, not gs.

Sorry I missed the ); at the end also, but you already caught that.

Main things you had missing, you did not extend AbstractAjaxProcessor and did not return anything.

Aoife.

Yeah I fixed the gs to ga too, this still isn't returning anything though when submitting a new ticket.

I've made sure it's client callable and also have tried changing accessible from to This application scope only and all application scopes