Pre-populate form fields

Avatar

Avatar
Validate 1
Level 2
alexandrad27705
Level 2

Likes

2 likes

Total Posts

19 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 2
alexandrad27705
Level 2

Likes

2 likes

Total Posts

19 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
alexandrad27705
Level 2

29-03-2016

Hi, 

I have an adaptive form which has unbound and bound fields because it it partially based on a XDP form model. 

I now need to pre-fill some of the fields with data from the currently logged in user. 

I found Adobe example files and took the following route: 

  1. Created a prefill.jsp 
  2. Include the prefill.jsp in the head.jsp of the form
  3. prefill.jsp gets the user data and creates the xml:

<%
    final boolean isAnonymous = UserPropertiesUtil.isAnonymous(slingRequest);
    boolean isDisabled = WCMMode.fromRequest(request).equals(WCMMode.DISABLED);
    if(!isAnonymous && isDisabled && slingRequest.getParameter("dataRef")== null
            && slingRequest.getAttribute("data")==null){

        UserProperties userProperties = slingRequest.adaptTo(UserProperties.class);
        String givenName = userProperties.getProperty("givenName");
        String dataXML="<afData>" +
                            "<afBoundData>" +
                                "<dataRoot>" +
                                    "<General_firstName>"+ StringUtils.trimToEmpty(userProperties.getProperty("givenName")) + "<General_firstName>" +
                                    "</dataRoot>" +
                            "</afBoundData>" +
                        "</afData>";
        slingRequest.setAttribute("data", dataXML);
    }
%>

In the above snippet, "<General_firstName>" is the element name of the form field. 

The jsp is included and has no errors, but the field is not pre-filled. Does the XML need a different structure? The above field was dragged from the content finder (the loaded XDP) but I also have unbound fields. 

Any help is greatly appreciated. 

Please don't point me to the documentation -  https://helpx.adobe.com/aem-forms/6-1/prepopulate-adaptive-form-fields.html - since it did not help me 😞

Thanks, Alex

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Ignite 1
Level 7
GirishBedekar
Level 7

Likes

167 likes

Total Posts

239 posts

Correct reply

37 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile

Avatar
Ignite 1
Level 7
GirishBedekar
Level 7

Likes

167 likes

Total Posts

239 posts

Correct reply

37 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile
GirishBedekar
Level 7

27-10-2016

import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.UserManager;
import  org.apache.jackrabbit.api.security.user.Authorizable ;

i have used the following

try {
            UserManager um = ((JackrabbitSession) session).getUserManager();
            Authorizable loggedinUser = um.getAuthorizable(session.getUserID());
            System.out.println("The path of the user is"+loggedinUser.getPath());
            System.out.println("The family name is "+loggedinUser.getProperty("profile/familyName")[0].getString());

}

suggest you use prefill service to populate forms , instead of doing it in the JSP page
        

Answers (6)

Answers (6)

Avatar

Avatar
Validate 1
Level 2
alexandrad27705
Level 2

Likes

2 likes

Total Posts

19 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 2
alexandrad27705
Level 2

Likes

2 likes

Total Posts

19 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
alexandrad27705
Level 2

03-04-2016

Hi Rishi, 

it needs to be <data> and not <dataRoot> as stated in the documentation. Submitting a form saves the form data in CRX, if you download the 'data' element you can look at the actual XML structure. Pre-filling needs to have the same structure.

Also, pre-filling can only be viewed in WCM Mode disabled, not in preview mode. 

Cheers, 

Alex

Avatar

Avatar
Establish
MVP
Nupur_Jain
MVP

Likes

165 likes

Total Posts

186 posts

Correct reply

76 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 25
View profile

Avatar
Establish
MVP
Nupur_Jain
MVP

Likes

165 likes

Total Posts

186 posts

Correct reply

76 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 25
View profile
Nupur_Jain
MVP

08-08-2016

Hello Alex,

