indexOf in business rule

Joshuu
Kilo Sage

Hi All,

 

I have two fields on the change form as below

Business Service

Maintenance Window (this is a list type field and taking values from scheduled entries table)

 

Whenever user selects any business service which is having below keywords, it should check the maintenance field values and skip the related choices.

'B2B', 'B2C', 'RES ESB', 'IRIS', 'Loyalty', 'Availability ESB', 'Concerto', 'Network'

 

From the below example, I have selected 'Loyalty Admin UI' as business service.

 

So, from the maintenance window field it should hide/remove below choices and populate remaining choices.

Loyalty Maintenance Window - PM,

Loyalty Maintenance Window - AM

 

And if user selects any other choice as business service apart from the above-mentioned keywords it should populate all the values in the maintenance window field.

 

Could you please check my below script and correct me if I am wrong.

 

Script:

 
 

 

(function executeRule(current, previous /*null when async*/ ) {
var arr = [];
var uniqueSchedules = {};

    var x = ['Availability ESB Maintenance Window', 'B2B Maintenance Window', 'B2C Web Maintenance Window', 'Concerto Maintenance Window', 'Network/Infrastructure Maintenance Window', 'Network Maintenance Window', 'Network Firewall Maintenance', 'Loyalty Maintenance Window - PM', 'Loyalty Maintenance Window - AM', 'IRIS Weekly Maintenance Window', 'RES ESB Maintenance Window'];

    var y = current.u_business_service.name.split(',');

    var z = ['B2B', 'B2C', 'RES ESB', 'IRIS', 'Loyalty', 'Availability ESB', 'Concerto', 'Network'];

    for (var j = 0; j < z.length; j++) {
        if (z[j].indexOf(y.toString()) == -1) {
            arr.push(z[j]);
            gs.log("Print Y: " + y.toString());
            if (!uniqueSchedules.hasOwnProperty(x)) {
                uniqueSchedules[x] = true; // Mark schedule name as seen
                for (var i = 0; i < x.length; i++) {
                    if (x[i].indexOf(z.toString()) == -1) {
                        arr.push(x[i]);
                    }
                }
                current.u_maintainence_window = arr.join(',');
            }
        }
    }
})(current, previous);

 

 

Output:

 

priyarao_0-1727771131325.png

 

priyarao_1-1727771202359.png

 

Please assist.

 

Best Regards.

8 REPLIES 8

Eshwar Reddy
Kilo Sage

Hi @Joshuu 

Try Below code 


var maintenanceWindows = [
'Availability ESB Maintenance Window',
'B2B Maintenance Window',
'B2C Web Maintenance Window',
'Concerto Maintenance Window',
'Network/Infrastructure Maintenance Window',
'Network Maintenance Window',
'Network Firewall Maintenance',
'Loyalty Maintenance Window - PM',
'Loyalty Maintenance Window - AM',
'IRIS Weekly Maintenance Window',
'RES ESB Maintenance Window'
];


var excludedKeywords = [
'B2B', 'B2C', 'RES ESB', 'IRIS',
'Loyalty', 'Availability ESB', 'Concerto', 'Network'
];


var businessServiceName = current.u_business_service.name;


var shouldFilter = excludedKeywords.some(function(keyword) {
return businessServiceName.includes(keyword);
});


var filteredWindows = [];

if (shouldFilter) {

for (var i = 0; i < maintenanceWindows.length; i++) {
var window = maintenanceWindows[i];


var exclude = excludedKeywords.some(function(keyword) {
return window.includes(keyword);
});


if (!exclude) {
filteredWindows.push(window);
}
}


current.u_maintenance_window = filteredWindows.join(',');
} else {

current.u_maintenance_window = maintenanceWindows.join(',');
}

 

Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution

Thanks
Eshwar

Hi @Eshwar Reddy ,

 

It is not working. Nothing is populating in maintenance window field.

Sai_Charan_K
Kilo Sage

Hi @Joshuu ,

Please find the code below :

(function executeRule(current, previous /*null when async*/ ) {
    var arr = [];
    var uniqueSchedules = {};

    // Maintenance windows
    var maintenanceWindows = ['Availability ESB Maintenance Window', 'B2B Maintenance Window', 'B2C Web Maintenance Window', 'Concerto Maintenance Window', 'Network/Infrastructure Maintenance Window', 'Network Maintenance Window', 'Network Firewall Maintenance', 'Loyalty Maintenance Window - PM', 'Loyalty Maintenance Window - AM', 'IRIS Weekly Maintenance Window', 'RES ESB Maintenance Window'];

    // Keywords to match against the selected Business Service
    var keywordList = ['B2B', 'B2C', 'RES ESB', 'IRIS', 'Loyalty', 'Availability ESB', 'Concerto', 'Network'];

    // Split the current business service name into an array of values
    var businessServices = current.u_business_service.name.split(',');

    // Loop through each business service
    for (var i = 0; i < businessServices.length; i++) {
        var service = businessServices[i].trim();

        // Check if the current business service contains any keyword
        for (var j = 0; j < keywordList.length; j++) {
            if (service.includes(keywordList[j])) {
                gs.log("Business Service matches keyword: " + keywordList[j]);

                // If there's a match, filter out the related maintenance windows
                for (var k = 0; k < maintenanceWindows.length; k++) {
                    if (maintenanceWindows[k].includes(keywordList[j])) {
                        // Skip the related maintenance windows (do not add them to the array)
                        gs.log("Skipping Maintenance Window: " + maintenanceWindows[k]);
                        continue;
                    }

                    // Add only the remaining maintenance windows to the arr[] (that are not skipped)
                    if (!uniqueSchedules.hasOwnProperty(maintenanceWindows[k])) {
                        arr.push(maintenanceWindows[k]);
                        uniqueSchedules[maintenanceWindows[k]] = true; // Mark schedule as added
                    }
                }
            }
        }
    }

    // Join the array into a comma-separated string and assign it to the field
    current.u_maintainence_window = arr.join(',');
    gs.log("Final Maintenance Windows: " + arr.join(','));
    
})(current, previous);

 

Please mark this solution as "Helpful" and "accepted solution" if this solution helped you in any way.

Thanks and Regards, 
K. Sai Charan
Sr. ServiceNow Developer
Deloitte India

Hi @Sai_Charan_K ,

 

Thank you for your response.

 

It is partially working.

 

For the below scenario it is working as expected. and for all the keywords list which is mentioned it is working well.

 

priyarao_0-1727774637385.png

If I select any other choice as business service apart from the above-mentioned keywords it should populate all the values in the maintenance window field.

 

If you see below screen shot, it is empty. it should not be blank but should show all as the values. 

Do we need write else condition?

 

Please assist.

 

priyarao_1-1727774928567.png