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.

Failure to substract GlideDateTime object

cbarlock
Kilo Guru

I have a table with a "suspended" column that is a Date/Time object.  I wrote a business rule that calls a function in a script include:

    deletable: function(current) {
        if (current.state == 'suspended') {
            var interval = gs.getProperty(x_ibmwa_gpra_polic.deleteSuspended);
			var suspended = current.suspended;
            var now = new GlideDateTime();
            if (GlideDateTime.subtract(suspended, now).getDayPart() < interval) {
                var msg = 'Cannot delete ' + current.getDisplayValue();
                msg += '.  Must be suspended for at least ' + interval + ' days.';
                gs.addErrorMessage(msg);
                current.setAbortAction(true);
            }
        }
    }

The subtract fails with:

org.mozilla.javascript.EvaluatorException: Can't find method com.glide.glideobject.GlideDateTime.subtract(com.glide.script.glide_elements.GlideElementGlideObject,com.glide.script.fencing.ScopedGlideDateTime).

Why is suspended a GlideElementGlideObject and not a ScopedGlideDateTime?  I found a reference to GlideElement.getRefRecord and tried:

var suspended = current.suspended.getRefRecord();

but the failure was the same.

1 ACCEPTED SOLUTION

Allen Andreas
Tera Patron

Hi,

Can you try using these lines for the suspended portion instead?

var suspended = current.suspended.getDisplayValue();
var suspendedTime = new GlideDateTime();
suspendedTime.setDisplayValue(suspended);
if (GlideDateTime.subtract(suspendedTime, now).getDayPart() < interval) {

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

6 REPLIES 6

Shishir Bose
Kilo Contributor

You can also make suspend a GlideDateTime Object 

var suspended = (new GlideDateTime(current.getValue('suspended'))); 

 

This will solve the problem

 

Please mark reply as Helpful/Correct, if applicable. Thanks!

Hi,

In the Correct answer, I also made it a GlideDateTime object, just approached it differently with a few added steps to make sure it worked. What you did was do it in the same line, but in the end, it's the same thing that I did.

Thanks! 🙂


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!