Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Include Third Party Javascript Library

thisisauniqueus
Giga Expert

Hi,

Once again im bitten by DataTime issues. Can I include a third party jacascript lib in service now and then use it in Transform Map. If yes please share the procedure.

For example if i want to include Moment Timezone   in service now is it possible?

P.S. the format i am trying to parse is 07/16/15 16:22:05 which gives me exception. Target format in which i want it to be is 2016-07-07 20:04:27

This is the code that i tried

var sourceTime = '07-16-15 16:22:05';

var gd = new GlideDateTime(sourceTime);

gd.setValueUTC(sourceTime, "MM-dd-yyyy HH:mm:ss");

var test = gd.getValue();

gs.print(test);

Exception:

Evaluator: java.lang.RuntimeException: '07/16/15 16:22:05' could not be parsed using format string 'yyyy-MM-dd HH:mm:ss' :Unparseable date: "07/16/15 16:22:05" Caused by error in script at line 3 1: var sourceTime = '07/16/15 16:22:05'; 2: var gd = new GlideDateTime(sourceTime); ==> 3: gd.setValueUTC(sourceTime, "yyyy-MM-dd HH:mm:ss"); 4: var test = gd.getValue(); 5: gs.print(test); 6: com.glide.glideobject.GlideDateTime.setValueUTC(GlideDateTime.java:255) sun.reflect.GeneratedMethodAccessor1949.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.mozilla.javascript.MemberBox.invoke(MemberBox.java:138) org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:292) org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2577) org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:42) org.mozilla.javascript.gen.null_null_10905._c_script_0(null.null:3) org.mozilla.javascript.gen.null_null_10905.call(null.null) org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:560) org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3439) org.mozilla.javascript.gen.null_null_10905.call(null.null) org.mozilla.javascript.gen.null_null_10905.exec(null.null) com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:236) com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:107) com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:73) com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:322) com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:283) com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:254) com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:242) com.glide.processors.ScriptProcessor.evaluateScript(ScriptProcessor.java:320) com.glide.processors.ScriptProcessor.runScript(ScriptProcessor.java:215) com.glide.processors.ScriptProcessor.process(ScriptProcessor.java:173) com.glide.processors.AProcessor.runProcessor(AProcessor.java:412) com.glide.processors.AProcessor.processTransaction(AProcessor.java:187) com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:165) com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:49) com.glide.sys.ServletTransaction.run(ServletTransaction.java:34) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) java.lang.Thread.run(Thread.java:745)

If i am doing it wrong please suggest.

Regards,

1 ACCEPTED SOLUTION

Thank you John. Keep me informed and let me know if it worked.


View solution in original post

7 REPLIES 7

The time needs to be in a format that ServiceNow can digest. yyyy-MM-dd HH:mm:ss works best.



What I would recommend is a few more lines of code to parse the input format and re-arrange it in to the format that works for the setValueUTC. For example (untested code disclaimer here):



var sourceArr = sourceTime.split(' ');


var sourceDateArr = sourceArr[0].split('/');


var newSourceTime = '20' + sourceDateArr[2] + '-' + sourceDateArr[0] + '-' + sourceDateArr[1] + ' ' + sourceArr[1];


gs.print('New Time=' + newSourceTime);


var gd = new GlideDateTime(sourceTime);


// Print the new variable with UTC and timezone applied


gs.print('gd.getValue()=' + gd.getValue() + ' gd.getDisplayValue()=' + gd.getDisplayValue());


Thanks alot Chuck. Ill follow your advice



Best Regards


Thank you John. Keep me informed and let me know if it worked.