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.

What is the alternative to "getGlideObject()" for scoped custom applications?

Navaneeth1
Tera Guru

I have a date validation business rule that uses the following script:

 

(function executeRule(current, previous /*null when async*/) {
var workStartElem = current.planned_start_date;
var workEndElem = current.planned_end_date;
var workStartGDT = workStartElem.getGlideObject();
var workEndGDT = workEndElem.getGlideObject();

if (workStartGDT.after(workEndGDT)) {
gs.addErrorMessage(gs.getMessage("{0} must be after {1}", [ workEndElem.getLabel(), workStartElem.getLabel() ]));
current.setAbortAction(true);
return;
}

// The method of duration calculation is used instead of a condition (in the condition builder)
// to ensure we use the same method of comparison between client and server
var FIFTY_YEARS_MILLIS = 1576800000000; // 1000 * 60 * 60 * 24 * 365 * 50
var duration = GlideDateTime.subtract(workStartGDT, workEndGDT).getNumericValue();
if (duration > FIFTY_YEARS_MILLIS) {
gs.addErrorMessage(gs.getMessage("{0} cannot be more than 50 years after {1}", [ workEndElem.getLabel(), workStartElem.getLabel() ]));
current.setAbortAction(true);
}
})(current, previous);​


This script is from a global business rule and it uses getGlideObject() in lines 4 and 5 which is not supported in my custom scoped application and is throwing an error stating:Function getGlideObject is not allowed in scope x_g_#####.

 

Can someone please provide an alternative to this?

Thank you in advance!
1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Navaneeth1 

Can you try using GlideDateTime

 

(function executeRule(current, previous /*null when async*/) {
    var workStartElem = current.planned_start_date;
    var workEndElem = current.planned_end_date;
    
    // Create GlideDateTime objects directly
    var workStartGDT = new GlideDateTime(workStartElem);
    var workEndGDT = new GlideDateTime(workEndElem);
    
    if (workStartGDT.after(workEndGDT)) {
        gs.addErrorMessage(gs.getMessage("{0} must be after {1}", [ workEndElem.getLabel(), workStartElem.getLabel() ]));
        current.setAbortAction(true);
        return;
    }
    
    // The method of duration calculation is used instead of a condition (in the condition builder)
    // to ensure we use the same method of comparison between client and server
    var FIFTY_YEARS_MILLIS = 1576800000000; // 1000 * 60 * 60 * 24 * 365 * 50
    var duration = GlideDateTime.subtract(workStartGDT, workEndGDT).getNumericValue();
    
    if (duration > FIFTY_YEARS_MILLIS) {
        gs.addErrorMessage(gs.getMessage("{0} cannot be more than 50 years after {1}", [ workEndElem.getLabel(), workStartElem.getLabel() ]));
        current.setAbortAction(true);
    }
})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

2 REPLIES 2

Maddysunil
Kilo Sage

@Navaneeth1 

Can you try using GlideDateTime

 

(function executeRule(current, previous /*null when async*/) {
    var workStartElem = current.planned_start_date;
    var workEndElem = current.planned_end_date;
    
    // Create GlideDateTime objects directly
    var workStartGDT = new GlideDateTime(workStartElem);
    var workEndGDT = new GlideDateTime(workEndElem);
    
    if (workStartGDT.after(workEndGDT)) {
        gs.addErrorMessage(gs.getMessage("{0} must be after {1}", [ workEndElem.getLabel(), workStartElem.getLabel() ]));
        current.setAbortAction(true);
        return;
    }
    
    // The method of duration calculation is used instead of a condition (in the condition builder)
    // to ensure we use the same method of comparison between client and server
    var FIFTY_YEARS_MILLIS = 1576800000000; // 1000 * 60 * 60 * 24 * 365 * 50
    var duration = GlideDateTime.subtract(workStartGDT, workEndGDT).getNumericValue();
    
    if (duration > FIFTY_YEARS_MILLIS) {
        gs.addErrorMessage(gs.getMessage("{0} cannot be more than 50 years after {1}", [ workEndElem.getLabel(), workStartElem.getLabel() ]));
        current.setAbortAction(true);
    }
})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Community Alums
Not applicable

Hi @Navaneeth1 ,

You can use new GlideDateTime(<your Variable name>). I checked in my PDI this will work for you

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak