Update Date/Time Formula when the input Date/Time field is edited

  • 1
  • Question
  • Updated 4 years ago
I'm working from this post that describes how to create a custom renderer on a formula that listens for changes to the input fields.  I have a very similar use case, but am using a Date/Time field and suspect I am getting tripped up on the translation from Salesforce Date/Time format to Javascript Date/Time and back.  

When I use the custom renderer, nothing happens.  My formula field continues to display its existing value until I save.

Here's my snippet:


var FIELDS_TO_LISTEN_ON = ['Nurse_Intake_Time_Patient_Time_Zone__c'];
var field = arguments[0],
     value = arguments[1],
     row = field.row,
     listener;

skuid.ui.fieldRenderers.DATETIME[field.mode](field,value);

// Register a listener so that if any of FIELDS_TO_LISTEN_ON are updated,
// then we will update ourself as well.

if (!listener) {
   
   listener = new skuid.ui.Field(row,field.model,null,{register:false});

   var calcNewValue = function(){

       var  value0 = row.Nurse_Intake_Time_Patient_Time_Zone__c || 0,
              value1 = skuid.time.parseSFDateTime(value0),
              value2 = row.Nurse_Assignment__r.UTC_Offset__c || 0,
              value3 = row.Account.tz__UTF_Offset__c || 0,        
        
        newValue0 = value1 + (value2/24 - value3/24);
        newValue=skuid.time.getSFDateTime(newValue0);
       
       field.model.updateRow(row,'Nurse_Intake_Time_Patient_Time_Zone__c',newValue);

       field.element.empty();
       skuid.ui.fieldRenderers.DATETIME.readonly(field,newValue);
       
   }
   
    listener.handleChange = function(){calcNewValue (); };
    listener.render = function() {};

   skuid.$.each(FIELDS_TO_LISTEN_ON,function(i,fieldToRegisterOn){
       field.model.registerField(listener,fieldToRegisterOn);
   });

   setTimeout(function(){
       // If there is already a changes row for this item,
       // then run calculation
       if (field.model.changes[field.row.Id]) {
             calcNewValue ();
       }
   },100);

}
Photo of Elissa Bradley

Elissa Bradley

  • 1,672 Points 1k badge 2x thumb

Posted 4 years ago

  • 1

Be the first to post a reply!