Help

Generate alphanumeric string and input it in a field

5150 4
cancel
Showing results for 
Search instead for 
Did you mean: 
Andreas_Iacovid
6 - Interface Innovator
6 - Interface Innovator

can I use a formula (javascript) to generate an alphanumeric string and then the result to be inputed automatically in a field.

4 Replies 4

Hi @Andreas_Iacovides - you can use the unique record ID for this with the formula:

MID(RECORD_ID(), 4, LEN(RECORD_ID())-3)

This uses the unique and random record ID with the “rec” prefix stripped off.

If you want something more customisable or different to the record ID you can do this with a script, but note that the result would be random but not necessarily unique.

// Generate a random alphanumeric string and update a field
// Note that the generated string is random, but *not necessarily* unique
// The string generator function is cribbed from here:
// https://stackoverflow.com/questions/10726909/random-alpha-numeric-string-in-javascript

// The string generator function:
function randomString(length, chars) {
    var result = '';
    for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
    return result;
}

// Set the table
let randomTable = base.getTable("Random");
// Get the records
let randomQuery = await randomTable.selectRecordsAsync();

// Loop through the records
for (let record of randomQuery.records) {
    // if the field is empty...
    if(!record.getCellValue("Random")){
        // then generate a new random string for it..
        let rString = randomString(20, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
        console.log(rString);
        // and update the field in the table
        await randomTable.updateRecordAsync(record, {
            "Random": rString
        })        
    }
}

As this is a script it wouldn’t be automatic - you would need to run the script to populate the empty records.

JB

Andreas_Iacovid
6 - Interface Innovator
6 - Interface Innovator

Thanks so much.

Regarsing the javascript how can i run this code in a particular field?

Also what do u mean is going to be random but not unique?

@Andreas_Iacovides - you have to run the script in a script block (currently available to all Airtable plans):

Screenshot 2020-04-03 at 16.01.50

More info on blocks here:

The record ID method will always provide a unique value as it represents a unique records within Airtable as a whole (AFAIK), not just your base or your workspace.

But the script generates a random string each time. Highly unlikely for a long string, but it could generate the same string twice and so might not be unique. Probably not a major issue depending upon your use case, but worth knowing in case it is important.

JB

Jonathan tnx so much. Lastly i want to ask for help for the following:

i need to generate a qr code containing the link of a record in the table. I want this to be automatic i. E when a record is created then a qr code to be generated in a fied for tha record.
I assume this shall be achieved with javasctipt. So u have such code??