Conditional Inline Snippet on Date

  • 1
  • Question
  • Updated 5 years ago
  • Answered
Hi, I am new to Javascript and while finding another article helpful, I need some assistance. I am adding an Inline snippet and I would like the "Available_to__c" and "Available_from__c" to be green and red if the "Available_to__c" date is passed TODAY.
Photo of Yina Chun

Yina Chun

  • 792 Points 500 badge 2x thumb
  • Confused

Posted 5 years ago

  • 1
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,828 Points 20k badge 2x thumb
Here is what you would do. 

Step 1.  Add the Available_from__c and Available_to__c fields to your model and to your table. 

In the field properties change the rendering properties to "custom"  and add a name of your choosing there.  I used OverDue

Step 2.  Go to resources and add a new Javascript resource.  
- The type should be "In-Line (Snippett)" 
- The name should be the same name you 

The code should be somthing like this: 

$ = skuid.$;
// get value of the Avaialable_to__c field in current row
var field = arguments[0];
var value = field.model.getFieldValue(field.row,'Available_to__c'); 

//translate to Javascript Date
var jsDate = skuid.time.parseSFDate(value);
//get current date and push time back to beginning of day
var currentDate = new Date();
//compare dates.
    if(jsDate < currentDate){
// do CSS stuff here...

//put updated css into Skuid field renderer

This should work for you. 

Note:  If the field 'avaialble_to__c' is a date time field you will need to adjust your code where you translate to a Javascript date.  It should read: 
var jsDate = skuid.time. parseSFDateTime(value);
This is how it will look: