So I dont want to write a script at start event instead i want to read a file placea at some external location in my script task. Script Type is Javascript.
Is there any way in which we can read external files ?
My use case is I want to assign some value to variable which is not workflow dependent and which a user can change at any point of time without changing the bpmn file.
@paritosh took at the specific post that i linked to. The specific post has a code sample that is for “loading a config file”, but you can modify that script to load any file you wish, and instead of loading that file from the process deployment resources, you can point your script to a network volume where you are storing your config file.
something like
var classloader = java.lang.Thread.currentThread().getContextClassLoader()
var myConfigFile = classloader.getResourceAsStream('configs/myConfigFile')
So you are using the script in the link as a base: all of the parts you need / logic are in that code snippet example. Just modify it for your purpose of loading from a external file (like in the two lines above), and then set your process variable based on the config file data.
Hi @StephenOTT Thanks for the solution. One thing i want to know here, i was getting the file as bytearray inputstream and i tried to convert the inputstream to string using the link you have provided but i am getting the following error -
Unable to evaluate script: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: “Java” is not defined.
please let me know what should be the correct way to convert the bytearrayinputstream to String in the script task
var classloader = java.lang.Thread.currentThread().getContextClassLoader()
var resource = classloader.getResourceAsStream(‘workflow/workflow.properties’)
var IOUtils = Java.type(‘org.apache.commons.io.IOUtils’);
var String = Java.type(‘java.lang.String’);
var myConfigFile = S(new String(IOUtils.toByteArray(resource), ‘UTF-8’));