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

The field is set to integer. Would that be part of the problem?

Should not be, ServiceNow handles that pretty well.  But I think I may see the problem now that I have a minute:

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();
    count = gr.getRowCount();
    var results = { "count": count };
    return JSON.stringify(results);
  }, 
  type: 'getCSTQueueAjax' 
});
function onSubmit() {
  var ga = new GlideAjax('getCSTQueueAjax');
  ga.addParam('sysparm_name', 'getCount');
  ga.getXMLAnswer(parseResult);
}

function parseResult(answer) {
  if (answer) {
    var returnData = JSON.parse(answer);

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

  }
}

 

Hopefully that will work now.

Aoife

Ahhhh yes! This worked, I assume the main change here is the JSON.parse and not the count++ being changed to getRowCount?

Though running into one more issue now.

While I did see it post the correct count to the field, after the ticket saved and reloaded, it no longer had the count in it. Is there a way to prevent this? I need that count to remain in the field.

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

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Leo,

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