End an automation partway through with script

Hey all, is it possible to stop an automation rule midway if certain conditions are met in a script?

I have a deduplication script that uses GraphQL to count the number of matching entities and delete the most recent one if the total number exceeds 1. I would like to notify a user as the next step, but only if the script has to delete an entity. I’m wondering if there is an ‘end’ or ‘stop’ function that I’m missing.

// Developer reference is at api.fibery.io/#action-buttons

// Fibery API is used to retrieve and update entities
const fibery = context.getService('fibery');
const SPACE_NAME = 'Data'

for (const entity of args.currentEntities) {
    const codeProduct = await fibery.getEntityById(entity.type, entity.id, ['Product']);
    const productID = codeProduct['Product'].id;
    const query = `{findProducts(id: { is: "${productID}" }) {id,name,productSkus(type: { name: { is: "Code" } }) {id,name}}}`;
    const result = await fibery.graphql(SPACE_NAME, query);
    console.log(result);
    const ids = result.data.findProducts[0].productSkus.length;
    console.log(ids)
    if (ids > 1) {
        await fibery.removeCollectionItem('Data/Product', productID, 'Product SKUS', entity.id);
    }
}

You can simply return - but make sure you’re awaited all Promises you’ve received from calling FIbery api.

1 Like

Thanks Matt, my lack of Javascript knowledge is definitely slowing me down a bit. I tried adding return to my script but I realized that I actually need to stop the automation from running a notification step as opposed to just ending the script. I don’t think that’s possible without throwing an error and if I do that then the script gets disabled if it happens too often. I think I can work around it by adding a “duplicate” checkbox that the script can set true and then use that to trigger a notification instead. Still trying to work around the basic fact that you can’t conditionally notify someone based on the outcome of a script.

// Developer reference is at api.fibery.io/#action-buttons

// Fibery API is used to retrieve and update entities
const fibery = context.getService('fibery');
const SPACE_NAME = 'Data'

for (const entity of args.currentEntities) {
    const user = args.currentUser
    console.log(args.steps[1])
    console.log(user)
    const codeProduct = await fibery.getEntityById(entity.type, entity.id, ['Product']);
    const productID = codeProduct['Product'].id;
    const query = `{findProducts(id: { is: "${productID}" }) {id,name,productSkus(type: { name: { is: "Code" } }) {id,name}}}`;
    const result = await fibery.graphql(SPACE_NAME, query);
    console.log(result);
    const ids = result.data.findProducts[0].productSkus.length;
    console.log(ids)
    if (ids > 1) {
        await fibery.removeCollectionItem('Data/Product', productID, 'Product SKUS', entity.id);
    }
    else {
        return;
    }
}

Indeed, it is the only way I know of aborting the automation entirely.

And yes, this would be my next recommendation, except you don’t need to have another automation, you could use a formula for the To field that is either the User who triggered the rule, or empty.

How you this be written? I can’t figure out how to write an If statement that lets me leave the false outcome as blank since it requires both the true and false outcomes to be a User type.

Best I could come up with was this and it actually seems work but it’s pretty messy.

If(
  [Step 1 Product SKU].Notify = true,
  [User who triggered Rule],
  [Users (fibery)]
    .Filter([Active?] = false)
    .Sort()
    .First()
)

It might seem messy, but it’s actually a perfectly fine way of doing it :blush:

1 Like

Related: Allow to return an empty value from a formula - #2 by Matt_Blais

1 Like