BR - Script does not execute "DOES NOT CONTAIN"

Manikandan Subr
Kilo Guru

Hi, i have an issue in the below condition. 

In Incident table there is a list field = u_customers and in the Change request table there is = u_customers_impacted list column.

I want to compare if the list of customers in Incident table is available in Change request customer list and what are found i want to remove them from the incident list. From the below script, what ever is found is getting updated but when i use for updating only not found values, it is updating multiple times the same found value.

if (cust_arr_inc[i].indexOf(cust_arr_cr[j])>= 0)  - this condition is updating the found values

but i want to do something like if (cust_arr_inc[i].indexOf(cust_arr_cr[j])<= 0) to update only not found values and it is repeating the customer names multiple times. Below is the complete script - please suggest. 

var cust_found_arr = [];
var match = false;
var i = 0;
var j = 0;
var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0170752');
gr.query();
while (gr.next()) {
    var changeReq = new GlideRecord('change_request');
    changeReq.addQuery('cmdb_ci', gr.cmdb_ci);
    changeReq.query();
    while (changeReq.next()) {
        gs.print('the CR numbers matched is ' + changeReq.number);
        var cust_arr_inc = [];
        cust_arr_inc = gr.u_customers.toString().split(',');
        gs.print('the Inc customers are ' + cust_arr_inc + gr.number);

        var cust_arr_cr = [];
        cust_arr_cr = changeReq.u_customers_impacted.toString().split(',');
        gs.print('the CR customers are ' + cust_arr_cr + gr.number);

        for (i = 0; i < cust_arr_inc.length; i++) {
            for (j = 0; j < cust_arr_cr.length; j++) {
                if (cust_arr_inc[i].indexOf(cust_arr_cr[j]) >= 0) {
                    cust_found_arr.push(cust_arr_inc[i]);
                    gs.print('The matched Companies are :' + cust_found_arr + ' - ' + gr.number);

                }

            }

        }
    }
}

if (cust_found_arr[0].length > 0) {
    gr.setValue('u_customers', '');
    gr.setValue('u_customers', cust_found_arr.toString());
    gs.print('The Final Customer names are ' + gr.u_customers);
}

 

1 ACCEPTED SOLUTION

// Try this code

var cust_arr_inc = [];
var cust_arr_cr = [];

var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0170752');
gr.query();
while (gr.next()) {

gs.print('the Inc customers are ' + cust_arr_inc.toString() + ' for ' + gr.number);
cust_arr_inc = gr.u_customers.toString().split(',');

var changeReq = new GlideRecord('change_request');
changeReq.addQuery('cmdb_ci', gr.cmdb_ci);
changeReq.query();
while (changeReq.next()) {
gs.print('the CR numbers matched is ' + changeReq.number);

var current_cr_cust = changeReq.u_customers_impacted.toString().split(',');
gs.print('the CR customers are ' + current_cr_cust.toString() + " for " + changeReq.number);
cust_arr_cr.concat(current_cr_cust); // collect into the array
}
}
var arrayUtil = new ArrayUtil();
gs.print("Not found Inc companies = " + arrayUtil.diff(cust_arr_inc, cust_arr_cr).toString());

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

18 REPLIES 18

Actually, if you try to execute the code that you sent in scripts - background, it gives the result as missing ID from both incident and CR table.

The script collects the company sys ids on the incident that are not in CR companies. Isn't that what you want?

 

Refer to your answer for.

"

Let's say your incident has companies 1,2,3,4

and your CR has companies 3,4,5,6

 

What do you want your final companies on incident?

1,2?

 

"

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Thank you for your support. I have now been able to complete my code and it is working. I am running this code in script include and calling in a Business rule and it is not executing. i am new to script include and i may be doing some mistake. Could you also take a look at the question in the below thread?

https://community.servicenow.com/community?id=community_question&sys_id=f34aa798db39b780d58ea345ca961996

I am away from my computer as it is late night. Will look into it tomorrow or someone else will help.
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022