Auto-Number each new row when using Inline Record Creation

I can’t think of a good way to auto-number each new row added to a table within my page. I’d like my “Line Item” field to auto-populate with a new number for each row created when the “Inline Record Creation” button is clicked. Right now I have an action set to update the “Line Item” field with the {{Id}} of the row when a new row is created. This works, but it starts at 2 and then jumps to 5 on the next row and so on. Can anyone think of a way to make it start at 1 and then 2,3,…? (The order doesn’t really matter) Thanks for the help!

Hi Sam,

Afaik the Autonumber is generated by SalesForce, meaning you have to save the record before you can access the Name (I suppose it’s a Name field).

The value of {{Id}} you get there are autogenerated Ids from Skuid. Those are needed from Skuid to identify each row even though the row is not saved in SalesForce yet.
A good use-case for that would be if you want to create two records relating to each other. You reference those Ids and Skuid then tells Salesforce that those two records are related. SalesForce then saves the relationship between those two records correctly for later use.

To answer your question: You could generate your own Autonumber using JS, which is not a good solution because the Autonumber won’t be unique anymore. I’d rather retrieve the latest saved record and generate a new Autonumber based on it’s Name. If you create new rows you have to acknowledge that (meaning to “remember” the latest generated Autonumber). That process is far from multiuser proved, so I would just let SalesForce generate the Autonumber.

Cheers

Hello Sam -

I’ve had to do this same thing for a “Line Number”.  You have a couple of different options:

1) Write a JS snippet that gets called when a row is created.  In the snippet, iterate all the rows in the model and identify the “MAX” Line number value.  Then, update the row that was created with MAX+1

2) Create a second model on the page that is used strictly for tracking the MAX line number by using a UI Only field.  Write a JS snippet that gets called when LineNumber field is updated.  In that snippet, compare the value of the row being updated to the value in your “MAX Line Number Model” - if it’s greater, update the Max Line Number model.  Then, write a snippet that gets called when a row is created and updates that row with the MAX Line Number model value + 1.

Hope this helps!

Thanks for the help! However, I’m a bit confused. David, I don’t need the number to be unique. “Auto-Number” may be the wrong terminology. The purpose of the number is to just have a reference number for each row on the table. The fields in this table will eventually be used by another app (DrawLOOP) to create a document with a table of products that need to be ordered and the “Line Item Number” is just a reference to each line within the order. Each new order will start over from 1. (It’s for a Purchase Order). I get the use of SKUIDs row Ids. I was only using the row Ids because it’s the only number I can think of that increases with each new row. Barry, I see where you are going, but I’m not familiar enough with JS to write a script to do this. Could you provide a sample or point me in the right direction? Thanks again.

Hi Sam -

Skuid exposes all data rows in the “data” property of a model so that’s the starting point. The below should provide what you need, you’ll just need to adjust the field name ‘LineNumber__c’ to correspond to the field name in your system.

If you setup a model action for your model to initiate on new row created and call the snippet ‘handleRowCreated’, you should encounter the behavior you are looking for.

&nbsp; &nbsp;skuid.snippet.registerSnippet('handleRowCreated', function(eventArg) {&nbsp; &nbsp; &nbsp; &nbsp; var initiatorId = eventArg.initiatorId<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , modelId = eventArg.modelId<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , model = eventArg.model<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , row = eventArg.row<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , rows = model &amp;&amp; model.data<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , maxLineNumber = 0; // initialize it to zero<br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; // if the model has rows<br>&nbsp; &nbsp; &nbsp; &nbsp; if (rows &amp;&amp; rows.length) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // iterate all the rows looking for the maximum line number<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.each(rows, function(id, row) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// if this row has a line number and if if it greater than our current max line number<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (row.LineNumber__c &amp;&amp; maxLineNumber &lt; row.LineNumber__c) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maxLineNumber = row.LineNumber__c;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; // assign the next line number to the newly created row<br>&nbsp; &nbsp; &nbsp; &nbsp; model.updateRow(row, 'LineNumber__c', maxLineNumber + 1, {initiatorId: initiatorId});<br>&nbsp; &nbsp; });