Auto-Number each new row when using Inline Record Creation

  • 1
  • Question
  • Updated 3 years ago
  • Answered
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!
Photo of Sam Becker

Sam Becker

  • 1,158 Points 1k badge 2x thumb

Posted 3 years ago

  • 1
Photo of David Giger

David Giger

  • 1,758 Points 1k badge 2x thumb
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
Photo of Barry Schnell

Barry Schnell, Champion

  • 18,076 Points 10k badge 2x thumb
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!
Photo of Sam Becker

Sam Becker

  • 1,158 Points 1k badge 2x thumb
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.
Photo of Barry Schnell

Barry Schnell, Champion

  • 18,076 Points 10k badge 2x thumb
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.

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