create business object when taking a task bonita

1
0
-1

Hello,

I would like to create an object in the database when the user takes a task instead of doing it in the operation(when the user submits the form).

how could this be done?

i tried doing it in a groovy script at the connectors in level as following :

Plan logic :

- if entry exists, return the id,

- if doesn't exist, create it and return the id

Code:

Entry entry = null;
List list = entryDAO.findByDocumentId(document.persistenceId);
if(list.size() > 0){
    entry = list.get(0);
} else {
    entry = new Entry();
    entry.setCreationDate(OffsetDateTime.now());
}
return entry;

but it returns an error saying :

27-Jul-2020 15:09:30.102 SEVERE [ConnectorExecutor-31] org.bonitasoft.engine.log.technical.TechnicalLoggerSLF4JImpl.log THREAD_ID=799 | HOSTNAME=mike-hp-envy-notebook | TENANT_ID=1 | The work [ExecuteConnectorOfActivity: flowNodeInstanceId = 80067, connectorDefinitionName = createEntry] failed. The failure will be handled.
27-Jul-2020 15:09:30.107 SEVERE [ConnectorExecutor-31] org.bonitasoft.engine.log.technical.TechnicalLoggerSLF4JImpl.log THREAD_ID=799 | HOSTNAME=mike-hp-envy-notebook | TENANT_ID=1 | org.bonitasoft.engine.commons.exceptions.SBonitaRuntimeException : "org.bonitasoft.engine.commons.exceptions.SBonitaRuntimeException: java.lang.IllegalStateException: org.bonitasoft.engine.transaction.STransactionNotFoundException: No active transaction."
org.bonitasoft.engine.commons.exceptions.SBonitaRuntimeException: org.bonitasoft.engine.commons.exceptions.SBonitaRuntimeException: java.lang.IllegalStateException: org.bonitasoft.engine.transaction.STransactionNotFoundException: No active transaction.
at org.bonitasoft.engine.connector.impl.ConnectorExecutorImpl.lambda$execute$0(ConnectorExecutorImpl.java:159)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: org.bonitasoft.engine.commons.exceptions.SBonitaRuntimeException: java.lang.IllegalStateException: org.bonitasoft.engine.transaction.STransactionNotFoundException: No active transaction.
at com.centralInspection.registry.server.SejelEntryDAOImpl.findBySejelDocumentAndPrefix(SejelEntryDAOImpl.java:140)
at com.centralInspection.registry.SejelEntryDAO$findBySejelDocumentAndPrefix.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136)
at Script1.run(Script1.groovy:15)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:574)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:612)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:583)
at org.bonitasoft.connectors.scripting.GroovyScriptConnector.executeBusinessLogic(GroovyScriptConnector.java:48)
at org.bonitasoft.engine.connector.AbstractConnector.execute(AbstractConnector.java:77)
at org.bonitasoft.engine.core.connector.impl.SConnectorAdapter.execute(SConnectorAdapter.java:74)
at org.bonitasoft.engine.connector.impl.ConnectorExecutorImpl$ExecuteConnectorCallable.call(ConnectorExecutorImpl.java:258)
at org.bonitasoft.engine.connector.impl.ConnectorExecutorImpl$ExecuteConnectorCallable.call(ConnectorExecutorImpl.java:217)
at org.bonitasoft.engine.connector.impl.ConnectorExecutorImpl.lambda$wrapForStats$1(ConnectorExecutorImpl.java:169)
at org.bonitasoft.engine.connector.impl.ConnectorExecutorImpl.lambda$execute$0(ConnectorExecutorImpl.java:156)
... 4 more
Caused by: java.lang.IllegalStateException: org.bonitasoft.engine.transaction.STransactionNotFoundException: No active transaction.
at org.bonitasoft.engine.business.data.impl.JPABusinessDataRepositoryImpl.getEntityManager(JPABusinessDataRepositoryImpl.java:182)
at org.bonitasoft.engine.business.data.impl.JPABusinessDataRepositoryImpl.findByNamedQuery(JPABusinessDataRepositoryImpl.java:308)
at com.centralInspection.registry.server.SejelEntryDAOImpl.findBySejelDocumentAndPrefix(SejelEntryDAOImpl.java:138)
... 19 more
Caused by: org.bonitasoft.engine.transaction.STransactionNotFoundException: No active transaction.
at org.bonitasoft.engine.transaction.JTATransactionServiceImpl.registerBonitaSynchronization(JTATransactionServiceImpl.java:261)
at org.bonitasoft.engine.business.data.impl.JPABusinessDataRepositoryImpl.getEntityManager(JPABusinessDataRepositoryImpl.java:180)
... 21 more

Comments

Submitted by romain.bioteau on Thu, 08/27/2020 - 18:15

Is it a good idea to create data at this time of the lifecycle ? What happens to that data if the user is unassigned of this tasks ?

Can you share what is your specific use case ?

1 answer

1
0
-1

Hello,

The answer is very simple: you should not update a BDM (or create a new one) INSIDE a connector. Only an operation can do that (and you have more operations place than you think: see below).

Understand the concept: a connector is a "sandbox". And a groovy connector is a connector. In the groovy connector (and in any connector call to be clear), you must not change anything in the content. The principle is very clear: keep in mind a connector can be reusable in different processes, so it must not manipulate anything from a process, that's why you have INPUT and OUTPUT in your connector. Only the OUTPUT updates something in the process.

So, your situation, the connector should produce an output, let's say a MAP which is the BDM to create. if (result.get("bdm") is null, you have to create a new one

Map entry = new HashMap<>();
List list = entryDAO.findByDocumentId(document.persistenceId);
if(list.size() > 0){
    entry.put("bdm", list.get(0));
} else {
    entry.put("creationdate", OffsetDateTime.now());
}
return entry;

Ok, your connector just accesses in READ ONLY.

Now, in the OUTPUT of the connector wizard, you have... operations!

Add an operation

Left operand: a BDM Variable

Right operand, the script

if (result.get("bmd") != null) {

return result.get("bdm");

}

entry = new Entry();
entry.setCreationDate( (OffsetDateTime) result.get("creationDate"));

return entry

Hope this is clear

Notifications