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

Hi @varaprasad123 

Suggestion code 1 and 2 did not work so I try suggestion code 3 and I'm still could not get it to work. could you please help review what I have done, could be some line I missed type:

CATALOG CLIENT SCRIPT:

Annie10_1-1691990075982.png

 

SCRIPT INCLUDE:

 

Annie10_0-1691989775647.png

 

 

 

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @Annie10 ,

                               I have same kind of Script it might help you, Lookup select box referring to sys_user, lookup value is department, so once we select any department in look value, it will display the count of associated users belonging to that selected department. ref script

Client Script : (onChange on Lookup select box) 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	
    var ga = new GlideAjax('demoScriptInclude'); // Replace with your Script Include name
    ga.addParam('sysparm_name', 'getSysUserCount'); // Replace with your server-side method name
    ga.addParam('sysparm_selected_dept', g_form.getValue('select_dept')); // pass lookup select box value
    ga.getXMLAnswer(function(response) {
	
       // Now you have the total count of sys_user records, you can use it for your conditions
        alert('Total Sys User Count:', response);
    });

}

Script Include : It should be client callable

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

 getSysUserCount: function() {
        var selectedDept = this.getParameter('sysparm_selected_dept');
        gs.info("Selected dept is"+ selectedDept);
        var sysUserGR = new GlideRecord('sys_user');
        sysUserGR.addQuery('department', selectedDept);
        sysUserGR.query();
        
        var associatedSysUserCount = 0;
        while (sysUserGR.next()) {
            associatedSysUserCount++;
        }
gs.info("Selected dept count is"+ associatedSysUserCount);
        return associatedSysUserCount.toString();
    },
    
    type: 'demoScriptInclude'
});

O/P

ChetanMahajan_0-1691985886982.png

Kindly mark correct and helpful if applicable

 

 

 

Hello @Chetan Mahajan 

Thank you for sharing your codes.  I'm totally lost after reviewing the codes.

 

Catalog Client Script - OnLoad:

function onLoad() {
   var ga = new GlideAjax('demoScriptInclude'); // Replace with your Script Include name
    ga.addParam('sysparm_name''getSysUserCount'); // Replace with your server-side method name
    ga.addParam('sysparm_selected_dept', g_form.getValue('number_of_books')); // pass lookup select box value
    ga.getXMLAnswer(function(response) {
    
       // Now you have the total count of sys_user records, you can use it for your conditions
        alert('Total Sys User Count:', response);
    });
 
However, for Script Include part, I do not know how to change the code.
so that after from loaded, show an Alert with the total count in the lookup box
 
Could you please help?  Thank you again.

 

 

 

 

 

 

Hello @Annie10 ,

                              could you please share snapshot of your CatLog item where Lookup box is placed ? Because select lookup box capability select single value. so which count you are expecting. are you looking for count of u_book table on catalog form ?

Hi @Chetan Mahajan 

This catalog item is still under developing.  Here are the steps we have done so far.

VARIABLES:

Annie10_2-1692022969327.png

 

SELF-SERIVCE PORTAL:

Annie10_1-1692022920503.png

The workflow should be similar as your.  Once the form loaded, it shows the number of books that the current student has in Number of Book field. 

 

When we select a different user, number of books changes accordingly.

 

We need help on the total count of the number of book such as an Alert so that we can use it to develop further programming logic.

 

Thank you again for your support.