The CreatorCon Call for Content is officially open! Get started here.

ADD Query Condition in Script

Joshuu
Kilo Sage

Hi All,

 

Below is the Change Risk Assessment script. It is not working as expected. It is always going to the else part. Requirement is, If there were any failed changes logged for the Primary CI in the past 6months then default the metric should be '300' otherwise '150'.

 

Please check if my conditions are correct or not.

 

var ci = new GlideRecord('change_request');
ci.addQuery('cmdb_ci', primary);
ci.addQuery('close_code', 'unsuccessful');
ci.addEncodedQuery('sys_updated_onONLast6months@javascript:gs.beginningOfLast6Months()@javascript:gs.endOfLast6Months()');
ci.query();
actual_result = ci.getRowCount();
if (actual_result>=1) {
    scaled_result = 300;
} else
scaled_result = 150;

 

 

Thanks & Regards

4 REPLIES 4

Amit Verma
Kilo Patron
Kilo Patron

Hi @Joshuu 

 

I can see you have made a query with cmdb_ci as Primary. Can you please confirm if primary is a variable ? Also, use gs.info(actual_result); before if condition to check the number of rows being returned from your query to debug it further.

 

You can even apply manual filters as well on the change_request table to see if you are able to get the relevant records or not to set the metric.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Hi @Amit Verma ,

 

Yes, As per the sample script which is mentioned in the risk assessment script, I have built my script. But it is always going to else part only. I suspect that something is wrong in first query where we are comparing the current cmdb_ci with the ci of the existing change records. Please suggest if that is correct or not.

 

// The following variables are available: 
// - primary: contains the sys_id of the assessable object to be evaluated 
// - string_result: the script sets the display string value for this metric to this variable 
// - actual_result: the script sets the actual value for this metric to this variable 
// - scaled_result: the script sets the scaled value (used in calculations) for this metric to this variable 
// 
// For example:
// var gr = new GlideRecord('cmdb_ci');
// gr.addQuery('manufacturer', primary);
// gr.query();
// actual_result = gr.getRowCount();
// string_result = actual_result + '';
// if (actual_result > 100) scaled_result = 5;
// else scaled_result = 1;

var ci = new GlideRecord('change_request');
ci.addQuery('cmdb_ci', primary);
ci.addEncodedQuery('close_code=unsuccessful^sys_updated_onONLast6months@javascript:gs.beginningOfLast6Months()@javascript:gs.endOfLast6Months()'); // Filter for changes in the past 6 months
ci.query();
    actual_result = ci.getRowCount();
	string_result = actual_result + '';
    gs.info('review Found! actual_result=' + actual_result);
    if (actual_result >= 1) {
        scaled_result = 300;
    } else {
        scaled_result = 150;
    }

 

hi @Joshuu ,

What is primary in your query variable or some hardcoded value , if it is a variable then you might need to try variablename.getDisplayValue and see if it works 

Amit Verma
Kilo Patron
Kilo Patron

Also, where you have defined the scaled_result variable ?

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.