Skip to content

Instantly share code, notes, and snippets.

@mpoore
Created May 26, 2020 16:18
Show Gist options
  • Save mpoore/8e9bf5dc69ab388b8921fa84a2b3cfc4 to your computer and use it in GitHub Desktop.
Save mpoore/8e9bf5dc69ab388b8921fa84a2b3cfc4 to your computer and use it in GitHub Desktop.
Generate New Hostname - vRO Workflow
// Set variables
var result = attNameFormat;
var hostnameIdx = attIndexFormat;
var nextNum = 0;
var digitCount = attNameFormat.match(/(#)/g).length;
// Create request object
var requestObj = JSON.parse(request.toLowerCase());
// Create translations object
var translationsObj = JSON.parse(attTranslations.getContentAsMimeAttachment().content.toLowerCase());
// Get request keys and run values through translations
var requestArr = Object.keys(requestObj);
for each (var key in requestArr) {
var requestVal = requestObj[key].toLowerCase();
System.debug("Processing request key '" + key + "' with value: " + requestVal);
if (requestVal in translationsObj.translations) {
requestVal = translationsObj.translations[requestVal];
}
result = result.replace("{{" + key + "}}",requestVal);
hostnameIdx = hostnameIdx.replace("{{" + key + "}}",requestVal);
}
// Remove leading / trailing special chars from hostname index
hostnameIdx = hostnameIdx.replace(/([-_]$)/g,"");
hostnameIdx = hostnameIdx.replace(/^([-_])/g,"");
System.debug("Hostname index: " + hostnameIdx);
// Lookup hostname index
try {
var attribute = attHostnames.getAttributeWithKey(hostnameIdx);
nextNum = attribute.value;
}
catch (e) {
System.warn("Hostname index '" + hostnameIdx + "' does not exist, it will be created");
}
finally {
nextNum++;
attHostnames.setAttributeWithKey(hostnameIdx,nextNum);
}
// Convert humber to string and add leading zeroes
var hostnameNum = nextNum.toString();
var leadingZeroes = new Array(digitCount - hostnameNum.length + 1).join("0");
hostnameNum = leadingZeroes + hostnameNum;
result = result.replace(/(#+)/g,hostnameNum);
// Finally, return the new hostname
if (attOutputCase == "upper") {
hostname = result.toUpperCase();
}
else {
hostname = result.toLowerCase();
}
System.log("Returning hostname: " + hostname);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment