Need help with getting a total count value in Lookup Select Box variable.

Annie10
Tera Contributor

Hello,

Our Catalog Item contains a Lookup Select Box variable which getting the data from a custom table(u_books)

We need help on getting the total count of the lookup box value after a form loaded so that we can write conditions.

Could someone please provide suggestion?

we appreciate your help

 

12 REPLIES 12

varaprasad123
Kilo Guru

Can you try the following onload script ?

 

// This script assumes you have identified the correct field name or sys_id for your Lookup Select Box.

// Attach the onLoad event to the catalog item form
function onLoad() {
// Replace 'lookup_box_field' with the actual field name or sys_id of your Lookup Select Box
var lookupBoxField = 'lookup_box_field';

// Get the reference to the Lookup Select Box field using g_form
var lookupBox = gel(lookupBoxField);

// Get the selected options in the Lookup Select Box
var selectedOptions = lookupBox.options;

// Calculate the total count of selected options
var totalCount = 0;
for (var i = 0; i < selectedOptions.length; i++) {
if (selectedOptions[i].selected) {
totalCount++;
}
}

// Display the total count (you can modify this to suit your needs)
console.log('Total selected books:', totalCount);
}

// This function retrieves an element by its ID
function gel(id) {
return document.getElementById(id);
}

Hello @varaprasad123 

Where exactly do I enter the script?  Is it under Catalog Client Script?

Thank you 

Hi @varaprasad123 

I went ahead and added the following code to Catalog Client Script. 

 
function onLoad() {
    var lookupBoxField = '7580c5e29720b110069cbbe3a253afe5'//Sys ID of the lookup Box  
    var lookupBox = gel(lookupBoxField);
    var selectedOptions = lookupBox.options;
   
    var totalCount = 0;
    for (var i = 0; i < selectedOptions.length; i++) {
        if (selectedOptions[i].selected) {
            totalCount++;
        }
    }
      alert('Total selected books:', totalCount);
}
function gel(id) {
    return document.getElementById(id);
}
 
However, I'm getting this error in the Service Catalog during the form loading.
Annie10_0-1691981456689.png

 

Could you please help?  Thank you

 

btw, where you want to show the total number ?  following should help you getting an alert of total number

 

function onLoad() {
var lookupBoxField = '7580c5e29720b110069cbbe3a253afe5'; //Sys ID of the lookup Box
var lookupBox = gel(lookupBoxField);
var selectedOptions = lookupBox.options;

var totalCount = 0;
for (var i = 0; i < selectedOptions.length; i++) {
if (selectedOptions[i].selected) {
totalCount++;
}
}
alert('Total selected books: ' + totalCount);
}

function gel(id) {
return document.getElementById(id);
}

 

If the gel(id) function is not working as expected in your client-side script, you can try using the document.querySelector() method as an alternative. Here's how you can modify your code to use document.querySelector() instead of gel(id):

function onLoad() {
var lookupBoxSelector = '#7580c5e29720b110069cbbe3a253afe5'; // Sys ID of the lookup Box
var lookupBox = document.querySelector(lookupBoxSelector);
var selectedOptions = lookupBox.options;

var totalCount = 0;
for (var i = 0; i < selectedOptions.length; i++) {
if (selectedOptions[i].selected) {
totalCount++;
}
}
alert('Total selected books: ' + totalCount);
}

 

If above doesn't work, let's go with GlideAjax following solution.

Step1: create a script include

var BookCatalogUtils = Class.create();
BookCatalogUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
countSelectedBooks: function() {
var lookupBoxField = this.getParameter('lookupBoxField'); // Sys ID of the lookup Box
var lookupBox = new GlideAggregate(lookupBoxField);
lookupBox.addQuery('selected', true);
lookupBox.query();
var totalCount = 0;
while (lookupBox.next()) {
totalCount++;
}
return totalCount.toString();
},

type: 'BookCatalogUtils'
});

 

step2: update/modify your onload catalog client script

 

 

function onLoad() {
var lookupBoxField = '7580c5e29720b110069cbbe3a253afe5'; // Sys ID of the lookup Box

var ga = new GlideAjax('BookCatalogUtils');
ga.addParam('sysparm_name', 'countSelectedBooks');
ga.addParam('lookupBoxField', lookupBoxField);
ga.getXML(function(response) {
var totalCount = response.responseXML.documentElement.getAttribute('answer');
if (totalCount) {
alert('Total selected books: ' + totalCount);
}
});
}