Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

setProperty not working as expected

mark_julianp181
Level 2
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
Jörg_Hoh
Correct answer by
Employee
Employee

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
smacdonald2008
Level 10
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.

.

Jörg_Hoh
Correct answer by
Employee
Employee

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

smacdonald2008
Level 10
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...

mark_julianp181
Level 2
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();

Jörg_Hoh
Employee
Employee

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