To get data from table and populate message in catalog item

2022_ServiceNow
Tera Expert

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.

2022_ServiceNow_0-1684696236990.png

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 

2 ACCEPTED SOLUTIONS

Mallidi Suma
Tera Guru

Hi @2022_ServiceNow ,

 

Please try the below code.

Create an OnChange catalog client script in your catalog item on a variable capacity.

Code:-

var table_ref = g_form.getReference('name',getCapacity); //name is catalog name variable 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('capacity');
}
Screenshot:- 
Screenshot 2023-05-22 at 1.23.48 AM.png
 
Please note that the catalog item name variable references your custom table name field.
 
Please mark my answer as helpful and accept it as a solution if it helps!!

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@2022_ServiceNow 

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');
	}
}   
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Mallidi Suma
Tera Guru

Hi @2022_ServiceNow ,

 

Please try the below code.

Create an OnChange catalog client script in your catalog item on a variable capacity.

Code:-

var table_ref = g_form.getReference('name',getCapacity); //name is catalog name variable 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('capacity');
}
Screenshot:- 
Screenshot 2023-05-22 at 1.23.48 AM.png
 
Please note that the catalog item name variable references your custom table name field.
 
Please mark my answer as helpful and accept it as a solution if it helps!!

Hi @Mallidi Suma 

 

Thank you for the response.

I tried with the above script. But it is not working

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 = 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');
	}
}   
}

u_room_name - name field backend value in the custom table

u_capacity - capacity field backend value in the custom table

 

Is it not required to add Script include here to reference the custom table? 

Ankur Bawiskar
Tera Patron
Tera Patron

@2022_ServiceNow 

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');
	}
}   
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar

 

I made the changes, but it is not working. To achieve this script include is not necessary? What other changes I have to make here. Can you please let me know? 

The capacity depends on the name selected.