Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Node values not getting updated in crx and also in UI

Avatar

Level 1

Hello ,

I have a requirement where io need to pre-populate node 1 values into Node2 thru path field selection from node1 in dialog .

AJAX  : Am using Ajax Call to send two node values ---

function(){

var componentDialog = this.findParentByType('dialog');

var currComponentPath = componentDialog.path;

var selectedPath = this.value;

$.ajax({

type: 'POST',

url: "/bin/portal/jsonservices",

dataType: "text json",

data:'SelectedPath='+ selectedPath+'&CurrentCompPath='+ currComponentPath,

cache: false,

success: AjaxSucceeded,

error: AjaxFailed

});

function AjaxSucceeded(result) {

alert("success");

alert(result.status + ' ====' + result.statusText +' ===== '+selectedPath + '=====' +currComponentPath);

}

function AjaxFailed(result) {

alert("fail");

alert(result.status + ' ====' + result.statusText +' ===== '+selectedPath + '=====' +currComponentPath);

}

}

Servlet  : Values are getting passed to servlet perfectly fine ....

@Component

@Service

@Properties({

@Property(name="service.description", value="Updates Nodes"),

@Property(name="service.vendor", value="3|SHARE"),

@Property(name="sling.servlet.paths",value="/bin/portal/jsonservices"),

@Property(name="sling.servlet.extensions",value="json"),

@Property(name = "sling.servlet.methods", value = {"POST"})})

