# End an automation partway through with script

**URL:** <https://community.fibery.io/t/end-an-automation-partway-through-with-script/5225>\
**Category:** Get Help\
**Created:** [September 29, 2023, 11:35pm UTC](https://community.fibery.io/t/end-an-automation-partway-through-with-script/5225 "2023-09-29T23:35:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tommy\_Hedley](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/tommy_hedley/32/6468_2.png) [@Tommy\_Hedley](https://community.fibery.io/u/Tommy_Hedley)\
**Post date:** [September 29, 2023, 11:35pm UTC](https://community.fibery.io/t/end-an-automation-partway-through-with-script/5225/1 "2023-09-29T23:35:37Z")

</div>

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.

```auto
// 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);
    }
}

```

---

<div class="post-metadata">

**Author:** ![Matt\_Blais](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/matt_blais/32/1464_2.png) [@Matt\_Blais](https://community.fibery.io/u/Matt_Blais)\
**Post date:** [September 30, 2023, 12:09am UTC](https://community.fibery.io/t/end-an-automation-partway-through-with-script/5225/2 "2023-09-30T00:09:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tommy\_Hedley](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/tommy_hedley/32/6468_2.png) [@Tommy\_Hedley](https://community.fibery.io/u/Tommy_Hedley)\
**Post date:** [September 30, 2023, 1:34am UTC](https://community.fibery.io/t/end-an-automation-partway-through-with-script/5225/3 "2023-09-30T01:34:21Z")

</div>

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.

```auto
// 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;
    }
}

```

 ![Screenshot 2023-09-29 at 6.32.52 PM](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/2/280687592cfb479de489092d14e9fa84f70ed555.png)

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 30, 2023, 2:46pm UTC](https://community.fibery.io/t/end-an-automation-partway-through-with-script/5225/4 "2023-09-30T14:46:56Z")

</div>

> [@Tommy\_Hedley](#):
>
> I don’t think that’s possible without throwing an error

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

> [@Tommy\_Hedley](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![Tommy\_Hedley](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/tommy_hedley/32/6468_2.png) [@Tommy\_Hedley](https://community.fibery.io/u/Tommy_Hedley)\
**Post date:** [October 1, 2023, 12:53am UTC](https://community.fibery.io/t/end-an-automation-partway-through-with-script/5225/5 "2023-10-01T00:53:59Z")

</div>

> [@Chr1sG](#):
>
> 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.

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

```

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [October 1, 2023, 7:37am UTC](https://community.fibery.io/t/end-an-automation-partway-through-with-script/5225/6 "2023-10-01T07:37:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matt\_Blais](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/matt_blais/32/1464_2.png) [@Matt\_Blais](https://community.fibery.io/u/Matt_Blais)\
**Post date:** [October 2, 2023, 2:25pm UTC](https://community.fibery.io/t/end-an-automation-partway-through-with-script/5225/7 "2023-10-02T14:25:07Z")

</div>

> [@Tommy\_Hedley](#):
>
> 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.

Related: [Allow to return an empty value from a formula - #2 by Matt\_Blais](https://community.fibery.io/t/allow-to-return-an-empty-value-from-a-formula/2983/2)
