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

Hi,

ok when user updates the list collector variable and if one of the user is the approver who already approved the RITM then you need to stop the update operation?

Is the understanding correct?

If yes then you can use before update BR on RITM table

1) get the variable values

2) check any of the user in the list collector is in the approver of the RITM which is already approved

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur,

I'm a beginner at scripting.  I have spent many hours trying to get this to work, but I didn't get anywhere. Could you please help?

If it is possible, we would like to trigger when the list collector onChange? Here's what I have so far and I know it is incorrect. I hope you can provide suggestions.  Thank you

find_real_file.png

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

You guys are awesome! Without your assistance, it would’ve taken me weeks to figure out.  

Can you recommend some beginner scripting courses? Also, how can I show a message with selected name displays.

Thank you so much for sharing your knowledge.  I really appreciate your help

Lisa

Hi Lisa,

Glad to resolve your issue.

You can check below course:

https://developer.servicenow.com/dev.do#!/learn/courses/quebec/app_store_learnv2_scripting_quebec_sc...

Also I would suggest you to keep eye on community threads which you find familiar. You can find problem statement as well as solutions provided. You can try these scenarios on your personal instance and improve your scripting skill.

 

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