Adding Dynamic Attributes in Groovy script issue

I’m running a groovy script inside of a listener. The script I have is setting dynamic attributes. If the attributes already existed (already have values) on the entity then everything works as intended, but if a dynamic attribute is new then it doesn’t get persisted. If I create a new dynamic attribute inside of the listener but not within the script then everything gets persisted as expected.

Thoughts on how I can get around this?

Thanks.

Hi Josh,

Could you provide a small test project so we could quickly prototype a solution?

DynamicAttributesGroovyIssue.zip (149.9 KB)

In this test project it looks like it didn’t matter if it’s in the groovy script or not. I am setting some dynamic attributes in the listener, but they aren’t persisting. They will only persist if I already have values for the dynamic attributes that I am saving.

Also, on another note. I’ve tried this with workflow as well, it’s not persisting dynamic attributes that I set in groovy using Activitii.

Thanks.

Thank you for the test project.

In order to save changes in dynamic attributes made inside of a transaction, invoke the storeDynamicAttributes() method of the DynamicAttributesManagerAPI bean. It is done automatically only when you work with entity outside of transaction and then pass it to DataManager.

So in your entity listener it should look like this:

@Component("dynamicattributesgroovyissue_TicketListener")
public class TicketListener implements BeforeInsertEntityListener<Ticket>, BeforeUpdateEntityListener<Ticket> {
    @Inject
    private DynamicAttributesManagerAPI dynamicAttributesManager;
    // ...
    private void Common(Ticket entity, EntityManager entityManager) {
    //...
        entity.setValue("+Violation:Value2", "Populated 2");                    // Thought this worked but it doesn't.
        dynamicAttributesManager.storeDynamicAttributes(entity);
    }
}

I’m running into the issue where if I set a dynamic attribute in a groovy script and call merge on the entity inside of an Activitii workflow, that I lose the attribute. When the listener is launched, no dynamic attributes are loaded on the entity (I just created another issue regarding this here). My question is do I need to explicitly save my dynamic attributes from the workflow groovy script? I need to detect if a dynamic attribute has been modified and having it already persisted before entering the listener will make that a lot harder than it needs to be. Thoughts?

Groovy scripts of Activiti workflows are run in the opened transaction, and as Konstantin already mentioned, the only way to save dynamic attributes in the transaction is to use the storeDynamicAttributes() method of the DynamicAttributesManagerAPI bean.
If I understood you correctly and your question is about how to find which dynamic attributes were modified, you may try to do the following:

  1. fetch dynamic attributes in the current transaction (this will give you updated attributes map)
  2. then fetch dynamic attributes in the NEW transaction (that will read the previous values of dynamic attributes), 3. compare two dynamic attributes maps.