how to compare two date fields in application scope. I uses getGlideObject method but it is giving me the error as this function has been blocked from your application scope??

Saurabh Kolte
Kilo Contributor

I am using this code for comparing two date fields

function onBefore(current, previous) {

  //This function will be automatically called when this rule is processed.

  if ((!current.today_s_date.nil())&& (!current.bill_generation_date.nil())) {

  var start = current.today_s_date.getGlideObject().getNumericValue();

  var end = current.bill_generation_date.getGlideObject().getNumericValue();

  if (start > end) {

  gs.addInfoMessage('bill generation date should not be greater than todays date');

  current.today_s_date.setError('bill generation date should not be greater than todays date');

  current.setAbortAction(true);

  }

  }

}

2 REPLIES 2

The SN Nerd
Giga Sage
Giga Sage

current.today_s_date is a GlideElement field.


The Scoped API for this object can be seen below.


You'll note there is no getGlideObject() method.



List of Scoped Scriptable Objects - ServiceNow Wiki


Scoped GlideElement API Reference - ServiceNow Wiki



GlideDate is the Glide object you want to work with Date fields.


List of Scoped Scriptable Objects - ServiceNow Wiki



GlideDateTime is the Glide object you want to work with DateTime fields.


http://wiki.servicenow.com/index.php?title=Scoped_GlideDateTime_API_Reference#gsc.tab=0



Try this code instead



function onBefore(current, previous) {


  //This function will be automatically called when this rule is processed.


  if ((!current.today_s_date.nil())&& (!current.bill_generation_date.nil())) {


  var start = (new GlideDateTime(current.getValue('today_s_date'))).getNumericValue();


  var end = (new GlideDateTime(current.getValue('bill_generation_date'))).getNumericValue();


  if (start > end) {


  gs.addInfoMessage('bill generation date should not be greater than todays date');


  current.today_s_date.setError('bill generation date should not be greater than todays date');


  current.setAbortAction(true);


  }


  }


}



Let me know if it works as I have not tested.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

It works fine, the script you shared is the solution.



thank you