public class GetNodeDetails extends org.apache.sling.api.servlets.SlingAllMethodsServlet   {

private static final long serialVersionUID = 1L;

private static final Logger logger = LoggerFactory.getLogger(GetNodeDetails.class);

@Override

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException

{

response.setHeader("Content-Type", "application/json");

try {

response.setContentType("application/json");

// ResourceResolver resolver = request.getResourceResolver();

Session session = request.getResourceResolver().adaptTo(Session.class);

String selectedPath = request.getParameter("SelectedPath");

String pagePath = request.getParameter("CurrentCompPath");

Node node = session.getNode(selectedPath);

Node node1 = session.getNode(pagePath);

Value vs[]= node.getProperty("rows").getValues();

String country = node.getProperty("country").getString();

String segment = node.getProperty("segment").getString();

String name = node.getProperty("name").getString();

if(vs!=null){

logger.info("== IN IF LOOP ==");

node1.setProperty("rows",vs);

}

node1.setProperty("country",country);

node1.setProperty("segment",segment);

node1.setProperty("name",name);

session.save();

response.setStatus(SlingHttpServletResponse.SC_OK);

}catch (Exception e) {

logger.error(" Exception", e);

}

7 Replies

Avatar

Level 1

Selected Path  ====/content/portal/brands/wst/retail-sales/jcr:content/basicParagraph/retailsales_192239199

Component Page  Path  ====/content/portal/brands/wst/retail-sales/jcr:content/basicParagraph/retailsales_354302026

Node data Rows  ====Property[PropertyDelegate{parent=/content/portal/brands/wst/retail-sales/jcr:content/basicParagraph/retailsales_192239199: { jcr:primaryType = nt:unstructured, jcr:createdBy = admin, jcr:lastModifiedBy = admin, existingtable = /content/portal/brands/wst/retail-sales/jcr:content/basicParagraph/retailsales_0, jcr:created = 2018-02-14T17:09:49.661+05:30, name = Top 5 Retail Sales - December 2017 - CAN, rows = [{"dealershipName":"TARDIF DIESEL INC.","numberofUnits":"49","salesRepName":"JEAN-MARC TARDIF"}, {"dealershipName":"CENTRE DU CAMION MONT-LAURIER (2009) INC.","numberofUnits":"11","salesRepName":"PASCAL LARIVIERE"}, {"dealershipName":"TEAM TRUCK CENTRES (LONDON) LIMITED","numberofUnits":"10","salesRepName":"TIM RYAN"}, {"dealershipName":"RJAMES WESTERN STAR ALBERTA","numberofUnits":"9","salesRepName":"GARY GOLDTHORPE"}, {"dealershipName":"EASTGATE TRUCK CENTRE","numberofUnits":"9","salesRepName":"PETER HORN"}, {"dealershipName":"NORTHLAND TRUCK CENTRE","numberofUnits":"8","salesRepName":"BLAKE CLARKE"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"MURRAY MYKYTIW"}, {"dealershipName":"EXPERT GARAGE LTD.","numberofUnits":"8","salesRepName":"REJEAN GOULET"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"WAYNE OSTBY"}], country = INDIA, jcr:lastModified = 2018-02-14T19:08:23.576+05:30, sling:resourceType = portal/components/modular/retailSales, segment = All}, property=rows = [{"dealershipName":"TARDIF DIESEL INC.","numberofUnits":"49","salesRepName":"JEAN-MARC TARDIF"}, {"dealershipName":"CENTRE DU CAMION MONT-LAURIER (2009) INC.","numberofUnits":"11","salesRepName":"PASCAL LARIVIERE"}, {"dealershipName":"TEAM TRUCK CENTRES (LONDON) LIMITED","numberofUnits":"10","salesRepName":"TIM RYAN"}, {"dealershipName":"RJAMES WESTERN STAR ALBERTA","numberofUnits":"9","salesRepName":"GARY GOLDTHORPE"}, {"dealershipName":"EASTGATE TRUCK CENTRE","numberofUnits":"9","salesRepName":"PETER HORN"}, {"dealershipName":"NORTHLAND TRUCK CENTRE","numberofUnits":"8","salesRepName":"BLAKE CLARKE"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"MURRAY MYKYTIW"}, {"dealershipName":"EXPERT GARAGE LTD.","numberofUnits":"8","salesRepName":"REJEAN GOULET"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"WAYNE OSTBY"}]}]

Node GET Country ====INDIA

Node GET Segment ====All

Node GET Name  ====Top 5 Retail Sales - December 2017 - CAN

== IN IF LOOP ==

Node1 SET Rows Before Save ====Property[PropertyDelegate{parent=/content/portal/brands/wst/retail-sales/jcr:content/basicParagraph/retailsales_354302026: { jcr:primaryType = nt:unstructured, jcr:createdBy = admin, jcr:lastModifiedBy = admin, existingtable = /content/portal/brands/wst/retail-sales/jcr:content/basicParagraph/retailsales_0, jcr:created = 2018-02-14T17:16:26.715+05:30, jcr:lastModified = 2018-02-16T02:46:53.701+05:30, sling:resourceType = portal/components/modular/retailSales, name = Top 5 Retail Sales - December 2017 - CAN, country = INDIA, rows = [{"dealershipName":"TARDIF DIESEL INC.","numberofUnits":"49","salesRepName":"JEAN-MARC TARDIF"}, {"dealershipName":"CENTRE DU CAMION MONT-LAURIER (2009) INC.","numberofUnits":"11","salesRepName":"PASCAL LARIVIERE"}, {"dealershipName":"TEAM TRUCK CENTRES (LONDON) LIMITED","numberofUnits":"10","salesRepName":"TIM RYAN"}, {"dealershipName":"RJAMES WESTERN STAR ALBERTA","numberofUnits":"9","salesRepName":"GARY GOLDTHORPE"}, {"dealershipName":"EASTGATE TRUCK CENTRE","numberofUnits":"9","salesRepName":"PETER HORN"}, {"dealershipName":"NORTHLAND TRUCK CENTRE","numberofUnits":"8","salesRepName":"BLAKE CLARKE"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"MURRAY MYKYTIW"}, {"dealershipName":"EXPERT GARAGE LTD.","numberofUnits":"8","salesRepName":"REJEAN GOULET"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"WAYNE OSTBY"}], segment = All}, property=rows = [{"dealershipName":"TARDIF DIESEL INC.","numberofUnits":"49","salesRepName":"JEAN-MARC TARDIF"}, {"dealershipName":"CENTRE DU CAMION MONT-LAURIER (2009) INC.","numberofUnits":"11","salesRepName":"PASCAL LARIVIERE"}, {"dealershipName":"TEAM TRUCK CENTRES (LONDON) LIMITED","numberofUnits":"10","salesRepName":"TIM RYAN"}, {"dealershipName":"RJAMES WESTERN STAR ALBERTA","numberofUnits":"9","salesRepName":"GARY GOLDTHORPE"}, {"dealershipName":"EASTGATE TRUCK CENTRE","numberofUnits":"9","salesRepName":"PETER HORN"}, {"dealershipName":"NORTHLAND TRUCK CENTRE","numberofUnits":"8","salesRepName":"BLAKE CLARKE"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"MURRAY MYKYTIW"}, {"dealershipName":"EXPERT GARAGE LTD.","numberofUnits":"8","salesRepName":"REJEAN GOULET"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"WAYNE OSTBY"}]}]

Node1 SET Country Before Save ====INDIA

Node1 SET Segment Before Save ====All

Node1 SET Name Before Save====Top 5 Retail Sales - December 2017 - CAN

In FOR Loop GET Row Values  ===={"dealershipName":"TARDIF DIESEL INC.","numberofUnits":"49","salesRepName":"JEAN-MARC TARDIF"}

In FOR Loop GET Row Values  ===={"dealershipName":"CENTRE DU CAMION MONT-LAURIER (2009) INC.","numberofUnits":"11","salesRepName":"PASCAL LARIVIERE"}

In FOR Loop GET Row Values  ===={"dealershipName":"TEAM TRUCK CENTRES (LONDON) LIMITED","numberofUnits":"10","salesRepName":"TIM RYAN"}

In FOR Loop GET Row Values  ===={"dealershipName":"RJAMES WESTERN STAR ALBERTA","numberofUnits":"9","salesRepName":"GARY GOLDTHORPE"}

In FOR Loop GET Row Values  ===={"dealershipName":"EASTGATE TRUCK CENTRE","numberofUnits":"9","salesRepName":"PETER HORN"}

In FOR Loop GET Row Values  ===={"dealershipName":"NORTHLAND TRUCK CENTRE","numberofUnits":"8","salesRepName":"BLAKE CLARKE"}

In FOR Loop GET Row Values  ===={"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"MURRAY MYKYTIW"}

In FOR Loop GET Row Values  ===={"dealershipName":"EXPERT GARAGE LTD.","numberofUnits":"8","salesRepName":"REJEAN GOULET"}

In FOR Loop GET Row Values  ===={"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"WAYNE OSTBY"}

Node1 SET Rows After Save ====Property[PropertyDelegate{parent=/content/portal/brands/wst/retail-sales/jcr:content/basicParagraph/retailsales_354302026: { jcr:primaryType = nt:unstructured, jcr:createdBy = admin, jcr:lastModifiedBy = admin, existingtable = /content/portal/brands/wst/retail-sales/jcr:content/basicParagraph/retailsales_0, jcr:created = 2018-02-14T17:16:26.715+05:30, name = Top 5 Retail Sales - December 2017 - CAN, rows = [{"dealershipName":"TARDIF DIESEL INC.","numberofUnits":"49","salesRepName":"JEAN-MARC TARDIF"}, {"dealershipName":"CENTRE DU CAMION MONT-LAURIER (2009) INC.","numberofUnits":"11","salesRepName":"PASCAL LARIVIERE"}, {"dealershipName":"TEAM TRUCK CENTRES (LONDON) LIMITED","numberofUnits":"10","salesRepName":"TIM RYAN"}, {"dealershipName":"RJAMES WESTERN STAR ALBERTA","numberofUnits":"9","salesRepName":"GARY GOLDTHORPE"}, {"dealershipName":"EASTGATE TRUCK CENTRE","numberofUnits":"9","salesRepName":"PETER HORN"}, {"dealershipName":"NORTHLAND TRUCK CENTRE","numberofUnits":"8","salesRepName":"BLAKE CLARKE"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"MURRAY MYKYTIW"}, {"dealershipName":"EXPERT GARAGE LTD.","numberofUnits":"8","salesRepName":"REJEAN GOULET"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"WAYNE OSTBY"}], country = INDIA, jcr:lastModified = 2018-02-16T02:46:53.701+05:30, sling:resourceType = portal/components/modular/retailSales, segment = All}, property=rows = [{"dealershipName":"TARDIF DIESEL INC.","numberofUnits":"49","salesRepName":"JEAN-MARC TARDIF"}, {"dealershipName":"CENTRE DU CAMION MONT-LAURIER (2009) INC.","numberofUnits":"11","salesRepName":"PASCAL LARIVIERE"}, {"dealershipName":"TEAM TRUCK CENTRES (LONDON) LIMITED","numberofUnits":"10","salesRepName":"TIM RYAN"}, {"dealershipName":"RJAMES WESTERN STAR ALBERTA","numberofUnits":"9","salesRepName":"GARY GOLDTHORPE"}, {"dealershipName":"EASTGATE TRUCK CENTRE","numberofUnits":"9","salesRepName":"PETER HORN"}, {"dealershipName":"NORTHLAND TRUCK CENTRE","numberofUnits":"8","salesRepName":"BLAKE CLARKE"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"MURRAY MYKYTIW"}, {"dealershipName":"EXPERT GARAGE LTD.","numberofUnits":"8","salesRepName":"REJEAN GOULET"}, {"dealershipName":"DUNLOP WESTERN STAR TRUCK CENTRE LTD.","numberofUnits":"8","salesRepName":"WAYNE OSTBY"}]}]

Node1 SET Country After Save ====INDIA

Node1 SET Segment After Save ====All

Node1 SET Name After Save ====Top 5 Retail Sales - December 2017 - CAN

==============================SERVER CALL ENDS======================================

Avatar

Level 1

I can see node values getting updated in server logs without any exceptions but the same fails in crx and on UI

Avatar

Level 9

I tried your code and its working for me.

Avatar

Level 10

I am going to test this code too and see if i encounter an issue,

Avatar

Level 9

I just tested the servlet only not the whole Ajax and catcher repository exception

Regards

Hemant

Get Outlook for iOS<https://aka.ms/o0ukef>

Avatar

Level 10

I wanted to get the Servlet to update a node in the JCR. I modified the code a bit.

I simply hard coded a value to pass in that reflects a node to update using the NODE API.

Here is my Java code --

package com.aem.community.core.servlets;

import org.apache.felix.scr.annotations.sling.SlingServlet;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import javax.servlet.ServletException;

import java.io.IOException;

import javax.jcr.Session;

import javax.jcr.Value;

import javax.jcr.Node;

import org.apache.felix.scr.annotations.sling.SlingServlet;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

* Servlet that writes some sample content into the response. It is mounted for

* all resources of a specific Sling resource type. The

* {@link SlingSafeMethodsServlet} shall be used for HTTP methods that are

* idempotent. For write operations use the {@link SlingAllMethodsServlet}.

*/

@SlingServlet(paths="/bin/jsonservices", methods = "POST", metatype=true)

public class SimpleServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet {

private static final long serialVersionUID = 1L;

private static final Logger logger = LoggerFactory.getLogger(SimpleServlet.class);

@Override

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException

{

try {

// ResourceResolver resolver = request.getResourceResolver();

Session session = request.getResourceResolver().adaptTo(Session.class);

String selectedPath = request.getParameter("SelectedPath");

String pagePath = request.getParameter("CurrentCompPath");

Node node = session.getNode(selectedPath);

//Node node1 = session.getNode(pagePath);

//Value vs[]= node.getProperty("rows").getValues();

String country = "USA";

String segment = "Test";

String name = "Scott2";

//write the values to the node in the servlet

node.setProperty("country",country);

node.setProperty("segment",segment);

node.setProperty("name",name);

session.save();

response.getWriter().write("Success");

}catch (Exception e) {

logger.error(" Exception", e);

}

}

}

As you can see - i kept is cleaner in terms of updating a node.

Here is the AJAX -

<script>

$(document).ready(function() {

    $('#submit').click( function() {

var componentDialog = "/content/node1";

var currComponentPath = "/content/JCRWrite/en";

var selectedPath = "/content/node1";

//Use JQuery AJAX request to post data to a Sling Servlet

    $.ajax({

         type: 'POST',   

         url:'/bin/jsonservices',

data:'SelectedPath='+ selectedPath+'&CurrentCompPath='+ currComponentPath,

        success: function(msg){

          alert (msg) ; 

         }

     });

  

  });  //end of click

}); // end ready

</script>

This Servlet did make an update the JCR node via the JCR APINode1.png: