Skip to content

Instantly share code, notes, and snippets.

@imtrinity94
Created March 31, 2023 12:42
Show Gist options
  • Save imtrinity94/bdf2e8a6cb8a505293d0ced748b686f3 to your computer and use it in GitHub Desktop.
Save imtrinity94/bdf2e8a6cb8a505293d0ced748b686f3 to your computer and use it in GitHub Desktop.
vRO script to update scripts of all actions inside a vRO action module
/**
* vRO script to update scripts of all actions inside a vRO action module
*
* @version 1.0.0
*
* @param {Module} actionModule
*
* @outputType string
*
*/
function bulkUpdateActionScripts(actionModule) {
var actionList = actionModule.actions;
// I wanted to add this line of code to all my actions in vRO
var actionLineToAdd = 'if(arguments.callee.name.substr(6)) System.setLogMarker("ACTION: "+arguments.callee.name.substr(6));\n';
//Get Transient Rest Host for VRO.
var restHost = RESTHostManager.createHost("dynamicRequest");
var transientHost = RESTHostManager.createTransientHostFrom(restHost);
transientHost.operationTimeout = 60;
transientHost.connectionTimeout = 30;
transientHost.hostVerification = false;
transientHost.url = "https://vro_fqdn.local";
for(var j=0;j<actionList.length;j++)
{
var actionId = actionList[j].id
System.log("Updating action: "+actionList[j].name);
//Create Request to fetch Action script
var header = '[{"key":"Accept","value":"application/json"},{"key":"Content-Type","value":"application/json"}]';
var queryString = "actions/"+actionId;
var httpMethod = "GET";
var content = "";
var headers = JSON.parse(header);
var request = transientHost.createRequest(httpMethod, queryString, content);
if (headers) {
for (var i = 0; i < headers.length; i++) {
request.setHeader(headers[i].key, headers[i].value);
}
}
//Execute Fetch Request
var response = request.execute();
var statusCode = response.statusCode;
System.debug(statusCode);
if (statusCode == '200') {
var responseBody = JSON.parse(response.contentAsString);
//System.log(responseBody.script);
if(responseBody.script.indexOf(actionLineToAdd) == -1){
var updatedScript = actionLineToAdd + responseBody.script;
}
else{
var updatedScript = responseBody.script;
}
}
responseBody.script = updatedScript;
//System.log(responseBody.script)
var body = JSON.stringify(responseBody);
//System.log(body);
//Create Request to update the Action's Script
header = '[{"key":"Accept","value":"application/json"},{"key":"Content-Type","value":"application/json"}]';
queryString = "actions/"+actionId;
httpMethod = "PUT";
content = body;
headers = JSON.parse(header);
request = transientHost.createRequest(httpMethod, queryString, content);
if (headers) {
for (var i = 0; i < headers.length; i++) {
request.setHeader(headers[i].key, headers[i].value);
}
}
//Execute Request to update Action's script
var response = request.execute();
var statusCode = response.statusCode;
System.debug("Status Code: "+statusCode);
if(statusCode == 200){
System.log("Action "+actionList[j].name+" script updated successfully");
}
else{
System.log("Action "+actionList[j].name+" script updation failed");
var responseBody = JSON.parse(response.contentAsString);
System.error(response.contentAsString);
}
}
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment