scripts

SIVAKARTHICS
Tera Contributor

SIVAKARTHICS_0-1753722604998.pngSIVAKARTHICS_1-1753722621083.png

this is my catlog item and table . i need block the user from submitting the catlog item if the there is same room with same start and end date is already present in the table .. some one pls give me entire code . i have tried for 2 days but still not working

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@SIVAKARTHICS 

remember these points:

1) Asynchronous GlideAjax won't work as by the time ajax function returns the value your form will get submitted

2) Synchronous GlideAjax is not allowed in portal and scoped application

Approach: Please use onChange catalog client script on Start and End and then use GlideAjax, if validation fails then show error message on that variable

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

4 REPLIES 4

anshul_goyal
Kilo Sage

Hello @SIVAKARTHICS,

Can you please attach your screenshots again?
As there is some problem going on in the community.

Regards,

Ankur Bawiskar
Tera Patron
Tera Patron

@SIVAKARTHICS 

remember these points:

1) Asynchronous GlideAjax won't work as by the time ajax function returns the value your form will get submitted

2) Synchronous GlideAjax is not allowed in portal and scoped application

Approach: Please use onChange catalog client script on Start and End and then use GlideAjax, if validation fails then show error message on that variable

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

@SIVAKARTHICS 

Hope you are doing good.

Did my reply answer your question?

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

Gaurav Shirsat
Mega Sage

Hello @SIVAKARTHICS 

 

Please understand the Technical Glitch here:

User is going to select the date from the Browser, and you are about to compare the date which is already there in tables record.

when you pass the date from client script to server, system do not have any provision to compare with it as it uses server's date and time using available APIs.

 

I am Providing you sample code :

 

Client Script : on Submit, based on your field add appropriate field
if you do this using on Change, then You need to write 2 client script for 2 variable. here you can use g_form.clearValue() and g_form.showFieldMsg

var room = g_form.getValue(''); // add your field name
var startDate = g_form.getValue('start_date'); // Replace with your field name
var endDate = g_form.getValue('end_date'); // Replace with your field name

// Call server-side GlideAjax
var ga = new GlideAjax('RoomBooking');
ga.addParam('sysparm_name', 'checkRoomAvailability');
ga.addParam('sysparm_room', room);
ga.addParam('sysparm_start_date', startDate);
ga.addParam('sysparm_end_date', endDate);

var result = ga.getXMLWait();
var answer = ga.getAnswer();

if (answer === 'true') {
g_form.showFieldMsg('room', 'This room is already booked for the selected date range.', 'error');
return false;
}

return true;
}

 

Script Include:


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

checkRoomAvailability: function() {
var room = this.getParameter('sysparm_room');
var startDate = this.getParameter('sysparm_start_date');
var endDate = this.getParameter('sysparm_end_date');

var gr = new GlideRecord(''); // add your table name
gr.addQuery('room', room);
gr.addQuery('start_date', startDate);
gr.addQuery('end_date', endDate);
gr.query();

if (gr.hasNext()) {
return 'true'; // Conflict exists
}

return 'false'; // No conflict
}

});

 

I hope, this might help you

 

Thanks and Regards

Gaurav Shirsat