Help

Help with script - find record ID from table

Topic Labels: Scripting extentions
3786 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Michael_Andrew
7 - App Architect
7 - App Architect

Following up on: Help on Script with Math Function

I have a new question. How can I find the record id of a record in a table. Here is my code so far:

let table = base.getTable(‘Project List’);
let queryResult = await table.selectRecordsAsync();
let record = queryResult.getRecord(queryResult.recordIds[0]);
console.log(record)
let yearstring = record.getCellValue(‘Year String’)
console.log(yearstring)
let yeartable = base.getTable(“Year”)
let yeartablequery = await table.selectRecordsAsync();
[here I want to add a filter/find function to get record - call it linkedrecord]
await table.updateRecordAsync(record.id, {“Year”: linkedrecord})
let year = record.getCellValue(‘Year Count’);
console.log(year);
let num = parseInt(year)+20018;
await table.updateRecordAsync(record.id, {“Project #”: num})

What I want to do is filter/find the record in yeartable by the name yearstring in field ‘Name’. In other words, I have a list of records in yeartable and the Name field is the Year. I want to search the table for the record with a name that matches yearstring, because I want to link that record in the other table.

3 Replies 3
gwynn_kruger
6 - Interface Innovator
6 - Interface Innovator

It looks like you’re missing the for loop part below:

let projectTable = base.getTable("Project List");
let projectRecord = await input.recordAsync('Pick a record', projectTable);
if (projectRecord) {
    let yearstring = projectRecord.getCellValue("Year String");
    let yearTable = base.getTable("Year");
    let yearTableQuery = await yearTable.selectRecordsAsync();

    for (let yearRecord of yearTableQuery.records) {
        if (yearRecord.getCellValue("Name") === yearstring) {
            let linkedrecord = yearRecord.id;
            await projectTable.updateRecordAsync(projectRecord, {"Year Link": [{id: linkedrecord}]});
        }
    }
}

The Project List table with a Do Linking button added that calls the script:

image

The Year table with the text based year in the Name field (as you described above):

image

Michael_Andrew
7 - App Architect
7 - App Architect

Hi, thanks so much for adding this. I should have clariifed that this is an automation. So I don’t want an input code line, but want to use record.id of the record that kicked off the automation. I note that it’s possible to input a variable (the record ID), but I’m not sure how to use in the code (FYI, I have no coding skills at all). I don’t know what to replace the input line with to get the record id of my variable and use that.

Also, there are two things I’m trying to accomplish. The first is to grab the record from Year table and update the Project List table with that in the field ‘Year Link’ but then as a second function, get the ‘Year Count’ number and add to the 20018 and update the field ‘Project #’

image

All input variables are available on a special input.config() method. Use it like this (borrowing from your screenshot):

let config = input.config()
let startRecord = config.StartRecord

Now the startRecord variable contains the record ID of the triggering record.