Help

Scripting: Can't seem to pass User field's value to another User field.

Topic Labels: Automations
Solved
Jump to Solution
555 12
cancel
Showing results for 
Search instead for 
Did you mean: 
byrnes
6 - Interface Innovator
6 - Interface Innovator

I suspect this is a super easy thing to do, but I have been googling for over an hour now and all attempt to make this script function is seemingly failing.

 

const { recordId } = input.config();
const table = base.getTable('tbliyrxpGU6tkQgG7');

let record = await table.selectRecordAsync(recordId);

let user = record.getCellValue("Requestor (Airtable)");
console.log(user)

await table.updateRecordAsync(recordId, { "Requestor (AT)": { id: user }});

 

Requestor (Airtable) is a Lookup Field that is pulling in a User.
Requestor (AT) is a (Single) User field.

The console log shows that 'user' is indeed an object that has id, email, name. But no matter how I try to pass the user onto the actual user field, it won't take it. "Cannot accept the provided value."

1 Solution

Accepted Solutions
TheTimeSavingCo
18 - Pluto
18 - Pluto

Hm, not sure if you've already resolved this, but maybe try this? 

 

const { recordId } = input.config();
const table = base.getTable('tbliyrxpGU6tkQgG7');

let record = await table.selectRecordAsync(recordId);

let user = record.getCellValue("Requestor (Airtable)")[0];
console.log(user)

await table.updateRecordAsync(recordId, { "Requestor (AT)": { id: user }});

 

Link to base

Screen Recording 2024-05-20 at 2.49.48 PM.gif

See Solution in Thread

12 Replies 12
PedroCerrano
4 - Data Explorer
4 - Data Explorer

From the code above, you're trying to pass a "user" object to a "string" id. That will never work.

Have you tried drilling into the user? Like this:

{ "Requestor (AT)": { id: user.id }})

 

Same problem, unfortunately.

byrnes_0-1715969913807.png

 

Here's an example of something I did today:

employeeTable.updateRecordAsync(employee, {
    'LINK to Business': [{'id': matchingBusiness?.id ?? ''}]
})

Make sure if you need optional chaining and also (if needed) to provide a default value.

Curious, what "type" is the object? Hover over it. Should read something like "User_Record" or "User_Record | undefined

byrnes_0-1715971046378.png

 

I expected something other than "any."

Is what you posted all of your script? I cannot reproduce the error because I can't destructure input.config(). It tells me I'm missing an argument.

BTW, I'm new to the Airtable community (as of two days ago) . iOS, React Native and JavaScript developer by trade, so there's a LOT that I don't yet know about AT. I just like helping people 😄

Only thing missing here is me defining the input variable,

byrnes_0-1715972845070.png

Otherwise,
Requestor (Airtable) is a Lookup Field that is pulling in a User.
Requestor (AT) is a (Single) User field.

byrnes_1-1715972937685.png

 

As I understand what you're trying to do is update a User field value by using a Lookup field, based on a Linked Record, correct?

Your code from the first post reads like you have identified the table and you're trying to update one record, but it's not clear from the code which record you want to update. Make sense? Reads like "update a record." But which one?

It would make sense if you wanted to check ALL to update ALL, but that's not what your code says. It says selectRecordAsync not selectRecordsAsync

Where I'd be digging? To provide more definition on the input.

Thank you for all the help! This feels really silly because it seems like most common "can paste from one cell into another and it accepts it" has varying degrees of complicated code when switching into script. I can almost never find examples on the web when scripting for our workspace.

I am trying to take the user in the (Airtable) field and put them into (AT) field.

We have a user(s) table where the source column is a formula field that creates a username based on name + company username. One of the fields on the user(s) table is their Airtable account. Half of our users work off of airtable and thus do not have actual accounts. This means when they submit a task to our team to do, they select themselves from the list of usernames on the user(s) table. Everyone internally uses their actual accounts in most scenarios. 

When someone internal, that has an account, uses the username method instead of the account method, I want to then pull their associated account back via lookup field (Airtable), then place that user account into the (AT) field. 

Nice. The developer in me thinks that if I know exactly the fields I want to pull from, I'd drill down there. Tell the script to look for those fields and then "paste" the new value.

Here's a global idea:

const table = base.getTable('tbliyrxpGU6tkQgG7');
const requestorField = table.getField("Requestor (Airtable)");
const requestorATField = table.getField("Requestor (AT)");
const usersResults = table.selectRecordsAsync({fields: [requestorField, requestorATField]})

for (const user of usersResults.records) {
    // maybe include a conditional check if the fields were different?
    table.updateRecordAsync(user, { "Requestor (AT)": { id: user }});
}