The CreatorCon Call for Content is officially open! Get started here.

List collector variable data comparison

Lisa Goldman
Kilo Sage

Hello,

I have a List Collector variable on the RITM.

Users can select name from the List Collector.  If selected name listed as an approver and state is approved, display a message and remove the name from the List Collector. I appreciate the help.  

 

find_real_file.png

 

1 ACCEPTED SOLUTION

A@Lisa Goldman 

Make sure you have checked  'Applies to a Requested Item'.

You can use below logic in your client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var RITM = g_form.getUniqueValue();
    var myList = newValue;
    var myArray = myList.split(',');
    var result = [];
    for (var i = 0; i < myArray.length; i++) {
        var sysapproval = new GlideRecord('sysapproval_approver');
        sysapproval.addQuery('sysapproval', RITM);
        sysapproval.addQuery('approver', myArray[i].toString());
        sysapproval.addQuery('state', 'approved');
        sysapproval.query();
        if (!sysapproval.next()) {
            result.push(myArray[i].toString());
        }
    }
    if (myArray.toString() != result.toString()) { // to avoid reccusrsive calls
        g_form.setValue('attendeeName', result.toString());
    }
}

 

With this script you will observer some performance issue if add more users from list.

I would suggest to use GlideAjax and get the result from server. It would be more efficient.

Pleas find below Ho you can use GlideAjax:

https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f961...

Pass myList and RITM id as parameters to and use below part of client script in Script include to get result:

var myArray = myList.split(',');
    var result = [];
    for (var i = 0; i < myArray.length; i++) {
        var sysapproval = new GlideRecord('sysapproval_approver');
        sysapproval.addQuery('sysapproval', RITM);
        sysapproval.addQuery('approver', myArray[i].toString());
        sysapproval.addQuery('state', 'approved');
        sysapproval.query();
        if (!sysapproval.next()) {
            result.push(myArray[i].toString());
        }
    }

 

I don't mine sharing complete script, but that would server the purpose only. I would be more happy if you try it by yourself. It would be useful in future as well. Happy Learning 🙂

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

21 REPLIES 21

Good morning Anil,

Thank you so much for your advice! I will keep searching for answer in the community if I need help in the near future.

I have tried to understand what your script is doing, but I only can understand part of what it is doing, because I'm weak at coding skill.  

One last question to this issue. After the program ran, I would like to displays a message with selected name.

I added alert, but it only displays numbers instead of the attendee name. 

find_real_file.png

 

 

This is because in backend ListCollector variable store sys_id of reference table records.

And we have converted that comma separated list into an array.

If you change this code to GlideAjax then you can use more serverSide Api to get name of users using dot Walking. and return result of removed user names.

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

I will try out in Glide Ajax. Thank you for your effort and time!

@Anil Lande 

Hi Anil,

I need to learn how to add an OR condition and If statement to your code.

I have tried this, but it did not work. Please help. Thank you

sysapproval.addOrCondition('state','requested');

sysapproval.addEncodedQuery('state=approved^ORstate=requested');


 

find_real_file.png

 

 

Hi,

You can do it by two ways:

1. 

var sysapproval = new GlideRecord('sysapproval_approver');
        sysapproval.addQuery('sysapproval', RITM);
        sysapproval.addQuery('approver', myArray[i].toString());
        sysapproval.addQuery('state', 'approved').addOrCondition('state','requested');
        sysapproval.query();

 

2.

var sysapproval = new GlideRecord('sysapproval_approver');
        sysapproval.addQuery('sysapproval', RITM);
        sysapproval.addQuery('approver', myArray[i].toString());
        var Q1 =  sysapproval.addQuery('state', 'approved');
        Q1.addOrCondition('state','requested'); 
        sysapproval.query();

 

In both cases result will be same:

https://docs.servicenow.com/bundle/newyork-application-development/page/script/business-rules/concep...

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande