Reverse a business rule when field value changes

Still Learning
Kilo Sage

Good morning experts, 

I have a requirement to 1.) create a risk value field on cmdb_ci records, and then to 2.) map those CI Risk levels to the risk field on theincident form, while copying the CI Risk to a custom field (u_ci_risk) - also on the incident form. 

To accommodate this requirement, I have created a custom field for hardware (u_asset_category) and software (u_risk_level) CIs and written the below Before insert/update business rule that is triggered when the Configuration Item field changes.

My problem is that when the Configuration Item field changes, the CI Risk (u_ci_risk) and risk ('risk') fields are initially  updated.  But if that field value is changed to a new value, the CI Risk and Risk fields do not update or refresh to account for that change.  Can someone please tell me what I've done wrong?  Here is my code: 

 

(function executeRule(current, previous /*null when async*/) {

	var ciRisk = current.cmdb_ci;	
	if(current.ciRisk.changes()){
	ciRisk.get(current.ciRisk);
	ciRisk.addEncodedQuery('ref_cmdb_ci_hardware.u_asset_category!=^ORref_cmdb_ci_appl.u_risk_levelINHigh,Medium,Low');
	ciRisk.query();
	while(ciRisk.next()){
		if(current.ciRisk == 'A') {			
				ciRisk.setValue('u_ci_risk', '1'); 
				ciRisk.setValue('risk', '1');
				
			} else if (current.ciRisk == 'B'){
				ciRisk.setValue('u_ci_risk', '2');
				ciRisk.setValue('risk', '2');
				
			} else if (current.ciRisk == 'C'){
				ciRisk.setValue('u_ci_risk', '3');
				ciRisk.setValue('risk', '3');
				
			} else if(current.ciRisk == 'D'){
				ciRisk.setValue('u_ci_risk', '4');
				ciRisk.setValue('risk', '4');
				
			} else if(current.ciRisk == "High"){
			ciRisk.setValue('u_ci_risk', '2');
			ciRisk.setValue('risk', '2');
			
		} else if(current.ciRisk == "Medium"){
			ciRisk.setValue('u_ci_risk', '3');
			ciRisk.setValue('risk', '3');
			
		} else if(current.ciRisk == "Low"){
			ciRisk.setValue('u_ci_risk', '4');
			ciRisk.setValue('risk', '4');
			}
		ciRisk.update();
	} if (ciRisk == '') {
			ciRisk.setValue('u_ci_risk', '5');
			ciRisk.setValue('risk', '3');
		ciRisk.update();
		}
}})(current, previous);

 

1 REPLY 1

Bert_c1
Kilo Patron

Hi,

 

It seems 'ciRisk' as above in the "addEncodedQuery" is not valid.  typically, when querying a table in a script, "myTable = new GlideRecord('table_name'); is used.

 

You can add gs.info(); lines in the code, and then check Script Log Statements for the results. for example add:

 

gs.info("My BR: query resulted in " + ciRisk.getRowCount() + " records to process.");

 

just after the ciRisk.query(). But, what is meant by the following:

 

	ciRisk.get(current.ciRisk);
	ciRisk.addEncodedQuery('ref_cmdb_ci_hardware.u_asset_category!=^ORref_cmdb_ci_appl.u_risk_levelINHigh,Medium,Low');
	ciRisk.query();

 

Are you trying to query some cmdb_ci table? Seems so. so you can update a field on the incident table. But you set 'ciRisk' to a value (sys_id of the cmdb_ci record in the incident table cmdb_ci field). then you try to use that in a GlideRecord query.

 

Start here:

 

GlideRecord

 

on the use of GlideRecord.