Update component node using groovy script in aem | Community
Skip to main content
gayatrik8153299
Level 2
September 28, 2020
Solved

Update component node using groovy script in aem

  • September 28, 2020
  • 2 replies
  • 8036 views

Hi All,

We have one requirement need to write groovy script to update component node.

1.need to find the list of pages where tab component been used

tab component has multifield where each field has tab id and tab name and can author upto 5 tabs.

2. Once we get the list of pages, we need to find the component with node name navigationtabs

as shown in the below screenshot.

ex: page /content/we-retail/auotmation-support-program has tab component with node name navigationtabs and under this navTabList (multifield name) node will be there and under this 1,2,3,4,5 with nodes if author added 5 tabs. here only 2 tabs are added hence only 1 and 2 node are created.

if we take node 1 has navId and navName and need to get property navId and like that from each nodes 1,2,3,4,5 if there were 5 nodes.
then get the navId value and it should be same as node created under node navigationtabs.
here in the screenshot can see navId value is "Vendor" and this value should get updated for "By Vendor" node.

 

Please suggest on how to write groovy script to update node.

 

1.find the list of pages used tab component

2.then find the node navigationTabs and then find the navTabsList node under that.

3.then find the nodes 1,2,3,4,5 if exists under navTabsList

4.check if navId has property and get the navId property from each nodes 1,2,3,4,5

5.once we get the navId value, find under navigationTabs node and should be same as navId Value under navigationTabs node else it should be updated  with navId value.

 

we are using AEM 6.4.5 version

Thanks,

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Vijayalakshmi_S

Hi @gayatrik8153299,

For updating node name, we need to make use of move method from Session API - javax.jcr.Session

  • node.getSession().move(scrPath, destPath);

For your specific use case,

Assumption : (based on the screenshot shared)

Node that needs to be updated say "By Vendor" under navigationtabs is the same as navName property value. Have used this value to frame srcPath for move method mentioned above (In other words, node under navigationtabs is changed from navName value to navId value)

Note:

While executing in higher environments, have a content back up and then execute the script (in phases based on the content volume)

2 replies

Varun_Shakya
Community Advisor
Community Advisor
September 28, 2020
Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
September 28, 2020

Hi @gayatrik8153299,

For updating node name, we need to make use of move method from Session API - javax.jcr.Session

  • node.getSession().move(scrPath, destPath);

For your specific use case,

Assumption : (based on the screenshot shared)

Node that needs to be updated say "By Vendor" under navigationtabs is the same as navName property value. Have used this value to frame srcPath for move method mentioned above (In other words, node under navigationtabs is changed from navName value to navId value)

Note:

While executing in higher environments, have a content back up and then execute the script (in phases based on the content volume)

gayatrik8153299
Level 2
September 28, 2020
Thank you Vijayalakshmi_S....I will try this one