cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a way to access both before- and after- document metadata during a documentModified handler?

Steven_Huwig1
Star Contributor
Star Contributor

I have a multivalue metadata element on my custom document type. I would like to run code when this information changes, but I need to know which items were added or removed from the list of elements in order to run this code.

Simplified examples:

  • Document contains ['item1'] - user adds 'item2' to this list and clicks Save. My handler runs and sees that the value has changed from ['item1'] to ['item1', 'item2'] for this element. It performs work for the new 'item2'.
  • Document contains ['item1', 'item2'] - user removes 'item1' from this list and clicks Save. My handler runs and sees that the value has changed from ['item1', 'item2'] to ['item2']. It performs work for the old 'item1'.

There must be several ways to implement this, but it is a simple enough concept that I think there must be a "best" way in the general case. Could someone give a general outline or a pointer to existing code/documentation that handles metadata changes on documentModified?

1 ACCEPTED ANSWER

Florent_Guillau
World-Class Innovator
World-Class Innovator

The better answer is to use the previous document that's already provided to you in the event context for a beforeDocumentModification:

DocumentModel oldDocument = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
if (oldDocument != null) {
    // beforeDocumentModification
    title = oldDocument.getTitle();
    // ...
}

View answer in original post

5 REPLIES 5

jmdavid_8121
Champ in-the-making
Champ in-the-making

You can have access to the origninal document model (the one containing old values, not the one being modified) with:

@{Document.getRef().reference()}

Thanks. I am doing this from inside Java code, however. I'm not sure what the equivalent of the above is, but input.getCoreSession().getDocument(input.getRef()) seems to get the same metadata as is on input during documentModified.

Steven_Huwig1
Star Contributor
Star Contributor

The answer to this question is to hook into the "before document modification" and retrieve the document from the repository.

public String run(DocumentModel input) throws ClientException {
    DocumentModel oldDocument = session.getDocument(input.getRef());
    // ...
}

In the above method, "input" contains the new values and "oldDocument" contains the old values.

My original question assumed "documentModified" was the correct event, but it was not.

edit: see the better answer.

Florent_Guillau
World-Class Innovator
World-Class Innovator

The better answer is to use the previous document that's already provided to you in the event context for a beforeDocumentModification:

DocumentModel oldDocument = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
if (oldDocument != null) {
    // beforeDocumentModification
    title = oldDocument.getTitle();
    // ...
}

Thank you! I knew there must have been a better way; I just had no idea how to find it.

Getting started

Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.