Populate DateTime of previous row in model

  • 1
  • Question
  • Updated 4 years ago
  • Answered
I've been hacking away at a snippet for a little while now to try and add a default date value when a new row in a model is created. Currently this is done using the actions framework, with a value of 'TODAY'.

What I really need is for this date to be the same as the date in the row which was previously created. So....if row 1 contains a dateTime of 27 Oct etc, then row 2 needs to have that date. If row 3 has 29 Oct, then row 4 also needs to have 29 Oct.



A couple of thoughts:

Is it possible to access the 'last' row in the model? There is a function for getFirstRow() but is there a getLastRow() or equivalent?

Is it possible to count the number of rows, ie .length, and then subtract 1 and get that specific row?

My incomplete snippet is pasted below if anyone can assist. Thanks a lot. 

var $ = skuid.$,params = arguments[0],model = skuid.model.getModel('Sectors');
//var prevSector = 0;
var sectorCount = model.data.length;
//get last row in model
var row = sectorCount - 1;
//thisDate = params.model.getFieldValue(params.row,'stack__Date__c');
model.updateRow({
    additionalConditions:[
        {field: 'stack__Date__c', value: params.Sectors.data.n.stack__Date__c}
        ]
});
Photo of Greg Jarrett

Greg Jarrett

  • 3,496 Points 3k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
I suppose this could work you just have to change "n" to "row".

var $ = skuid.$,params = arguments[0],model = skuid.model.getModel('Sectors');
//var prevSector = 0;
var sectorCount = model.data.length;
//get last row in model
var row = sectorCount - 1;
//thisDate = params.model.getFieldValue(params.row,'stack__Date__c');
model.updateRow({
    additionalConditions:[
        {field: 'stack__Date__c', value: params.Sectors.data.row.stack__Date__c}
        ]
});
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Assuming that this snippet is being fired by your "Add Sector" button, this should do the trick. It will use TODAY as the default if there are NO previous rows, but if there ARE any previous rows, it will use the last one's stack__Date__c field:

var $ = skuid.$,   
   params = arguments[0],
   model = params.model,
   
// Create a new row in our Model,
// with the stack__Date__c field defaulted to the same date as the last row in the table   
rows = model.getRows(),
numRows = rows.length,
defaultDate;
// If we have a previous row, default it to that rows date
if (numRows) defaultDate = rows[numRows-1].stack__Date__c;
// Otherwise default it to NOW / TODAY
else defaultDate = 'TODAY';
model.createRow({
    additionalConditions:[
        {field: 'stack__Date__c', value: defaultDate}
    ]
});
Photo of Greg Jarrett

Greg Jarrett

  • 3,496 Points 3k badge 2x thumb
Thanks Moshe & Zach, that one's working nicely - to add to it, is it possible to force the new row to be added at the end of the model AND in edit mode?

Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Yes, change the createRow() call to add the "editModeForNewItems" and "doAppend" options:

model.createRow({
    additionalConditions:[
        {field: 'stack__Date__c', value: defaultDate}
    ],
    editModeForNewItems: true,
    doAppend: true
});
Photo of Greg Jarrett

Greg Jarrett

  • 3,496 Points 3k badge 2x thumb
That works perfectly. Thanks a lot.