I have already done so,

The xml i got was

<?xml version="1.0" encoding="UTF-8"?><afData>
  <afUnboundData>
    <data>
      <textbox1>hgbfvd</textbox1>
      <textbox2>ujhytgrfed</textbox2>
    </data>
  </afUnboundData>
  <afBoundData>
    <data xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"/>
  </afBoundData>
  <afSubmissionInfo>
    <lastFocusItem>guide[0].guide1[0].guideRootPanel[0].textbox2[0]</lastFocusItem>
    <computedMetaInfo/>
  </afSubmissionInfo>
</afData>

 

taking it as a base i set the data attribute , just removed unnecessary tags from above xml

Avatar

Avatar
Ignite 5
Employee
workflowuser
Employee

Likes

136 likes

Total Posts

446 posts

Correct reply

70 solutions
Top badges earned
Ignite 5
Ignite 3
Ignite 1
Give Back 50
Give Back 5
View profile

Avatar
Ignite 5
Employee
workflowuser
Employee

Likes

136 likes

Total Posts

446 posts

Correct reply

70 solutions
Top badges earned
Ignite 5
Ignite 3
Ignite 1
Give Back 50
Give Back 5
View profile
workflowuser
Employee

08-08-2016

try the following steps first

1.Set the form's submit action to "Store Content". Preview the form and enter some value in the text fields

2.Submit the form

3.go to the node where the form's data is submitted. Get the data xml from the jcr:data node. Save the data xml as a xml file on your file system

4.Preview you form with the data xml stored in the previous steps

5.The forms should prepopulate with the data from the xml file

6.Then make sure the data that you are passing in your code matches the structure in the xml file which you got in step 4

Avatar

Avatar
Establish
MVP
Nupur_Jain
MVP

Likes

165 likes

Total Posts

186 posts

Correct reply

76 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 25
View profile

Avatar
Establish
MVP
Nupur_Jain
MVP

Likes

165 likes

Total Posts

186 posts

Correct reply

76 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 25
View profile
Nupur_Jain
MVP

08-08-2016

Hello Alex,

I am working on AEM forms too,

I also have tried to use prefilling of form by using data attribute but its not working.

Even tried the things you have suggested, still facing problem.

Please help me

Here is the data attribute:

<%
String dataXML="<afData>"+
                    "<afUnboundData>"+
                        "<data>"+
                            "<textbox1>hello</textbox1>"+
                            "<textbox2>hii</textbox2>"+
                        "</data>"+
                    "</afUnboundData>"+
                "</afData>";
slingRequest.setAttribute("data", dataXML);
%>

The Form is without any data model and containing only two textbox with name "textbox1" and "textbox2"

Avatar

Avatar
Boost 1
Level 2
rishim22446870
Level 2

Likes

2 likes

Total Posts

32 posts

Correct reply

12 solutions
Top badges earned
Boost 1
Affirm 5
Affirm 3
Affirm 10
Affirm 1
View profile

Avatar
Boost 1
Level 2
rishim22446870
Level 2

Likes

2 likes

Total Posts

32 posts

Correct reply

12 solutions
Top badges earned
Boost 1
Affirm 5
Affirm 3
Affirm 10
Affirm 1
View profile
rishim22446870
Level 2

01-04-2016

Hi,

Can you throw some light on what was the problem and what were the steps taken to solve it ? Also, with 6.2, we have introduced a much simpler and manageable way to prefill forms with the notion of "prefill service", the documentation of this would be live soon.

Thanks,

Rishi

Avatar

Avatar
Validate 10
Level 2
doeringa
Level 2

Likes

3 likes

Total Posts

54 posts

Correct reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 3
Boost 1
View profile

Avatar
Validate 10
Level 2
doeringa
Level 2

Likes

3 likes

Total Posts

54 posts

Correct reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 3
Boost 1
View profile
doeringa
Level 2

31-03-2016

Solved it 🙂