- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 12:13 PM - edited 05-22-2023 12:40 AM
Hi all,
I have a requirement in the catalog item.
There is a table where the data is stored. And in the catalog item, there are two fields.
Eg.: When "Joshua Tree" is selected in name, the capacity of that name is 32 according to the table data. So, in the capacity field, if the user enters the number more than 32, then it should show some alert and clear the value.
Same for "Emerald Bay". If "Emerald Bay" is selected in name, the capacity of that name is 28 according to the table data. So, in the capacity field, if the user enters the number more than 28, then it should show some alert and clear the value.
CATALOG CLIENT SCRIPT
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax("autoPopulateDetails"); //script include name
ga.addParam("sysparm_name", "getCapacity"); //function name
ga.addParam("sysparm_sysid", newValue); //paramter pass to server
ga.getXMLAnswer(setdetails); //callback funtion
var table_ref = g_form.getReference('u_room_name', getCapacity); //catalog room name backend value
function getCapacity(table_ref) {
var name_capacity = table_ref.u_capacity; //u_capacity is custom table Capacity Field back end value
if(newValue >= name_capacity) {
alert("maximum capacity exceeded");
g_form.clearValue('please_confirm_the_number_of_people_that_are_going_to_attend_your_meeting_on_site');
}
}
}
SCRIPT INCLUDE
var autoPopulateCapacity = Class.create();
autoPopulateCapacity.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCapacity: function() {
var val;
var sysid = this.getParameter("sysparm_sysid");
var grUsr = new GlideRecord("u_conference_room");//table name
grUsr.addQuery("sys_id", sysid);//filters with sysid
grUsr.query();
if (grUsr.next()) {
val = grUsr.u_capacity; //field name to get
return val;
}
},
type: 'autoPopulateCapacity'
});
How can I achieve this? Thanks in advance!
@Ankur Bawiskar @Community Alums @Karan Chhabra6 @Pradeep Sharma @Allen Andreas @Chuck Tomasi @Jaspal Singh @Musab Rasheed @Pavankumar_1 @Manmohan K
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 12:57 PM
Hi @2022_ServiceNow ,
Please try the below code.
Create an OnChange catalog client script in your catalog item on a variable capacity.
Code:-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2023 01:32 AM
update as this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var table_ref = g_form.getReference('u_room_name', getCapacity); //name is catalog name variable backend value
}
function getCapacity(table_ref) {
var name_capacity = parseInt(table_ref.u_capacity); //u_capacity is custom table Capacity Field back end value
if(parseInt(newValue) >= name_capacity) {
alert("maximum capacity exceeded");
g_form.clearValue('please_confirm_the_number_of_people_that_are_going_to_attend_your_meeting_on_site');
}
}
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2023 01:46 AM
u_room_name variable must be of reference type and pointing to that custom table
then it will work fine
Did you debug by adding alert?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2023 01:52 AM
Thank you Sir, it is working fine