Script/other solution: Capture values before updates

Hi guys, I am looking how to capture the information from a record before it is going to be updated.
Scenario:
I have two columns 1) {Current location} 2) {Last time location}. Column {Current location} is 3 times per week updated by CSV import.

I’m looking how to capture in {Last time location} the values from {Current location} before those are updated so by this to analyze difference between {Current location} and {Last time location}.

Real case: Today {current location} is Monaco so {Last time, location} should become empty.
Tomorrow {current location} is going to be Paris so {Last time, location} should became Monaco.

I saw before on the forum that there is a solution for that and it was used to record Notes like: Column {Comments} and {Global comments} for each line.

  • So the script was working like:
    When values as text are inserted in {Comments} - those are automatically moved in {Global comments} and merge {Comments} values with existing {Global comments}.

In my case I’m looking for a script or other solution that will just overwrite instead of merging data with the existed one.

Many thanks in advance for your time and help!

Hi,
you can use some temp field and 2 automations: one runs when temp not empty and not match Current => move Current to Last move temp to Current.
Second automation - temp not empty and match Current => just clear temp.

I would create single automation, “when temp field not empty” with script step. In script config, choose variables and name them after field values, then put this code:

let rec={...input.config()}  
if (rec.temp!=rec.current) rec.last=rec.current;
rec.current=rec.temp
rec.temp=''
output.set('record',rec)

it nice to use script inside automation, you don’t need to write code for load data/save data, just data processing part.
Note that if your fields are not text, some types can’t be cleaned by
rec.temp=''. You should pass empty array or rec.temp=null instead

Then add next step, Update record. Use values from script step according to fields.

Hi Alexey,
And first of all many thanks to pay attention to my post - really apricate this.

So I tried to not go to may case with the script that you provide till I will not understand the logic of it.
I created a table with fields as you name them in your script and tried to run a test - and from the fact that my script knowledge are “0” - I cant understand why it does not work. From the log I see that there are many error in the way how I tried to use your script.
Can you please help me how to handle this ! :pray: :pray: :pray:

ERROR

TypeError: Invalid arguments passed to output.set(key, value): • value.current should be a JSON-serializable type, not undefined

at main on line 5

or

Property ‘temp’ does not exist on type ‘{}’.(2339)

Veiw link: Airtable - Grid view

Hi,

i suppose, you define input variables in a correct way (otherwise script would not reach output step).
it should be something like that
image

Well, i had some school programming backround very long time ago, before Windows era, and refused to understood all those object-oriented stuff from books. But it’s easier to learn when you create something yourself. you are not supposed to be a developer, but to work in airtables, you need to get some basic things.

I think it fails because some of your fields type is not ‘string’.

if you put text field value in some variable, like ‘temp’, you work with it directly.
some fields, like single select, multiselect, lookups, contain
arrays, it’s like a box of ordered values [ ‘Monaco’, ‘Paris’, ‘Nice’ ]

or objects, the box of named values
in my example, “rec” is object. it may be described as
{current:‘Monaco’, last:‘Paris’, temp:‘Nice’}

You can refer to Field types reference for more info.
But right now, let’s see what you got here:

try to paste this, to see your data types, and i’ll tell you how to fix it

let rec={...input.config()}  
output.set('last',typeof(rec.last))
output.set('current',typeof(rec.current))
output.set('temp',typeof(rec.temp))

Hi Alexey and thanks one more time for your time and patience.

Regrind work with airtbles - I know that I really should learn coding basics to use all features and to be able to build really powerful thinks - but to be honest I don’t know from what to start - it should be html or JavaScript?

About your kind last script suggestion - here what is the output from it:

Hi,
It seems like you didn’t define input parameters
here is a sample how it should be done.

image

After you defined them, you can begin write a script.
Parameters stored in read-only variable input.config()

If parameters are defined, you will see it as possible values, when typing variable name, like this

The rest are as described before

I don’t think you’ll need html for it. Regarding JS, for now it’s enough to read and understand some basic examples, like this

i supposed you already prepared first automation step, like this

image

if not, i would recommend to try and create some simple automation, without scripting at all, just get some value by trigger, put in to other cell. then, after you automation executed succesfully, you will better understand how to create the same, but using script after trigger, like on my last picture.