# Cancel creation of element

**URL:** <https://forum.bpmn.io/t/cancel-creation-of-element/7154>\
**Category:** General\
**Created:** [January 27, 2022, 10:15pm UTC](https://forum.bpmn.io/t/cancel-creation-of-element/7154 "2022-01-27T22:15:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjay](https://forum.bpmn.io/letter_avatar_proxy/v4/letter/k/d6d6ee/32.png) [@kjay](https://forum.bpmn.io/u/kjay)\
**Post date:** [January 27, 2022, 10:15pm UTC](https://forum.bpmn.io/t/cancel-creation-of-element/7154/1 "2022-01-27T22:15:55Z")

</div>

Hi,

I’m selecting an element from the palette and then creating it on the Canvas. I’ve created a CommandInterceptor on preExecute to prompt the user for a name, that will be assigned to this element.

If the user enters the name, element will be created. If the user presses "cancel’ on the dialog box, how do I abort the creation of element?

Thanks,  
Jay.

---

<div class="post-metadata">

**Author:** ![Niklas\_Kiefer](https://forum.bpmn.io/user_avatar/forum.bpmn.io/niklas_kiefer/32/3882_2.png) [@Niklas\_Kiefer](https://forum.bpmn.io/u/Niklas_Kiefer)\
**Post date:** [January 28, 2022, 8:35am UTC](https://forum.bpmn.io/t/cancel-creation-of-element/7154/2 "2022-01-28T08:35:21Z")

</div>

> [@kjay](#):
>
> I’ve created a CommandInterceptor on preExecute to prompt the user for a name, that will be assigned to this element.

Are you able to share your approach via [CodeSandbox](https://codesandbox.io/s/bpmn-js-sandbox-3zkw0)? This way it will be easier for us to help you.

---

<div class="post-metadata">

**Author:** ![kjay](https://forum.bpmn.io/letter_avatar_proxy/v4/letter/k/d6d6ee/32.png) [@kjay](https://forum.bpmn.io/u/kjay)\
**Post date:** [January 28, 2022, 1:56pm UTC](https://forum.bpmn.io/t/cancel-creation-of-element/7154/3 "2022-01-28T13:56:13Z")

</div>

I’m quite new to this - I’ve created the sandbox environment but it doesn’t run on it. However, the code is all there.

> **[diagram-js - CodeSandbox](https://codesandbox.io/s/diagram-js-oqbwc?file=%2Fsrc%2Ffeatures%2Fcommands%2FCreateInterceptor.js)**
>
> diagram-js by k-jay-c using diagram-js

If you see the code at line 19 - how do I stop the creation of element on canvas if the user hasn’t entered a name in the dialog box?

Thanks.

---

<div class="post-metadata">

**Author:** ![philippfromme](https://forum.bpmn.io/user_avatar/forum.bpmn.io/philippfromme/32/2803_2.png) [@philippfromme](https://forum.bpmn.io/u/philippfromme)\
**Post date:** [February 3, 2022, 9:08am UTC](https://forum.bpmn.io/t/cancel-creation-of-element/7154/4 "2022-02-03T09:08:27Z")

</div>

Hey,

you can hook into the `create.end` event with high priority hook in before the element is created by [Create](https://github.com/bpmn-io/diagram-js/blob/v8.1.0/lib/features/create/Create.js#L185).

```javascript
eventBus.on("create.end", HIGH_PRIORITY, ({ context }) => {
  const { elements, hints, shape } = context;

  if (elements && elements.length > 1) {
    // more than one element created
    return;
  }

  const name = prompt("Please enter a name");

  if (name) {
    shape.businessObject.set("name", name);

    // disable direct editing
    hints.createElementsBehavior = false;

    return;
  }

  // do not create element
  return false;
});

```

Returning `false` from the event listener will prevent any other event listeners to be called afterwards. This effectively cancels creating the element.

Working example: [Set Name on Create Example - CodeSandbox](https://codesandbox.io/s/set-name-on-create-example-l34sv?file=/src/index.js)

---

<div class="post-metadata">

**Author:** ![nikku](https://forum.bpmn.io/user_avatar/forum.bpmn.io/nikku/32/5130_2.png) [@nikku](https://forum.bpmn.io/u/nikku)\
**Post date:** [July 22, 2025, 3:41pm UTC](https://forum.bpmn.io/t/cancel-creation-of-element/7154/5 "2025-07-22T15:41:51Z")

</div>

Closed on July 22, 2025, 3:41pm UTC


