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

vkachineni
Kilo Sage
Kilo Sage

//Try

 

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

 

 

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

vkachineni
Kilo Sage
Kilo Sage

You can use ArrayUtil

 

var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c"); //your full companies list on incident
var a2 = new Array("c"); //your companies found on CR 
gs.print(arrayUtil.diff(a1, a2));


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

How i can use arrayUtil for the below condition?

  if (cust_arr_inc[i].indexOf(cust_arr_cr[j]) >= 0)

 

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