Conditional Inline Snippet on Date

  • 1
  • Question
  • Updated 4 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 4 years ago

  • 1
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 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();
currentDate.setHours(0,0,0,0);
//compare dates.
if(field.metadata.accessible){
    if(jsDate < currentDate){
// do CSS stuff here...
        field.element.css({'color':'#F22'});
    }

//put updated css into Skuid field renderer
    skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);

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: