how to populate the condition field in the filter using scripting?

mouli7
Tera Contributor

Hi all,

my third party is providing me the list of application services and its blackout values in the below format

var reqData = {
"ID": "8611",
"Details": "INPAT:SBBW_Monday_14:16_R0",
"Recurrence": "Monthly",
"StartTime": "06/05/2024 14:00:06",
"EndTime": "06/05/2024 16:00:16",
"MType": "Day of the month",
"Application": [{
"AppName": "DI_metal_app_service"
},
{
"AppName": "DI_metal_app_service1"
},
{
"AppName": "DI_metal_app_service2"
}
]
}

how to populate the application services name in the below format in the blackout schedule

mouli7_0-1715150765411.png

 

I have tried the below code but it does not help me

 

var conditiona = "";
var gr = new GlideRecord('cmn_schedule_blackout');
gr.addQuery('sys_id', 'e518327987b9829c49aa0d030cbb350c');
gr.query();
if (gr.next()) {

gs.info('dsssdggg:,.........................' + gr.source);
var arrayUtil = new global.ArrayUtil();
for (var i = 0; i < reqData.Application.length; i++) {
var appName = reqData.Application[i].AppName;
gs.info('appName..........' + appName);
if (conditiona) {
conditiona += "OR";
}
conditiona += '("ref_cmdb_ci_service_auto.name="' + appName + ' ")' ;

}

}
gr.condition = "(" +conditiona + ")";
gr.update();


please provide me a solution if possible.

1 ACCEPTED SOLUTION

By using the "IN" operator you're able to execute a much more efficient query. OR conditions in SQL are much slower and impact instance performance 

View solution in original post

5 REPLIES 5

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @mouli7 ,

 

You can set the value of this condition field via script by passing the encoded quety in the script.

 

Create a same filter in the list view copy the encoded query and pass and set value in script.

 

I hope this helps


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

mouli7
Tera Contributor

i am not sure how to do that via scripting
can you please try and share me the code

Kieran Anson
Kilo Patron

I'd recommend looking up the CI's (assuming those are the full names) to improve efficiency when the Maintenance Window & Conflict Checker business rules run.

 

(function(){
var reqData = {
    "ID": "8611",
    "Details": "INPAT:SBBW_Monday_14:16_R0",
    "Recurrence": "Monthly",
    "StartTime": "06/05/2024 14:00:06",
    "EndTime": "06/05/2024 16:00:16",
    "MType": "Day of the month",
    "Application": [{
            "AppName": "DI_metal_app_service"
        },
        {
            "AppName": "DI_metal_app_service1"
        },
        {
            "AppName": "DI_metal_app_service2"
        }
    ]
};

if (!reqData || !reqData.hasOwnProperty('Application') || !Array.isArray(reqData.Application))
    return;

var applicationCMDBSysIds = [];

reqData.Application.forEach(function(application){
	var cmdbCI = new GlideRecord('cmdb_ci_service_auto');
	cmdbCI.setLimit(1);
	cmdbCI.addQuery('name' , application.AppName);
	cmdbCI.query();
	if(cmdbCI.next()){
		applicationCMDBSysIds.push(cmdbCI.getUniqueValue())
	} else {
		gs.warn(gs.getMessage("Unable to find Application with name {0} for Maintenance Schedule",[application.AppName] ));
	}
});

if(applicationCMDBSysIds.length == 0)
return;

var scheduleGR = new GlideRecord('cmn_schedule_blackout');
if(scheduleGR.get('e518327987b9829c49aa0d030cbb350c')){
	scheduleGR.setValue('condition' , 'sys_idIN' + applicationCMDBSysIds.join(','));
	scheduleGR.update();
}

})();

Hi @Kieran Anson 

Thank you very much!
it is working 
But can you please send me how to do the same in OR condtion format to map the sysyids
mouli7_0-1715156424268.png


this can also help me.