Script to look up multiple emails from Google Calendar event hitting script limit by less than a second

I am trying to figure out how to get my script to run under the 30 second limit. The Find Records automation doesn’t handle a list and I am trying to loop through a short list of Google Event attendees to see if they exist in a Contacts table.

If they exist, I want to add the Contact name to an array. If they don’t, I want to create a new record and then add them to the array by Name.

Then I want to create a Meeting record that adds the Attendees by name to the Meeting record.

let attendees = input.config();
let customers = new Array();
let contacts = base.getTable("Contacts");

let contactRecords = await contacts.selectRecordsAsync({fields: ['Email']});

for (let i = 0; i < attendees.attendees.length; i++){
        var attendee = attendees.attendees[i]
        
    if (attendee.includes("@tray.io") === false){
        
        for (let record of contactRecords.records){
            if (attendee === record.getCellValue("Email")){
                customers.push(record)
            }
            else{
                let newContact = await contacts.createRecordsAsync([
                     {
                    fields: {
                        'Name': attendee.substr(0, attendee.indexOf('@')),
                        'Email': attendee,
                    }
                      }]
                      )
                customers.push(newContact)
            }
        } 
    }
}

Hey @Derek_Squires

Any chance you could just compile the list of emails from your Contacts table into an object and then compare that directly against your attendees list?

That way you’d only loop through your Contacts records the once, and the comparison would be near instant as it’s done against the object keys

(Apologies to any experienced programmers if I’m butchering terminology here)

Your script gone wrong, because you are looping await contacts.createRecordsAsync against each new record, while you have to use it once for array of new records.
Also, createRecords returns array of IDs. A record is an object {‘id’:id, ‘name’:name}. I don’t think pushing it into the same array is OK.

let attendees = input.config();
let contacts = base.getTable("Contacts");
let contactRecords = await contacts.selectRecordsAsync({fields: ['Email']});

let existing=new Map(contactRecords.records.map(r=>[r.getCellValue("Email"),r.id]))

let filteredAtt=attendees.filter(a=>!a.includes("@tray.io"))
let newrec=a=>({fields: {'Name': a.split('@').shift(), 'Email': a})
let create=filteredAtt.filter(fa=>!existing.has(fa)).map(newrec)
let newrecs= await contacts.createRecordsAsync(create)
let customers=[...newrecs,...filteredAtt.filter(fa=>existing.has(fa)).map(a=>existing.get(a))]

sorry if mistaken, i’m in hurry

This topic was solved and automatically closed 15 days after the last reply. New replies are no longer allowed.