Help

Changing the value of a linked record field

Topic Labels: Scripting extentions
Solved
Jump to Solution
5076 9
cancel
Showing results for 
Search instead for 
Did you mean: 
TWu
6 - Interface Innovator
6 - Interface Innovator

Got a newbie question for you. I’ve got 2 tables linked together: Viewers and Movies, through a field on Movies table called, “Fans”.

I’m trying to look up the viewer of a movie and change their status to “Favorite” if it is currently “Watched”.

let viewer = record.getCellValue("Viewers");
if {name: viewer[0].getCellValueAsString('Status')} == "Watched" {
    updateRecordAsync(writer[0], {
        'Status': {name: 'Favorite'};
    })
}

The error gives me an unexpected token ‘{’, which I suspect isn’t the real problem. Any ideas here? Would love to get my first script block running!

1 Solution

Accepted Solutions

As I said you’d have to query your Viewers records, so I just rewrote your whole script:

let movieTable = base.getTable("Movies");

let viewerTable = base.getTable("Viewers");
let viewerQuery = await viewerTable.selectRecordsAsync()
let viewers = viewerQuery.records

let record = await input.recordAsync('Pick a record', movieTable);

let linkedViewers = record.getCellValue("Viewers") || [];

if (linkedViewers.length > 0) {
    let viewerId = linkedViewers[0].id
    
    let viewer = viewers.filter(x => x.id == viewerId)[0]
    
    if (viewer.getCellValueAsString('Status') == "Submitted") {
            await viewerTable.updateRecordAsync(viewer, {
                'Status': {name: 'Agreement'};
            })
        }
}

See Solution in Thread

9 Replies 9

Anytime you call updateRecord (or create, or delete), you have to do “await”, as its an asynchronous function, and you have to define the table.

await table.updateRecordAsync(writer[0], {
   'Status': {name: 'Favorite'};
})

Thanks for that clarification. I have made those 2 adjustments and now I run into the error:

This condition will always return 'false' since the types '{ name: any; }' and 'string' have no overlap.(2367)

Here is my code thus far:

let movieTable = base.getTable("Movies");
let viewerTable = base.getTable("Viewers");
let record = await input.recordAsync('Pick a record', movieTable);

let viewer = record.getCellValue("Viewers");
if ({name: viewer[0].getCellValueAsString('Status')} == "Submitted") {
        await viewerTable.updateRecordAsync(writer[0], {
            'Status': {name: 'Agreement'};
        })
    }

Just now saw you’ve defined an object and are in fact comparing it to a string. There doesn’t appear to be a reason to do {name: ...}.

if (viewer[0].getCellValueAsString('Status') == "Submitted") {...}

you’re probably going to have to query your Viewers table first, and adjust your viewer variable to retrieve the record where recordId == record.getCellValue("Viewers")[0].id

My “Status” column in the Viewers table is a single select and my linked record field is “Viewers”. I’m getting this error now

viewer[0].getCellValueAsString is not a function

Is it possible that I’m not pulling the linked record properly? I am trying to pull the record but perhaps I’m pulling the value?

Can you post the entirety of your script? There’s no way of telling what writer is and why its apparently either an array or an object.

Apologies, it is my entire script, I edited my answer above. More clarification below

2 tables: Movies, Viewers.
Movies is linked to Viewers under the column “Viewers”
Viewers has a single select called “Status”

As I said you’d have to query your Viewers records, so I just rewrote your whole script:

let movieTable = base.getTable("Movies");

let viewerTable = base.getTable("Viewers");
let viewerQuery = await viewerTable.selectRecordsAsync()
let viewers = viewerQuery.records

let record = await input.recordAsync('Pick a record', movieTable);

let linkedViewers = record.getCellValue("Viewers") || [];

if (linkedViewers.length > 0) {
    let viewerId = linkedViewers[0].id
    
    let viewer = viewers.filter(x => x.id == viewerId)[0]
    
    if (viewer.getCellValueAsString('Status') == "Submitted") {
            await viewerTable.updateRecordAsync(viewer, {
                'Status': {name: 'Agreement'};
            })
        }
}

Thank you, you’ve written me my first script and I’ll be able to adapt to a few more cases!

Quick question before you go: My Viewers table has 20,000 entries so it takes a long time to run. Is there a way to speed this up, especially since the linked record points at the exact entry already? Seems unnecessary to query the entire table no?

Yes, you have to query the table. Otherwise I wouldn’t have written the script that way. Linked record fields don’t return a record in Scripts, they return an object with just the ID and “name” of the record. Functions performing actions on records need the whole record, not the ID (which is why the update function is being passed viewer and not viewerId).