Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

setProperty not working as expected

Avatar

Level 2

Hi,

I have a process step in a workflow that launches when a content fragment is created. It calls a custom process with the following code:

public void execute(WorkItem item, WorkflowSession session, MetaDataMap args) throws WorkflowException {

try {

Map<String, Object> serviceParams = new HashMap<String, Object>();

serviceParams.put(ResourceResolverFactory.SUBSERVICE, "test");

   ResourceResolver resourceResolver;

resourceResolver = resolverFactory.getServiceResourceResolver(serviceParams);

Node node = resourceResolver.getResource(item.getContentPath()).adaptTo(Node.class);

Node tempNode = node.getNode("/jcr:content");

tempNode.setProperty("activated", "false");

but the setProperty is not adding the property to the jcr:content as expected. Is there something I am missing or doing wrong?

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi,

First of all, why do you create a new session to perform this operation? Is there anything special, that you need to execute this as a special user?

You could use something like

Session mySession = workflowSession.getSession();

to get the session the workflow is using itself. Then you don't need to take care of the livecycle of the resourceResolver/JCR session at all. In you example you'll have a session leak, which will eventually lead to an OutOfMemory situation.

Then it's not enough just to set the property, you need to save the session as well.

session.save();

Jörg

View solution in original post

6 Replies

Avatar

Level 10

Looks like you are using a system user for this task.

When you created a system users - make sure you did the following:

1. Provide permissions so this user can access the applicable location in the JCR.

2. You have correctly Mapped the System user using Felix console.

See this article as a reference -- Scott's Digital Community: Querying Adobe Experience Manager 6 data using the Sling getServiceResour...

There is a video too you can watch that shows you all steps.

.

Avatar

Correct answer by
Employee Advisor

Hi,

First of all, why do you create a new session to perform this operation? Is there anything special, that you need to execute this as a special user?

You could use something like

Session mySession = workflowSession.getSession();

to get the session the workflow is using itself. Then you don't need to take care of the livecycle of the resourceResolver/JCR session at all. In you example you'll have a session leak, which will eventually lead to an OutOfMemory situation.

Then it's not enough just to set the property, you need to save the session as well.

session.save();

Jörg

Avatar

Level 10

Joerg makes a great point. When working with AEM JCR sessions - always close them:

// Save the session changes and log out

  session.save();

  session.logout();

CQ development patterns – Sling ResourceResolver and JCR sessions | Things on a content management s...

Avatar

Level 2

Jörg,

Thank you, changed my code to the following:

Session session = workflowSession.adaptTo(Session.class);

Node node = session.getNode(item.getContentPath()+"/jcr:content");

node.setProperty("activated", "false");

session.save();

session.logout();

Avatar

Employee Advisor

Remove the "session.logout();" statement, as it will cause problems in the workflow.

Here you violate the 1st rule of session handling: "You open the session, you close the session! If you don't open the session, don't close the session".

Jörg