The CreatorCon Call for Content is officially open! Get started here.

Conditional field styling based on date field within a scope

Ina Therese N_1
Mega Contributor

Hi,

I wish to achieve conditional styling based on a date field, but I am struggling with the syntax for the code;

  • I have a custom date field in a scoped application (called avventer_svar_innen)
  • I want to add styling (somewhere) based on the date field (e.g. if the date is passed: make some field red)
  • Mission: get my date field, get todays date - compare the two. Should be easy enough, but I am having no luck. 

I have achieved some conditional styling based on other criteria, as posted by Manjul here. I have also learned that I need to use GlideDate.subtract (and not gs.dateDiff) as this is a scoped application. 

Here is one thing I tried, which I thought would to the trick, but no luck:

Javascript: if (GlideDate.subtract(new GlideDate().setValue(current.avventer_svar_innen), new GlideDate()).getDayPart()) < 0;

What am I missing here? Thanks in advance! 

1 ACCEPTED SOLUTION

Hi,

The suggested code didn't work for me, but I got it working with this code: 

javascript: GlideDateTime.subtract(new GlideDateTime(current.avventer_svar_innen),  new GlideDateTime()).getDayPart() > 0;

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

use this and it will work; I have applied the color to same field i.e. the date

you can give your custom field as well;

I can apply color to Field1 based on the Date value as well

give value as below

javascript: new GlideDateTime(current.avventer_svar_innen) < new GlideDateTime()

I tested this in my scoped app table and it is working on list

find_real_file.png

find_real_file.png

Mark āœ… Correct if this solves your issue and also mark šŸ‘ Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi,

The suggested code didn't work for me, but I got it working with this code: 

javascript: GlideDateTime.subtract(new GlideDateTime(current.avventer_svar_innen),  new GlideDateTime()).getDayPart() > 0;

asifnoor
Kilo Patron

Hi, you need to use setDisplayValue and it will work. I tried in background script and this is working fine. 

Javascript: var gd = new GlideDate(); gd.setDisplayValue(current.avventer_svar_innen); 
 if(GlideDate.subtract(gd,new GlideDate()).getDayPart()) <0 

Mark the comment as a correct answer and helpful once worked.

Ina Therese N_1
Mega Contributor

Thanks for the replies! I didn't get it to work with the suggested code, but I have added the code that worked for me in my reply above.