Developing and debugging MID server script include scripts, especially to CMP/Cloud Provisioning and Governance,
can be a little tricky and slow since your native logging is done with the ms.log()
function
and you can't see the logs from the ServiceNow instance,
but instead you need to grab the logs from MID server.
Another problem is the script/function execution and context management (login to cloud provider, etc.).
Let's say you need to develop new REST API based functions (day 2 action, like change CPU and memory of a VM) to VMware. How would you develop and test your code? One option is to make a full debugging catalog item and call that from the end user cloud portal. This is an OK end-2-end testing solution but it really is unnecessary if you only create small functions and test those and it adds way too much ServiceNow overhead.
One solution to mitigate this problem is to test and run your MID server script include scripts
from ServiceNow instance Script Background by using the JavascriptProbe.
One slight problem is how to handle context from catalog item.
This can be easily mocked by prototyping the .get()
method.
With this example you will be able to run any of your own MID server scripts
and get the results quickly to webhook.site without ms.log()
.
Note that all the javascript has to be a single string that is passed to the setJavascript()
method!
var defStr = 'var LoginParams = function(url,username,password){this.Endpoint=url;this.Identity=username;this.Credentials=password;};';
// prototype the .get() method
var proStr = 'LoginParams.prototype.get = function(name){return this[name]};';
// add your cloud provider / vmware url and credentials
var parStr = 'var params = new LoginParams("https://10.10.10.10:443/sdk","[email protected]","mypassword123");';
// VMWareRestAPI is just an example, here you would put your own class, takes login params as parameter
var iniStr = 'var api = new VMWareRestAPI(params);';
// here you can call your own functions
var exeStr = 'var res = api.getVirtualMachinePowerState("vm-2062");';
// this will log out results from the function call to webhook.site
var posStr = 'var req = new Packages.com.glide.communications.HTTPRequest("https://webhook.site/870427f1-e2a3-40ca-a7bb-d71ffcc9024c0");req.post(JSON.stringify({res: res}));';
// your mid server name
var midServerName = 'my-midserver';
var jspr = new JavascriptProbe(midServerName);
var js = defStr + proStr + parStr + iniStr + exeStr + posStr;
jspr.setName('VMWareRestAPI');
jspr.setJavascript(js);
jspr.create();
// see log info at: https://webhook.site/#!/870427f1-e2a3-40ca-a7bb-d71ffcc9024c