The CreatorCon Call for Content is officially open! Get started here.

Christopher_Mal
ServiceNow Employee
ServiceNow Employee

I spent all night trying to figure out why what I know works in Java code can't be used in a MID server script includes. I mean this is basic IO stuff -- kind of.

I am doing an integration where I take the attachments associated with an incident and send them to an FTP server. I will share how that all works in my next blog, as you can use the same idea to send XML, database query results, reports, etc.

For this blog I have to share a major sticky point with Rhino and Javascript. The scripting engine has a hard time with overloaded Java methods and can't distinguish what method you are calling based on the parameters passed in. This article was helpful in understanding the issue (but not fixing it) :
http://www.mozilla.org/rhino/ScriptingJava.html

After working through the night trying every data stream I could possibly think of, I finally consulted the Oracle of all oracles this morning, John Andersen on my team. He mentioned that he had struggled through that same issue a while back and that you had to call an overloaded Java method differently, than the normal way, by explicitly having to identify which method you are executing. I owe you a lunch friend. I have a great Don Chuy's burrito coming your way when you visit next week.

So in the case of FileOutputStream I needed to write a byte[] like this:
fos["write(byte[])"](this.payloadBytes);

You can see I have to specify the byte[] as the param type I am passing in. Hopefully my blog will show up on someones Google search some troubled night for them. If you don't call the overloaded method as above you will see an error similar to this:

06/11/12 23:30:22 (510) Probe: JavascriptProbe:OpenTextAttachmentCopy WARNING *** WARNING *** org.mozilla.javascript.EvaluatorException: The choice of Java method java.io.FileOutputStream.write matching JavaScript argument types (byte[]) is ambiguous; candidate methods are: void write(byte[]), void write(int) (script_include:OpenTextAttachmentCopy; line 55)

Another tip that I picked up on my own while surviving on a diet of Cheese-Its and black tea last night is that you can get the byte[] from any base64 encoded string (like one you would send if you are passing attachments via web service) like this:
this.StringUtil = Packages.com.glide.util.StringUtil;
this.StringUtil.base64DecodeAsBytes(probe.getParameter("attachmentPayload"));



this.payloadBytes = this.StringUtil.base64DecodeAsBytes(probe.getParameter("attachmentPayload"));
this.fileName = probe.getParameter("attachmentFileName");

if(this.fileName.length() > 0 && this.payloadBytes) {
var file = new this.File(this.MIDSERVER_FILE_PATH + "/" + this.MIDSERVER_FILE_NAME);
var fos = new this.FileOutputStream(file);

try {
fos["write(byte[])"](this.payloadBytes);
} catch (e) {
this.log("Error in writing the file: " + e);
} finally {
fos.flush();
fos.close();
}
}


Anyway, happy coding all. I have a day full of meetings and miles to go before I sleep. Cheers.