Access previous value of field?

  • 2
  • Question
  • Updated 3 years ago
  • Answered
If I have a model with unsaved changes, is there a way that I can access the value of a changes field that is still on the server (the saved version)?

An example:

I have field Date__c which is a DateTime.
I change Date__c in the skuid model.
Before I save the model, can I access the value of Date__c on the server, (and compare it to Date__c in the model?)
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb

Posted 4 years ago

  • 2
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
The model has these values.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Sweet. Thanks, Pat. I thought it had to be there somewhere... I just missed 'originals' when I did a quick scan.
Photo of Jared Jones

Jared Jones

  • 2,214 Points 2k badge 2x thumb
Is there a simple way to display the original value in a template without a js snippet?
Photo of Barry Schnell

Barry Schnell, Champion

  • 18,076 Points 10k badge 2x thumb
Hi Jared -

Unfortunately, I don't think there is a way without some javascript.

In theory, you could write a simple UI only field such as:

{{$Model.Account.originals.{{Id}}.Name}}

However, merge syntax inside of merge syntax isn't supported currently and since there is no way to "concatenate" inside of a formula and then process merge syntax to the concatenated result your left with JS.

You have a couple of options:

1) Create a UI only field for each original you care about.  Then, write a JS snippet that is triggered on model events like row.updated.  In the JS, update the UI only field (make sure to use updateRow api) to correspond to originals values and then use the UI only field anywhere on your page.

2) Write a field renderer that displays the original value that you want displayed.  On your component (e.g. field editor, table, etc.) add a field for the field that you are concerned with (e.g. Name) and then set to use the custom renderer.  It's important to use the actual field that you care about in all cases because you want that field to re-render when it changes elsewhere in order to display the original.

Hope this helps!
Photo of Jared Jones

Jared Jones

  • 2,214 Points 2k badge 2x thumb
Many thanks, Barry...again.  I forgot that I hadn't thanked you and a couple weeks slipped by.

I ended up using the first option, but it took me way longer than it should have to figure out the correct syntax to access the original value (as I am relearning javascript after many years of neglect).
The error handling is due to the fact that original value is undefined until an update occurs.

On the chance that this may help others, here's a snippet for a custom renderer that captures the original value of the field.

var $ = skuid.$,    params = arguments[0],
    values = arguments[1],
    model = arguments[0].model,
    row = arguments[0].row,
    element = arguments[0].element;
    
    var originalValue;
    var rowId = row.Id;
try {
    originalValue = model.originals[rowId].Latest_Update__c;    
} catch (e) {
    originalValue = values;
}
element.append( originalValue );

Many happy returns!
Photo of Barry Schnell

Barry Schnell, Champion

  • 18,076 Points 10k badge 2x thumb
No worries Jared, your welcome - glad you got it working!