Help

Re: Using a script to add values from one field to another

8109 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Jens_Schott_Kn1
4 - Data Explorer
4 - Data Explorer

Non-developer type here! My team is using Airtable as a task management tool Each week we run a report, which includes a long text field (“Details Field”) with details on the work performed on a specific task during that week. To retain a record of the content that’s appeared in the Details Field, I wonder if it would be possible to write a script that would copy and clear the values of the Details Field and cumulatively add them to a field (“History Field”). The History Field would then eventually contain a list of all the updates that’s been put into the Details Field over time (and ideally with date stamps to next to the text that was copied at a given time).

Thanks so much for your feedback on this!

Jens

31 Replies 31

Hi @JonathanBowen

Thank you!

It will only be to copy Multi select field options, since I have the list created already, so this will be great.

Now, silly question. Do I just copy and paste the script into the block? Once I do this, do I need to do anything in the multi select field?

Pardon my questions, this is quite new to me.

Thank you for your patience.

Mary Kay

Hi @M_k, so the only things you should need to change are the table name (my script refers to “Table 1”) and the two multi-select field names (where I have “Multi-select 1” and “Multi-select 2”). Change these values to match your base. In fact, maybe make a copy of your base first, edit the values noted above to match your base, then try it out. Assuming all is well, then move it over to your original base (making sure the table and field names are still good).

Beyond that, there should be nothing. The script doesn’t care what the multi-select values are as long as they are the same in both MS fields.

Any probs, post back here :slightly_smiling_face:

JB

@JonathanBowen, As a follow up question, is it possible to set the colors for the multi-select options as well? It seems looking through the different field options for multi-select it should be possible, and if I had a column/array of the color options that it looked to for the choices.

These are great examples, I’m seeking to use the script block to populate image url’s into image fields. I have an image url field and have been attempting to modify your code to copy / past to the image field, but it needs an array.

“Error: Can’t set cell values: invalid cell value for field ‘Image’.
Cell value has invalid format: must be an array.
Attachment field value must be an array of objects. New attachments must have property ‘url’ and optional ‘filename’. Existing attachments must be passed back unmodified.”

Do you have any ideas as to how to go about using the script block for image updating in this manner?

Thank you.

@Hannah_Henry

Attachment fields have a different write format. Even a single attachment must be an array of object(s).

See the Cell value write format for the multipleAttachments field type in the API reference for scripting block.

TYPEDEF
Array<{
url: string,
filename?: string,
}>

Example:

[ { url: "https://dl.airtable.com/foo.jpg" } ]
Sabostar
4 - Data Explorer
4 - Data Explorer

@JonathanBowen thanks for the multi-select script example. I have a similar challenge, but would like to copy comma separated values from one field to another which is a linked value field (so the unlinked values become linked values). Any ideas?

Hi @Sabostar - If I understand your question correctly, using a script you want to copy a value in one field like this:

item1, item 2, item 3

to a linked field (i.e. the values item1, item 2 and item 3 are values in the primary field of the linked table). Is that right?

You can’t do this at the moment - at least not directly. Using the script block you can update a linked field but you need to know the record IDs of the records in the linked table. From the script docs the format is:

Screenshot 2020-04-16 at 11.50.34

So, in code, this would be something like:

table.updateRecordAsync(record, {
  MyLinkedField: [
    {id: recabc12345678},
    {id: recabc12345abc},
    {id: recabc12345xyz}
  ]
});

Where the record IDs (“rec…”) above are the record IDs from the linked table.

It is possible to do this in a script - I did have something similar to this that I was working on, so will try and put this out later today. Tbh, I’m sure someone else has already put out something like this on the community, but I can’t put my hand on it. Other members might be able to point to it in the meantime.

This script from @Sam_Cederwall is also along similar lines so you might be able to tweak this for your use case:

JB

Sabostar
4 - Data Explorer
4 - Data Explorer

Thanks for the quick response @JonathanBowen.

Your assumption is accurate on what I am trying to achieve. This is part of a project to successfully backlink between rows in the same table. At the moment I am using a script to conduct the backlink (works fine), but then I have to join two linked fields on the same row into a single field, which I’m using a concatenate formula to achieve, but I can’t do that on a linked field, hence the last step of trying to copy and paste via a script to a linked field. Perhaps I could use a script to join the two linked fields
instead?

Hope that makes sense!

I have tried the same thing and copy exactly everything, yet I still get this error

P: Can't set cell values: invalid cell value for field 'Multi-select 2'.
Could not find a choice with that ID or name
at main on line 12

image

image

Your multi-selects were probably created in a different way so the choices have different ids and/or names.

If you are sure that the target field contains all of the choices in the source field, you can map the original choices to make the array match the write format for a multi-select, using the name but not the id of the choice.