Display value in ListCollector based of multipe conditions

sanvi
Tera Expert

Hi All,

I have a list collector variable on form for which look up table is say 'internal table'.

The variable should display values based on below conditions.

1) If requester is part of VIP department then ------> display all the device from 'Internal table' which has 'availablility' -- 'available', 'not available','both'.

2) If requester is not part of VIP department then ----> display only devices which has availability ---- 'available', 'both'.

 

i have configured the below condition for 2nd part but i am not sure how to include the first condition.

javascript: 'availability=both^ORavailability=available^u_status=active^' + "u_brand="+current.variables.preferred_brand;

 

Can anyone suggest how to filter data using both of the conditions 1 and 2.

10 REPLIES 10

Sonam_Tiwari
Kilo Sage

Hi @sanvi ,

 

Not very sure, but I believe these 2 are mutually exclusive conditions and you won't be able to combine them. Better go via script an populate based on the logic that you have mentioned. There, you can check if user is vip then return all records that satisfy 1st conditon else if not then all the records that satisfy the second set of condition. 

Basically, put that in a query variable and pass it in your glide based in if user is vip or not.

 

Or a less preferable one, have 2 list collectors each configured with one these conditions and based on vip/non-vip, control the visibility.

Consider indicating the response as helpful and marking it as correct if it meets your needs.

Hi @Sonam_Tiwari ,

Could you please help me with the script as i have not used it anytime not sure how to call script and pass variables in the reference qualifier.

Amit Verma
Kilo Patron
Kilo Patron

Hi @sanvi 

 

You could refer below blog for help in scripting. Moreover, I have made a skeleton of the script include required to get this done. You can refer it and proceed accordingly.

https://www.servicenow.com/community/developer-blog/dynamically-set-list-collector-on-change-of-vari...

 

var checkDeviceAvailability = Class.create();
checkDeviceAvailability.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	checkDeviceAvailable : function(){
		var listCollectorValues = [];
		var vipUser = '';
		var user = this.getParameter('sysparm_user'); // sysid of user record
		var gr = new GlideRecord('internal table name'); // query the internal table
        var usr = new GlideRecord('sys_user'); //query the user table
        usr.addQuery('sys_id', user); //filter the record with sysid
        usr.addEncodedQuery('vip=true'); //to check user VIP or not
        usr.query();
		
        if (usr.next()) {
			vipUser = 'yes'; 
			}
        else {
			vipUser = 'no';	
		}
		
		if(vipUser == 'yes'){
			gr.addQuery('availability=both^availability=available^availability=not available');
		}
		else if (vipUser == 'no'){
			gr.addQuery('availability=both^availability=available');
		}
		
		gr.query();
		
		while(gr.next()){
				listCollectorValues.push(gr.device.toString());
		}
		return listCollectorValues.toString();
	},
	
    type: 'checkDeviceAvailability'
});

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Sonam_Tiwari
Kilo Sage

Hey @sanvi ,

 

You can do something like : javascript: 'sys_idIN'+new DeviceQueryScriptInclude().getDeviceSysIds(current.u_brand,current.caller_id.toString()); // instead of caller pass the requestor as per your use case

 

and build a script include:

 

Here's a rough sample for reference:

 

I was trying to do using incident and asset table. You will have to change it to your internal table name instead of asset table and take it further.

 

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

    isUserVIP: function(requestorSysId) {
        var userGr = new GlideRecord('sys_user');
        if (userGr.get(requestorSysId)) {
            return userGr.getValue('vip') == true;
        }
        return false; // Handle the case where the user record is not found
    },

    getDeviceSysIds: function(brand,requestorSysId) {
		gs.info('brand '+brand)
        var isVIP = this.isUserVIP(requestorSysId);
        //var query = 'u_status=active^';
var query='';
        if (isVIP) {
            query += 'install_status=1^ORinstall_status=6^ORinstall_status=10'+'^'; // your availability logic here
        } else {
            query += 'install_status=1^ORinstall_status=6^'; // if not vip
        }

        if (brand) {
            query += 'company=' + brand; // company should be switched with the backend name of your preferred brand field
        }

        var gr = new GlideRecord('alm_asset'); // your internal table name here
		gs.info('query'+query)
        gr.addEncodedQuery(query);
        gr.query();

        var sysIds = [];
        while (gr.next()) {
            sysIds.push(gr.getUniqueValue());
			//gs.info(gr.model.display_name+' '+gr.install_status) // install status should be switched with your availability field
        }

        return sysIds;
    },

    type: 'DeviceQueryScriptInclude'
});

 

If you find any issues, post it here and we can look into it further.

Consider indicating the response as helpful and marking it as correct if it meets your needs.