Convert a BR to a Script include

EricG2
Tera Expert

I'm not sure how to the this completed.

 

Currently i have a BR to calculate Risk and Impact on a Change Request.

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

	// Add your code here
	var nRisk = '';
	var nImpact = '';
	var currType = current.type;

	var expC = parseInt(current.getValue('u_risk_question_5'));
	var bckP = parseInt(current.getValue('u_risk_question_4'));
	var busI = parseInt(current.getValue('u_risk_question_3'));
	var comP = parseInt(current.getValue('u_risk_question_2'));
	var outS = parseInt(current.getValue('u_risk_question_1'));
	var rTTL = expC+bckP+busI+comP+outS;
	var impTTL = outS+busI;

	if (rTTL > 0 && rTTL < 11) {
        current.setValue('risk', 4);
		nRisk = 4;
    } else if (rTTL > 10 && rTTL < 21) {
        current.setValue('risk', 3);
		nRisk = 3;
    } else if (rTTL > 20 && rTTL < 41) {
        current.setValue('risk', 2);
		nRisk = 2;
    }
    else if (rTTL > 40) {
        current.setValue('risk', 1);
		nRisk = 1;
    }
	//gs.info("the impact is "+impTTL);
	if(impTTL<=1){
		current.setValue('impact',4);
		nImpact = 4;
	}
	else if(impTTL>1 && impTTL<=6){
		current.setValue('impact',3);
		nImpact = 3;
	}
	else if(impTTL<6 && impTTL>=10){
		current.setValue('impact',2);
		nImpact = 2;
	}
	else if(impTTL>10){
		current.setValue('impact',1);
		nImpact = 1;
	}

	if((nImpact<3 || nRisk<4) && currType=="standard"){
		var msg = "The change type has been switched to 'Normal' due to the risk and impact rating. Standard changes are meant for low risk/low impact recurring changes. You will need to resubmit the Standard Change with a low risk assessment.";
		gs.addInfoMessage("Attention: "+msg);
		current.setValue('type','normal');
	}
})(current, previous);

 

I'm getting an error
Could not save record because of a compile error: SyntaxError: Expected ',', got 'answer' at line 7, column 1

 

Script Include

