want to write a groovy script to locate the component on a page and change the one of the field value in it | Community
Skip to main content
Level 4
September 21, 2022
Solved

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

  • September 21, 2022
  • 3 replies
  • 1920 views

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"

->

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

 

Please help me out I am stuck on the implementation

 

Thank you

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 arunpatidar
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();

3 replies

Level 4
September 21, 2022

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.

 

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
September 21, 2022
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
Level 3
March 19, 2024

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
arunpatidar
Community Advisor
Community Advisor
March 20, 2024

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

Arun Patidar