Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

GlideController functionality in Scoped app

Daniel Peel
Mega Sage

Currently I have an error function in a script include... I'm using in global and I want to move this process into a scoped application... I'm using GlideController to take a passed status value 430, 431 etc and use it to lookup a constant.  This works fine... but how can I do this in a scoped app... the GlideScopedEvaluator() doesn't seem to work the same way.

 

Thanks

//this are constants at the top the SI
ERROR430/*old 415*/ = 'The following required fields are missing: ';
ERROR431/*old 416*/ = 'The following field has an incorrect value: ';
ERROR432/*old 417*/ = 'The following record was not found: ';
ERROR433/*old 419*/ = 'The following error occurred: ';


//this is the function that is called and uses those for standard output.
_errorHandle: function(status,messageAddOn,calledFunction){

var message = '';
var getErrorMessage = GlideController;
var errorConstant = 'ERROR' + status;
var errorMessageFound = getErrorMessage.evaluateString(errorConstant);

if (errorMessageFound){
message = errorMessageFound + messageAddOn;
}
else {
message = messageAddOn;
}
var cmxError = new sn_ws_err.ServiceError();
cmxError.setStatus(status);
cmxError.setMessage(message);
cmxError.setDetail("Failure in: " + calledFunction);
return cmxError;
},

 

15 REPLIES 15

Daniel Peel
Mega Sage

@Callum Ridley 

It is part of a SI... I didn't think I needed to share the entire script as it's wasn't relevant, I could have indicated it was in a SI. I updated the OP

Using the code above... I'm returned this
TypeError: Cannot read property \"ERROR430\" from undefined.  

@Pranesh I listed I tried with GSE and that doesn't work the same way as the controller does in Global.  I was using GC to turn the string into a variable... and get the value that way. 

try this

STATIC_PROPERTIES: {
		ERROR430:'The following required fields are missing: ',
		ERROR431:'The following field has an incorrect value: ',
		ERROR432:'The following record was not found: ',
		ERROR433:'The following error occurred: ',
	},
	
	initialize: function() {
	},

	_errorHandle: function(status, messageAddOn, calledFunction){
		var message = '';
		var errorConstant = 'ERROR' + status;
		var errorMessageFound = this.STATIC_PROPERTIES.errorConstant;

		if (errorMessageFound){
			message = errorMessageFound + messageAddOn;
		} else {
			message = messageAddOn;
		}
		var cmxError = new sn_ws_err.ServiceError();
		cmxError.setStatus(status);
		cmxError.setMessage(message);
		cmxError.setDetail("Failure in: " + calledFunction);
		return cmxError;
	},

Pranesh, this not how Javascript works.

Daniel, the code I shared works perfectly.

var myClass = new MyClass();
var errorObj = myClass._errorHandle(430, 'my_field', 'myFunction');
gs.info(errorObj)

output:
*** Script: [object ServiceError]

Daniel Peel
Mega Sage

@Callum Ridley 

Taking your path... I did this and it worked... script below edited to only show changes

	
initialize: function() {
//put this back in the initialize and changed the : to = ... 
STATIC_PROPERTIES = {
	ERROR430:'The following required fields are missing: ',
	ERROR431:'The following field has an incorrect value: ',
	ERROR432:'The following record was not found: ',
	ERROR433:'The following error occurred: ',
}

},

_errorHandle: function(status, messageAddOn, calledFunction){
	var message = '';
	var errorConstant = 'ERROR' + status;
//removed this.
var errorMessageFound = STATIC_PROPERTIES[errorConstant];

You must make sure that you are defining the STATIC_PROPERTIES on the prototype of your class like in my example, perhaps you put them in the wrong place in your code. I promise you, it 100% works.