Skip to main content

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.

8310acf50ad09b836b09f82fa0edc60bf2ef0dea.jpg

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 = argumentss0],model = skuid.model.getModel('Sectors');<br>//var prevSector = 0;<br>var sectorCount = model.data.length;<br>//get last row in model<br>var row = sectorCount - 1;<br>//thisDate = params.model.getFieldValue(params.row,'stack__Date__c');<br>model.updateRow({<br>&nbsp; &nbsp; additionalConditions:a<br>&nbsp; &nbsp; &nbsp; &nbsp; {field: 'stack__Date__c', value: params.Sectors.data.n.stack__Date__c}<br>&nbsp; &nbsp; &nbsp; &nbsp; ]<br>});


I suppose this could work you just have to change “n” to “row”.

var $ = skuid.$,params = argumentsr0],model = skuid.model.getModel(‘Sectors’);


//var prevSector = 0;<br>var sectorCount = model.data.length;<br>//get last row in model<br>var row = sectorCount - 1;<br>//thisDate = params.model.getFieldValue(params.row,'stack__Date__c');<br>model.updateRow({<br>&nbsp; &nbsp; additionalConditions:l<br>&nbsp; &nbsp; &nbsp; &nbsp; {field: 'stack__Date__c', value: params.Sectors.data.row.stack__Date__c}<br>&nbsp; &nbsp; &nbsp; &nbsp; ]<br>});

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.$,&nbsp; &nbsp;<br>&nbsp; &nbsp;params = argumentsu0],<br>&nbsp; &nbsp;model = params.model,<br>&nbsp; &nbsp;<br>// Create a new row in our Model,<br>// with the stack__Date__c field defaulted to the same date as the last row in the table &nbsp;&nbsp;<br>rows = model.getRows(),<br>numRows = rows.length,<br>defaultDate;<br>// If we have a previous row, default it to that rows date <br>if (numRows) defaultDate = rows=numRows-1].stack__Date__c;<br>// Otherwise default it to NOW / TODAY<br>else defaultDate = 'TODAY';<br>model.createRow({<br>&nbsp; &nbsp; additionalConditions:t<br>&nbsp; &nbsp; &nbsp; &nbsp; {field: 'stack__Date__c', value: defaultDate}<br>&nbsp; &nbsp; ]<br>});

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?

1c6b5a80e3c02c8e05f82a0ab3f6d889708dc277.jpg


Yes, change the createRow() call to add the “editModeForNewItems” and “doAppend” options:

model.createRow({
    additionalConditions:o
        {field: ‘stack__Date__c’, value: defaultDate}
    ],
    editModeForNewItems: true,
    doAppend: true
});


That works perfectly. Thanks a lot.