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
New Participant
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
  • 1916 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

New Participant
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
New Participant
March 20, 2024

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

Arun Patidar
arunpatidar
arunpatidarAccepted solution
New Participant
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
New Participant
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.