title: Groovy Script AEM Asset Reference Update
body: <p>So I'm working with AEM and am attempting to create a script that grabs all pages under a specific path and updates the image reference on the page from a list of assets under a curtain path.</p>
  <p>Both of my select query's aren't returning the specific pages and assets I need.</p>
  <p>I'm also getting an error that my queries are searching over 100000 Nodes</p>
  <p>How can i resolve this error and query my resources better?</p>
  <pre><code>import com.day.cq.wcm.api.Page
  import javax.jcr.query.Query
  import javax.jcr.query.QueryManager
  import org.apache.sling.api.resource.ModifiableValueMap
  
  import groovy.transform.Field
  
  
  static void main(String[] args) 
  {
  
  String[] assetNodes
  String[] pageNodes
  
  String pagePath =&quot;/content/we-retail/us/en&quot;
  String pageResourceType = &quot;weretail/components/structure/page&quot;
  String assetPath =&quot;/content/dam/microsoft/internal/en&quot;
  String assetQuery = &quot;b1048291-23fa-422a-a7c4-9ea4bae0effc&quot;
  boolean isAsset = true;
  pageNodes = GetResourcePath(pagePath, pageResourceType);
  
  assetNodes = GetRosourceAsset(assetPath, assetQuery);
  
  InputAssetsOnPage(pageNodes,assetNodes);
  } 
  
  
  //Find the Node paths for all Pages to modify 
  //Narrow down to image component
  
  def GetResourcePath(String rootPath,String queryParam)
  {
      int i = 0
     def String[] allNodes = new String[500]
  
      Page page = getPage(rootPath)
  
  def queryManager = session.workspace.queryManager;
  def param= queryParam;
  def statement = 'select * from nt:base where jcr:path like \''+page.path+'/%\' and sling:resourceType = \'' + param + '\'';
  
  Query query=queryManager.createQuery(statement, 'sql');
  
  final def result = query.execute()
  
  println &quot;Total pages found = &quot; + result.nodes.size();
  
  NodeIterator nodeIterator = result.getNodes();
  
  while(nodeIterator.hasNext())
  {
      def hitNode = nodeIterator.nextNode();
      allNodes[i] = hitNode.getPath();
      i++;
  
  }
  println allNodes
  
  return allNodes;
  }
  
  
  //Find all assets paths to add to page 
  def GetRosourceAsset(String rootPath,String queryParam)
  {
     int i = 0
     def String[] allNodes = new String[500]
  
      Page page = getPage(rootPath)
  
  def queryManager = session.workspace.queryManager;
  def param= queryParam;
  def statement = 'select * from nt:base where jcr:path like \''+rootPath+'/%\'';
  Query query=queryManager.createQuery(statement, 'sql');
  
  final def result = query.execute()
  
  println &quot;Total Assets found = &quot; + result.nodes.size();
  
  NodeIterator nodeIterator = result.getNodes();
  
  while(nodeIterator.hasNext())
  {
      def hitNode = nodeIterator.nextNode();
      allNodes[i] = hitNode.getPath();
      i++;
  
  }
  println allNodes
  
  return allNodes;
  }
  
  //Modify image component property with unique asset path 
  void InputAssetsOnPage(String[]pageRefrence, String[]assetRefrences)
  {
  String[] nodes= pageRefrence;
  String[] assetNodes = assetRefrences;
  
  nodes.eachWithIndex { self,i-&gt;
  
      javax.jcr.Node node=getNode(nodes[i])
  
      ModifiableValueMap mVMap=resourceResolver.resolve(node.path).adaptTo(ModifiableValueMap.class);
  
      mVMap.put(&quot;fileRefrence&quot;, assetNodes[i]);
  
      println &quot;Property modified to &quot;+node.path;
  
      println &quot;Dry Run &quot;+data.dryRun;
  
      if(!data.dryRun) {
          session.save();
      }
  }
  
  }
  //Save session
  </code></pre>