Skip to main content

Hi there, I’m trying to create an airtable automation script whereby it takes in the Full name of a record and changes only the first letter into uppercase (upper caps). This is my code, I can’t seem to get it to work, can anyone help me with this?

 

let table = base.getTable("Training Masterlist");

 

// Use the correct field name that maps to the automation's Record ID

let recordId = input.config().AirtableRecordID;  // Correct this if it's named differently

 

// Ensure that the recordId exists, if not, log an error

if (!recordId) {

    throw new Error("Record ID is undefined.");

}

 

// Field name

let fullNameField = "Full Name (As in NRIC)";

 

// Fetch the record that triggered the automation

let record = await table.selectRecordAsync(recordId);

 

// Get the current value of the "Full Name (As in NRIC)" field

let fullName = record.getCellValue(fullNameField);

 

if (fullName) {

    // Convert the full name to the desired format

    let formattedName = fullName

        .split(" ") // Split the name into words by spaces

        .map(word => {

            // Capitalize the first letter and make the rest lowercase

            return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();

        })

        .join(" "); // Join the words back together

 

    // Update the record with the new formatted name

    await table.updateRecordAsync(recordId, {

        >fullNameField]: formattedName

    });

}

 

Hey ​@NdrewY,

I did not go through the full script yet, but if the only thing you need the script for is to make first letter of the value under a given field to be upper cap, and all the rest of the letters to be lower cap, I really believe that you will be better off using a formula to such effect.

Please try out the formula below, and replace {Your Field} with your actual field name.

 

UPPER(LEFT({Your Field}, 1)) & LOWER(MID({Your Field}, 2, LEN({Your Field})))


As a rule of thumb, I try to formulas rather than automations (specially if the involve scripts) as (i) automations can be error-prone; and (ii) there are some hard limits to Airtable automations you will want to avoid.

Please let me know if this helps, or if you want me to go through the details of the script! Feel free to reach out.

Mike, Consultant @ Automatic Nation


Could I confirm you’ve set up the ‘AirtableRecordID’ variable?  If that doesn’t help, could you share a read only link to your base with no records in it?  That’ll make it a lot easier to help you!

 


Hey!

Check out this case change script: https://www.lomlabs.io/airtable/scripts/case-change (free to use for 100 runs w/o license key)

 

use “start case” to capitalize the first letter of each word



Clone this sample base to your workspace if you’d like to test it out: https://airtable.com/invite/l?inviteId=inv1rDjAj9dqqcqwE&inviteToken=38375de0f1cb77aed6ad7e02f3b7af73fbf32b9e6ea0e13c523c9c371cadfb65

 

Disclaimer: some names include lowercase particles that shouldn't be capitalized, especially in multicultural contexts. My script does not take this into account. e.g. "bin" or "binti" (Mohammed bin Yusof), "a/l" or "a/p" (Raj a/l Ramasamy), and European particles like "de", "van", or "di" (e.g. André de Souza, Luca di Montezemolo)


Reply


OSZAR »