Prefill Service in Adaptive Forms

In this blog, I want to share how we can use the Prefill Service feature in adaptive forms.

What is the use of Prefill Service?

It is used to prepopulate the form with given values.


How to create Prefill Service?

For creating prefill service you need to create an osgi component. For example:-

In my form I have two text boxes as shown.

So to prefill these values while Preview, I am making the osgi component. My class name is MyPrefillAdaptiveForm.class


import com.adobe.forms.common.service.*;

import org.osgi.service.component.annotations.Component;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;


@Component

public class MyPrefillAdaptiveForm implements DataProvider {

   private Logger logger = LoggerFactory.getLogger(MyPrefillAdaptiveForm.class);

   public String getServiceName() {

       return “My Prefill Service Name”;

   }

   public String getServiceDescription() {

       return “My Prefill Service”;

   }

   public PrefillData getPrefillData(final DataOptions dataOptions) throws FormsException {

       return new PrefillData() {

           public InputStream getInputStream() {

               return getData(dataOptions);

           }

           public ContentType getContentType() {

               return ContentType.XML;

           }

       };

   }

   private InputStream getData(DataOptions dataOptions) throws FormsException {

       try {

           DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

           DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

           Document doc = docBuilder.newDocument();

           Element rootElement = doc.createElement(“data”);

           doc.appendChild(rootElement);

           Element name = doc.createElement(“name”);

           name.setTextContent(“This is Name Field”);

           Element title = doc.createElement(“title”);

           title.setTextContent(“This is Title Field”);

           final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

           final DOMSource source = new DOMSource(doc);

           final StreamResult outputTarget = new StreamResult(outputStream);

           TransformerFactory.newInstance().newTransformer().transform(source, outputTarget);

           InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

           return inputStream;

} catch (Exception e) {

            logger.error(“Error while creating prefill data”, e);

            throw new FormsException(e);

       }

   }

}


Where the Prefill service option is available in form?

In the form container dialog, under Basic you can find the dropdown to select your Prefill Service.

Note:-

  • Name should be the same in text box dialog and as well as in Java class.
  • Your Prefill Service is shown by your description provided in java class in prefill service dropdown.

Now select your prefill service and click on preview. Now you will see pre-filled data in your text boxes as shown.

@Reference

https://helpx.adobe.com/experience-manager/6-2/forms/using/prepopulate-adaptive-form-fields.html#AEMFormsprefillservice