Expand my Community achievements bar.

SOLVED

Update component node using groovy script in aem

Avatar

Level 2

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.

 

gayatrik8153299_0-1601282238214.png

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,

 

1 Accepted Solution

Avatar

Correct answer by
Level 10

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)

View solution in original post

3 Replies

Avatar

Correct answer by
Level 10

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)