var setChangeRisk = Class.create();
setChangeRisk.prototype = Object.extendsObject(AbstractAjaxProcessor, {

setChangeRisk : function() {
		// Add your code here
	var nRisk = '';
	var nImpact = '';
	var currType = current.type;

	var expC = parseInt(current.getValue('u_risk_question_5'));
	var bckP = parseInt(current.getValue('u_risk_question_4'));
	var busI = parseInt(current.getValue('u_risk_question_3'));
	var comP = parseInt(current.getValue('u_risk_question_2'));
	var outS = parseInt(current.getValue('u_risk_question_1'));
	var rTTL = expC+bckP+busI+comP+outS;
	var impTTL = outS+busI;

	if (rTTL > 0 && rTTL < 11) {
        current.setValue('risk', 4);
		nRisk = 4;
    } else if (rTTL > 10 && rTTL < 21) {
        current.setValue('risk', 3);
		nRisk = 3;
    } else if (rTTL > 20 && rTTL < 41) {
        current.setValue('risk', 2);
		nRisk = 2;
    }
    else if (rTTL > 40) {
        current.setValue('risk', 1);
		nRisk = 1;
    }
	//gs.info("the impact is "+impTTL);
	if(impTTL<=1){
		current.setValue('impact',4);
		nImpact = 4;
	}
	else if(impTTL>1 && impTTL<=6){
		current.setValue('impact',3);
		nImpact = 3;
	}
	else if(impTTL<6 && impTTL>=10){
		current.setValue('impact',2);
		nImpact = 2;
	}
	else if(impTTL>10){
		current.setValue('impact',1);
		nImpact = 1;
	}
	///current.update();
	if((nImpact<3 || nRisk<4) && currType=="standard"){
		var msg = "The change type has been switched to 'Normal' due to the risk and impact rating. Standard changes are meant for low risk/low impact recurring changes. You will need to resubmit the Standard Change with a low risk assessment.";
		gs.addInfoMessage("Attention: "+msg);
		current.setValue('type','normal');

		
	}
}
	answer = ;
    type: 'setChangeRisk'
};

 

2 REPLIES 2

Bert_c1
Kilo Patron

Seems there are several problems with the above. Extending 'AbstractAjaxProcessor', not declaring 'answer'.  Seems you may want to return an object containing nRisk and nImpact. 'current' won't be available unless passed in to the 'setChangeRisk' function.

 

I suggest that you look at existing script includes in your instance.

 

https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/script/server-scripting/concept/c...

 

If you post your goal, how you intend to use that script include, I believe you will get additional assistance.

Bert_c1
Kilo Patron

@EricG2 ,

 

I tested using your BR logic in a script include, and created a new BR to call that on the change_request table. See below

Screenshot 2025-02-04 103052.png

The script:

 

var setChangeRisk = Class.create();
setChangeRisk.prototype = {
    initialize: function() {
    },

	setChgRisk : function(current) {
		// Add your code here
		var nRisk = '';
		var nImpact = '';
		var currType = current.type;
		gs.log('setChgRisk: currType = ' + currType + ', number = ' + current.number);

//		var expC = parseInt(current.getValue('u_risk_question_5'));
//		var bckP = parseInt(current.getValue('u_risk_question_4'));
//		var busI = parseInt(current.getValue('u_risk_question_3'));
//		var comP = parseInt(current.getValue('u_risk_question_2'));
//		var outS = parseInt(current.getValue('u_risk_question_1'));
		// for testing
		var expC = 5;
		var bckP = 4;
		var busI = 3;
		var comP = 2;
		var outS = 1;
		var rTTL = expC+bckP+busI+comP+outS;
		var impTTL = outS+busI;

		gs.info("setChgRisk: the rTTL is "+rTTL);
		if (rTTL > 0 && rTTL < 11) {
			current.setValue('risk', 4);
			nRisk = 4;
		} else if (rTTL > 10 && rTTL < 21) {
			current.setValue('risk', 3);
			nRisk = 3;
		} else if (rTTL > 20 && rTTL < 41) {
			current.setValue('risk', 2);
			nRisk = 2;
		}
		else if (rTTL > 40) {
			current.setValue('risk', 1);
			nRisk = 1;
		}

		gs.info("setChgRisk: the impact is "+impTTL);
		if(impTTL<=1){
			current.setValue('impact',4);
			nImpact = 4;
		}
		else if(impTTL>1 && impTTL<=6){
			current.setValue('impact',3);
			nImpact = 3;
		}
		else if(impTTL<6 && impTTL>=10){
			current.setValue('impact',2);
			nImpact = 2;
		}
		else if(impTTL>10){
			current.setValue('impact',1);
			nImpact = 1;
		}
		///current.update();
		if((nImpact<3 || nRisk<4) && currType=="standard"){
			var msg = "The change type has been switched to 'Normal' due to the risk and impact rating. Standard changes are meant for low risk/low impact recurring changes. You will need to resubmit the Standard Change with a low risk assessment.";
			gs.addInfoMessage("Attention: "+msg);
			current.setValue('type','normal');	
		}
	},

    type: 'setChangeRisk'
};

 

The new BR (runs on Insert, Update, and When is 'Before' code:

 

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

	// Add your code here
	var newRisk = new setChangeRisk();
	newRisk.setChgRisk(current);

})(current, previous);

 

Result when updating a CHG

Screenshot 2025-02-04 103357.png

If you plan to call the script include from a client script or some other client-side means, then changed are needed to use GlideAjax.

 

Good luck, post back here and myself or others here can assist you.