Expand my Community achievements bar.

SOLVED

want to write a groovy script to locate the component on a page and change the one of the field value in it

Avatar

Level 5

I want to write a groovy script where I have to change one of the text field value in the component dialog box

 

-> I have the paths of the pages where this component has been used

-> the resourceType of component  is : /components/content/form/embed

->the text field name that need to be changed is : "formName"

->

rahul234dabas_0-1663746298287.png

->the text field value need to be changed using groovy script

 

Please help me out I am stuck on the implementation

 

Thank you

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
def buildQuery() {
    def queryManager = session.workspace.queryManager;
    def statement = "SELECT * FROM [nt:unstructured] AS s WHERE ISDESCENDANTNODE([/content/path/]) AND s.[sling:resourceType]= 'myapp/components/content/form/embed' AND s.[formName] is not NULL"
    queryManager.createQuery(statement, 'sql');
}


final def query = buildQuery();
final def result = query.execute();

result.nodes.each {
    node ->
        node.setProperty('formName', 'newvalue');
    }

session.save();
println 'Number Of Component Found :' + result.nodes.size();


Arun Patidar

View solution in original post

4 Replies

Avatar

Level 5

you can use the component conversion tool from the AEM modernization suit as well.

Here is how to use it:

https://opensource.adobe.com/aem-modernize-tools/pages/component/usage.html

 

you won't need to provide the page paths it will fetch it based on the component's resource type.

 

Avatar

Correct answer by
Community Advisor
def buildQuery() {
    def queryManager = session.workspace.queryManager;
    def statement = "SELECT * FROM [nt:unstructured] AS s WHERE ISDESCENDANTNODE([/content/path/]) AND s.[sling:resourceType]= 'myapp/components/content/form/embed' AND s.[formName] is not NULL"
    queryManager.createQuery(statement, 'sql');
}


final def query = buildQuery();
final def result = query.execute();

result.nodes.each {
    node ->
        node.setProperty('formName', 'newvalue');
    }

session.save();
println 'Number Of Component Found :' + result.nodes.size();


Arun Patidar

Avatar

Level 2

Is there any easy way to update all resourcetype paths for all components in a project?

Example 

myapp/components/content/form/embed
to
myapp2/components/content/form/embed

Avatar

Community Advisor

Hi @gvaem 
With groovy scripts, yes you can write any business logic and peform updates.



Arun Patidar