setProperty not working as expected

Avatar

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile
mark_julianp181
Level 2

28-09-2017

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?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,161 posts

Correct reply

1,079 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,161 posts

Correct reply

1,079 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

28-09-2017

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

Answers (5)

Answers (5)

Avatar

Avatar
Ignite 1
Level 1
muthuprasanthm4
Level 1

Likes

2 likes

Total Posts

6 posts

Correct reply

0 solutions
Top badges earned
Ignite 1
Give Back
Boost 1
Applaud 5
View profile

Avatar
Ignite 1
Level 1
muthuprasanthm4
Level 1

Likes

2 likes

Total Posts

6 posts

Correct reply

0 solutions
Top badges earned
Ignite 1
Give Back
Boost 1
Applaud 5
View profile
muthuprasanthm4
Level 1

05-06-2019

Thanks @joerg

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,161 posts

Correct reply

1,079 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,161 posts

Correct reply

1,079 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

29-09-2017

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

Avatar

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile
mark_julianp181
Level 2

28-09-2017

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

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

28-09-2017

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

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

28-09-2017

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.

.