May 17, 2024 10:41 AM
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."
Solved! Go to Solution.
May 19, 2024 11:51 PM
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 }});
May 17, 2024 11:04 AM
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 }})
May 17, 2024 11:18 AM
Same problem, unfortunately.
May 17, 2024 11:29 AM
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"
May 17, 2024 11:37 AM
May 17, 2024 11:50 AM
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 😄
May 17, 2024 12:09 PM
Only thing missing here is me defining the input variable,
Otherwise,
Requestor (Airtable) is a Lookup Field that is pulling in a User.
Requestor (AT) is a (Single) User field.
May 17, 2024 12:47 PM
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.
May 17, 2024 01:20 PM
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.
May 17, 2024 01:50 PM
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 }});
}