Code is not working: I need to update total created requests count in a catalog variable.

ROS4349
Tera Expert

Hi Folks,

Below are the Script include and Onload Catalog Client script.

I have a catalog single line text type variable "available_slots_for_today".

 

Requirement:

There is a group they can fullfil daily 10 request only.

When user is creating a request we need to show a field with how many requests created today and how many slots are available.

 

For that I need to update total created request count in current date. And to extend to this the request creation should not be exceed more that 10 request from a catalog item.

Script include:

------------------------------
var RecordCounter = Class.create();
RecordCounter.prototype = {
    initialize: function() {},

    getRecordCount: function() {
        var gr = new GlideRecord('sc_request');
        gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
        gr.query();
     return gr.getRowCount();
    },
type: 'RecordCounter'
};
====================================================
Client Script:
 
function onLoad() {
    var ga = new GlideAjax('RecordCounter');
    ga.addParam('sysparm_name', 'getRecordCount');
    ga.getXMLAnswer(function(response) {
        g_form.setValue('available_slots_for_today', response);
    });
}
==============================================
 
Can you please help me in correcting above codes as per my requirement.
Attaching a requirement screenshot for your reference.
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ROS4349 

your script include should be client callable and then it will work. Currently it's not.

Ensure the client callable checkbox is true and update script as this

AnkurBawiskar_0-1704454370860.png

 

var RecordCounter = Class.create();
RecordCounter.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getRecordCount: function() {
		var gr = new GlideRecord('sc_request');
gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
		gr.query();
		return gr.getRowCount();
	},

	type: 'RecordCounter'
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Rahul Talreja
Mega Sage
Mega Sage

Hi @ROS4349 ,

Code seems to be pretty fine. Can you share the output? 

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Not getting any output. 

available_slots_for_today variable shows empty.

Ankur Bawiskar
Tera Patron
Tera Patron

@ROS4349 

your script include should be client callable and then it will work. Currently it's not.

Ensure the client callable checkbox is true and update script as this

AnkurBawiskar_0-1704454370860.png

 

var RecordCounter = Class.create();
RecordCounter.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getRecordCount: function() {
		var gr = new GlideRecord('sc_request');
gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
		gr.query();
		return gr.getRowCount();
	},

	type: 'RecordCounter'
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you @Ankur Bawiskar.

Can you pls help me for below:

 

User should not select the date if 10 requests created today from a catalog item.

ROS4349_0-1704454852745.png

As per above screenshot it should show avalable slots to create record out of 10 requests.

if the requests count reaches to 10 per day, then user should get the populated error. And user should not select the date.