Client Script to look for everything created after a certain date

Stacy1
Mega Guru

I have a client script where I only want to set the fields to mandatory if the item was created after 6-2017-06-02 23:59:59.

It's not working, can I not do a basic greater than script?   My alert comes up empty??? 😞

function onLoad() {

    }

var cdt = g_form.getValue('sys_created_on');

alert ("date created is " + cdt);

if   (cdt>('2017-06-02','23:59:59')){

Thanks,

Stacy

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Stacy,



Step 1: Create a display business rule and set the filter condition created date | after | date. The conditioning filter is required here so that this is only trigger per your filter defined.


Place the below line in between function template


g_scratchpad.mand = true;



Step 2: Now create the client script as



if (g_scratchpad.mand)


      {


  g_form.setMandatory('fieldname',true);


}


View solution in original post

6 REPLIES 6

You are very welcome Stacy. Thanks for participating in the community!


srinivasthelu
Tera Guru

Hi Stacy,



Unfortunately   No ..



You can not simply use client side > operator as the date and time format display format is dependent on user preference.




Some one can use a format of yyyy-dd-mm hh:mm: ss others can have yyyy-mm-dd hh:mm:ss



you could do date and time format conversions at client side and then can attempt tying using the > operator, but here comes another chaos in the form of



dd-mm-yyyy.




for example, 02-06-2012, 02-07-2011 dates. String comparison goes letter by letter from left side and so system would think 02-07-2011 greater than 02-06-2012 ugh!!!!..




So, you might want to go with server side comparison as date and time objects as pradeepksharma suggested, I have just added below bit (bold)to his code




if (g_scratchpad.mand && g_scratchpad.mand== 'true' )


      {


  g_form.setMandatory('fieldname',true);


}