Created
June 3, 2014 16:54
-
-
Save amitrogye/a039b4d2db56f046084b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SubststitutionHelper = Class.create(); | |
SubststitutionHelper.prototype = { | |
initialize: function(record) { | |
this.log = new GSLog("intuit.custom.debug.level", this.type); | |
this.record = record; | |
// preload variables for request item | |
this.variables = {}; | |
if(this.record.getTableName() == 'sc_req_item'){ | |
this.preloadVariables(); | |
} | |
}, | |
substitute:function(strBody){ | |
// evalauate and return javascript script result | |
if(strBody.substring(0, 11) == this.JAVASCRIPT_PREFIX){ | |
var script = strBody.substring(11); | |
this.log.logDebug("inside javascript evaluation -- " + script); | |
var scriptResult = ""; | |
try{ | |
scriptResult = GlideController.evaluateString(script); | |
this.log.logDebug("scriptResult - " + scriptResult); | |
}catch(e){ | |
this.log.logDebug("error executing the script - " + e.toString()); | |
} | |
return scriptResult; | |
} | |
// Tokens could be field name or to display display value of field then | |
// prefix it with dv_ : dv_fieldName | |
// fieldnames are enclosed inside currly braces {dv_fieldName} | |
var tokens = strBody.match(/[^{}]+(?=\})/g); | |
if(!tokens){ | |
return strBody; | |
} | |
var objToken = {}; | |
// convert array to object | |
for (var i = tokens.length - 1; i >= 0; i--) { | |
objToken[tokens[i].toString()] = ""; | |
}; | |
// iterate over the token object and get values from this.record | |
for(var key in objToken){ | |
var keyValue = ""; | |
// check if to display Variable value or field value or display value | |
if(key.substring(0, 10) == this.VARIABLE_PREFIX){ | |
var variableName = key.substring(10); | |
keyValue = this.variables[variableName]; | |
} | |
else if (key.substring(0, 3) == this.DISPLAY_VALUE_PREFIX) { | |
var displayKey = key.substring(3); | |
keyValue = this.record.getDisplayValue(displayKey); | |
}else{ | |
keyValue = this.record.getValue(key); | |
} | |
objToken[key] = keyValue; | |
// find and replace the tokens | |
var re = new RegExp('\{' + key + '\\}', 'g'); | |
strBody = strBody.replace(re, keyValue); | |
} | |
return strBody; | |
}, | |
preloadVariables:function(){ | |
// Preload variables in this.variables global object | |
var set = new GlideappVariablePoolQuestionSet(); | |
set.setRequestID(this.record.getUniqueValue()); | |
set.load(); | |
var al = set.getFlatQuestions(); | |
for (var i=0; i < al.size(); i++) { | |
var question = al.get(i); | |
var variableName = question.getName().toString(); | |
var variableValue = question.getValue().toString(); | |
var variableDisplayValue = question.getDisplayValue().toString(); | |
this.variables[variableName] = variableValue; | |
this.variables[this.DISPLAY_VALUE_PREFIX + variableName] = variableDisplayValue; | |
} | |
}, | |
type: 'SubststitutionHelper', | |
VARIABLE_PREFIX : 'variables.', | |
JAVASCRIPT_PREFIX : 'javascript:', | |
DISPLAY_VALUE_PREFIX : 'dv_' | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment