Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Passing values from one form to another

Avatar

Level 6

I need to present a Form B to the user after he submits Form A.  Form B needs to be prefilled with data based on values entered by the user in Form A.  I know how to set up a custom submit service.  I know how to specify Form B as the redirect for Form A so that it appears after a successful submit.  I also know how to set up a custom prefill for Form B.  What I can't seem to do is pass anything from Form A's submit service to the prefill service for Form B.  I must be missing something obvious, but I've searched high and low with no success.

In the prefill service I have access to DataXMLOptions.  From there I can get a user ID, but that's not useful because these may be anonymous users.  Is there anything that I can set in my submit service that I can access through DataXMLOptions (e.g. a session ID)?  If so, I could stage what I need in a file or a database during the submit service and retrieve it again in the prefill service.  I just don't see any way of doing that.

Any help is greatly appreciated.  Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 3

You will need to create a custom submit action for your form. In your post.POST.jsp you'll need to get the data from your form and then handle the redirect. There are numerous ways to do this but here are a few sample lines:

Get the XML from the submitted form:

String formXML = request.getParameter("jcr:data");

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder;

builder = factory.newDocumentBuilder();

org.w3c.dom.Document doc = builder.parse( new InputSource( new StringReader( formXML ) ) );

Extract some specific data:

XPath xPath = XPathFactory.newInstance().newXPath();

String someID = xPath.compile("//SomeID").evaluate(doc);

Prepare and do some redirection (other code included that I thought would be useful that I had to dig around for too):

final String nodePath = properties.get("nodePath", null);

final StringBuilder path = new StringBuilder(request.getContextPath());

final String programCoursesURL = "/content/some-project/someForm.html?RecordID=" + someID;

path.append(programCoursesURL);

response.sendRedirect(path.toString());

In this case the form had a template that read the recordID parameter and pre-filled from a DB using the RecordID. You can also use a "dataRef" service too. The redirect code was a tough piece to get over though.

View solution in original post

7 Replies

Avatar

Correct answer by
Level 3

You will need to create a custom submit action for your form. In your post.POST.jsp you'll need to get the data from your form and then handle the redirect. There are numerous ways to do this but here are a few sample lines:

Get the XML from the submitted form:

String formXML = request.getParameter("jcr:data");

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder;

builder = factory.newDocumentBuilder();

org.w3c.dom.Document doc = builder.parse( new InputSource( new StringReader( formXML ) ) );

Extract some specific data:

XPath xPath = XPathFactory.newInstance().newXPath();

String someID = xPath.compile("//SomeID").evaluate(doc);

Prepare and do some redirection (other code included that I thought would be useful that I had to dig around for too):

final String nodePath = properties.get("nodePath", null);

final StringBuilder path = new StringBuilder(request.getContextPath());

final String programCoursesURL = "/content/some-project/someForm.html?RecordID=" + someID;

path.append(programCoursesURL);

response.sendRedirect(path.toString());

In this case the form had a template that read the recordID parameter and pre-filled from a DB using the RecordID. You can also use a "dataRef" service too. The redirect code was a tough piece to get over though.

Avatar

Level 6

Thanks very much Lee, but how do I get the RecordID parameter from my prefill script?  All I have is the DataXMLOptions parameter. 

Avatar

Level 3

In the template that does the actual render of the form, you can populate the "data" variable that will be pushed into the form. To get variables and parameters, the most useful gets are from the properties object and the request object. To get a parameter passed by a form or URL, you can use "getParameter" on the request object.

For example:

String title = properties.get("jcr:title", "default title");
String text = properties.get("jcr:text", "default text");

String employeeID = request.getParameter("employeeID");
String entryType = request.getParameter("entryType");

<%

           String dataXML="<afData>" +

                            "<afUnboundData>" +

                                "<dataRoot>" +

                                    "<first_name>"+ "Tyler" + "</first_name>" +

                                    "<last_name>"+ "Durden " + "</last_name>" +

                                    "<id>"+ employeeID + "</id>" +

                                    "</dataRoot>" +

                            "</afUnboundData>" +

                        "</afData>";

        slingRequest.setAttribute("data", dataXML);

%>

Avatar

Level 6

I've been trying to get this going using Lee's code.  I still don't know how to access the get parameter in the prefill script of my second form.  Also, I'm getting an error when I use the redirect (although the redirect seems to be working).  Here's my code:

log.info("This is GCDS Submit 906");

final StringBuilder path = new StringBuilder(request.getContextPath());

final String redirectURL = "/content/forms/af/jared/second-form.html";

path.append(redirectURL);

log.info("Here A");

response.sendRedirect(path.toString());

The log says:

02.08.2018 09:17:32.228 *INFO* [10.128.42.67 [1533215852114] POST /lc/content/forms/af/jared/gcds-submit/jcr:content/guideContainer.af.submit.jsp HTTP/1.1] apps.gcds.gcds_submit.post$POST$jsp This is GCDS Submit 906

02.08.2018 09:17:32.228 *INFO* [10.128.42.67 [1533215852114] POST /lc/content/forms/af/jared/gcds-submit/jcr:content/guideContainer.af.submit.jsp HTTP/1.1] apps.gcds.gcds_submit.post$POST$jsp Here A

02.08.2018 09:17:32.230 *ERROR* [10.128.42.67 [1533215852114] POST /lc/content/forms/af/jared/gcds-submit/jcr:content/guideContainer.af.submit.jsp HTTP/1.1] com.adobe.aemds.guide.servlet.GuideSubmitServlet Could not complete Submit Action due to null

java.lang.IllegalStateException: null

    at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:420)

    at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:170)

    at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:170)

. . .

Any ideas?  Thanks.

Avatar

Level 10

This use case needs to be documented. We are going to add this to our HELXP community article list to make it easy to follow.

Avatar

Level 6

Lee,

I'm really grateful for your patient attempt to help me.  I'm afraid I don't know what "In the template that does the actual render of the form" means.  I am using a custom prefill service, which is written in Java and packaged up in an OSGi bundle.  The data to prefill the form are returned from the getDataXMLForDataRef method defined in the DataXMLProvider interface.  The code you've provided looks like it's from a JSP.  Where would I put this JSP in CRX-DE Lite?  Does it get called before or after the prefill service?  How do I pass data between this and the prefill service?  Thanks.

Jared