Is it possible to create a task using javascript?

Hello,
I am a developer and I work on a BPMN editor using bpmn.js.
I would like to create a task glossary, but for that I need to be able to create tasks using javascript.
I would like to know if the library allows you to create tasks using code or I have to modify the XML directly.
My goal would be that the user fills in a glossary and that he can add the tasks he selects in a diamgram.
Sorry for my approximate level of English.
thank you in advance.

Hi Hugo,

the ElementFactory is your friend here. Please refer to this example for guidance.

Thank you so much !
To add a name to a task I use this:
var modeling = modeler.get('modeling');
modeling.updateProperties(task, {
name: 'New name'
});
Is this a good practice ?

Hello,
I noticed an error, when I delete the task I just created I have an error.
erreurbpmn
If I move the task before removing it all is well.
Do I have to fill in the task somewhere after creating it?
I noticed that the xml does not update when I create the task.
It only updates when I move it.

The best entry into adding elements programatically is likely the modeling API you’ve already found.

const modeling = bpmnEditor.get('modeling');
const elementRegistry = bpmnEditor.get('elementRegistry');

const parentShape = elementRegistry.get('Process_1');

const taskShape = modeling.createShape(
  { type: 'bpmn:Task' }, 
  { x: 10, y: 20 }, 
  parentShape
);

Once you’ve created a task via that API you’ll also be able to update it afterwards.

If you create tasks using the lower level APIs (i.e. Canvas#addShape) strange things will happen, unless you know, what you’re doing.

Under the hood modeling uses the elementFactory to create elements, however more things are happening behind the scenes.

It seems to be working well, thanks !
For the line parentShape = elementRegistry.get('Process_1'); Is there a function to retrieve the name of BPMN ?
I created one but it looks for it in XML and I don’t think it’s the best solution.
I do the same to change the name.