Help

Automation: Getting the most recent Rec using script

2631 5
cancel
Showing results for 
Search instead for 
Did you mean: 
Lost-Merovingia
7 - App Architect
7 - App Architect

I'm in over my head.  I got ChatGPT to create this script for me, so ... I don't know if I've activated SkyNet or not.

So I'm creating an automation, and I noticed that sometimes there are duplicate records, because the Webhook comes in and runs when a record is created and updated (so it creates a record for both).  I'm trying to create something to minimize this.

Screenshot of automation: https://tinyurl.com/2q5a57je

When I try to update the record, I can't choose the record that the script outputs.

5 Replies 5

The script looks to be using what I would consider outdated technique which we can argue, why ChatGPT is using var instead of let or const, or is using for() instead of .filter(), .map(), .reduce(), forEach(). and why it's also using .then instead of await.

I think this is the problem with ChatGPT, in that, if users haven't run through enough of the subject matter to know right from wrong, what's old technique, what's modern technique, what's common technique and what's preferable technique - it might actually compound your learning of the subject.

Not to say that there isn't a million ways to do the same thing in JavaScript. ChatGPT is powerful though, and where I would then take the conversation from your sample script, is ask it to use an await, and tell it to use .filter(), .map(), forEach(), or .reduce(), and ask it why it's using var instead of const or let declaration. You need to call it out as you start to understand the structure of code - and surprisingly, in many cases it will correct these problems and give you legit/healthier looking code.

But anyway, I digress - back to focusing on your actual problem! 😂

Within an Automation Action itself, you'll see there's the ability to add input from other previously run actions, such as your Find Records, or the Trigger itself. You can then uses these inputs within your Automation script accessing then through the input.config() function.

My example below shows you an easy way to take those inputs values, and by using the JavaScript destructuring technique, assign them directly into their own self-named variables.

Karlstens_0-1674161033279.png

 

Inputs are a great way to get started with Automation Scripting. I've personally found a couple of limitations with them however, where I prefer to use API calls to find the data that I'm needing in my Automation - where typically I'll only grab the Trigger record input, but not any other data, or at least, I'm very selective with what data I draw-in to my script via input.config() - there are pro's and cons, it will all depend on your project. Mostly, input.config() will typically do what you need it to do for you.

 

Another thing to keep in mind when trying to use a script created by ChatGPT is that ChatCPT doesn't actually understand your question and doesn't actually think through the necessary logic.

Your screen shot doesn't show the entire script, so it isn't clear what the script outputs, if anything. Your script also does not appear to do anything with the "Find Records" action.

On the other hand, if you want to identify the most recent record, why do you need a script to do this? When a record is created, it is the most recent record. So have an automation based on a "when record created" automation. The automation can then unflag the previous "most recent" record, and then flag itself as the most recent record. Note that if you have multiple records created at the same time, this could cause problems due to the delay in executing automations. But a script could also have the same issues (although a script is better able to handle the situation).

I asked ChatGPT to refactor some simple VB code this morning, and although it's impressive, its example failed to return the correct result. I find it's great for learning isolated technique and commands - I tend to think of it as a manual on steroids (as much as I hate that term...), and if anything it's certainly replacing my time googling/stackOverflowing (which I often find is not very a inclusive community in structure) - ChatGPT is amazing, but it's certainly not the insta-fix to everything code.

Lost-Merovingia
7 - App Architect
7 - App Architect

Thanks for all the help.  I'm still at a loss on what to do.

I've got this script from the examples:

// the table to check
let table = base.getTable("Stays");
// the record we're searching for duplicates of.
// we need to create a 'recordId' input variable connected to a record trigger
let config = input.config();
let recordId = config.recordId;
// the field to save duplicates in. this should be a self-linked record field
let duplicatesField = table.getField("HAID");
// query the table and find our record according to its id:
let query = await table.selectRecordsAsync({fields: []});
let record = query.getRecord(recordId);
// search for duplicates
let foundDuplicates = query.records.filter((potentialDuplicate) => {
    // if they're the exact same record, they're not duplicates:
    if (potentialDuplicate === record) {
        return false;
    }
    if (potentialDuplicate.name === record.name) {
        // if the names match, we've found a duplicate:
        return true;
    }
    return false;
});
console.log(`Found ${foundDuplicates.length} duplicates of ${record.name}`);
// save the duplicates:
await table.updateRecordAsync(record, {
    [duplicatesField.name]: foundDuplicates,
});
""
The table is called 'Stays' and the record that I need to isolate the most recent is base on the 'HAID'.  The input is the result of the last action.
LostMerovingia_1-1674241245224.png

Maybe I need to create a field with the record ID in it?  Maybe then I don't even need a script like @kuovonne  said.

Lost-Merovingia
7 - App Architect
7 - App Architect

Actually!!! Maybe I can do this in a view.  I can filter out one record per HAID and use MAX() ... somehow.  Does that make sense?