1
votes

brand new to App Maker (loving it). I need help to create a auto-number field, so my App can be referenced by a uniqueID. My previous app used Sheets and I was able to compile a script that got the lastvalue and added 1 for each new record. Here was my old code:

var SS = SpreadsheetApp.openById(Key).getSheetByName('Data');
var LR = SS.getLastRow();
var SC = SS.getRange('A1').getValue();
if(! SC){SS.getRange('A1').setValue(1);return}; 
var colValues = SS.getRange('A1:A').getValues();
var CNT=0;
for(var r in colValues){ 
if(Number(colValues[r][0]>CNT)){CNT=colValues[r][0]}; 
} CNT++ ;

var setCID = SS.getRange('a1').offset(LR, 0);

So if the Previous Clinic ID was 27, setCID would be 28

Cant seem to find anything about this. Can anyone guide me using AppMaker as this obviously wont work. Thanks. Deryk

1

1 Answers

0
votes

Welcome to App Maker! So first of all, we already assign a unique key to all records, which you can access using the "_key" property. However, for Drive Tables this is a random ID, not an incrementing one. It's good enough to refer to by ID, but it is kind of long and ugly. If this is good enough, I suggest going this route.

If it's not good enough, I can try to expand this question, but it's a fair bit tougher. (The rough summary is you in the "onSave" for your record, you need to query for the record with the current highest ID, and add to it. But it's not that simple, you need to do this within a lock, otherwise you expose yourself to race conditions. Plus, this will make saving records a bit slower.)