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

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!

cbarlock
Kilo Guru

That works!  Thank you.  Why it is necessary if my suspended column is a Date/Time?

Hi,

I believe it's because you're passing the current record through to your script include as a function parameter and along the way the object type is changing to an element instead.

Anytime you're working with a field outside of the actual retrieval of that record, this can happen.

Please mark my above post as Helpful & Correct. Thank you and have a great weekend! 


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

cbarlock
Kilo Guru

That's very good to know!  Thank you again.