Skip to main content

Version: 24.10

Form Prefill

The Form Prefill service provides XML pre-population data to the form when it is first rendered. This service is not called when someone subsequently opens a saved form.

Service Invoke Parameters

Parameters are not nullable except where otherwise indicated.

ParameterDescription
svcDefSvcDef
A service definition value object.
formForm
A form value object.
formXmlDocument
An XML Document object that is used to pre-fill the form.
txnTxn
Nullable. A transaction record value object.
requestHttpServletRequest
A HTTP servlet request.
userUser
Nullable. An authenticated user.

Script Result

The script can either return a form XML string value or update the schemaSeed XML Document parameter object. If the script returns XML data then this value will be used as either:

  • the form's XML data Document, if no Input XML Prefill Mapping are configured; or
  • input XML prefill data that is mapped into the forms XML data Document, if Input XML Prefill Mapping are configured.

If the script does not return any XML or update the schemaSeed XML Document parameter, the form will use its default prefill data instead.

Error Handling

If your Groovy script throws an error during execution, the exception is logged to the Journey Manager database error log and the user is redirected to a form error page that contains an Error Reference Number. This reference number is the Journey Manager database error log ID that you can use to look up the error. The error log record contains useful contextual information about the error.

If, at prefill time, you determine you don't want the user to view the form, you can redirect to another page using a RedirectException.

import com.avoka.tm.util.RedirectException

throw new RedirectException('/form-expired.htm?formCode=' + form.formCode)
info

You cannot use this design pattern in TransactField App because the form security filter service is executed on the server and not in the client.

Templates

Service

import com.avoka.core.groovy.GroovyLogger as logger
import com.avoka.tm.util.*
import com.avoka.tm.vo.*
import javax.servlet.http.*
import org.w3c.dom.*

class FluentFormPrefill {

/*
* Perform form prefill service
*
* return: the form XML prefill document
* throws: RedirectException to redirect user to another page
*/
Document invoke(SvcDef svcDef, Form form, Document formXml, Txn txn, HttpServletRequest request, User user) throws RedirectException {

// TODO: replace with data lookup call

XmlDoc xmlDoc = new XmlDoc(formXml)

xmlDoc.setText("/AvokaSmartForm/Customer/Address/PostCode", "9999")

return xmlDoc.document
}
}

Unit Test

import com.avoka.core.groovy.GroovyLogger as logger
import com.avoka.tm.svc.*
import com.avoka.tm.test.*
import com.avoka.tm.util.*
import com.avoka.tm.vo.*
import org.w3c.dom.*
import org.junit.Test

class UnitTest extends AbstractJUnitTest {

/*
* Perform service unit test
*
* throws exception if unit test fails
*/
@Test
void testPrefillPostCode() throws Exception {

Document formXml = new XmlDoc(testParams['Test XML Data']).document
MockRequest request = new MockRequest()

Map params = [
"svcDef": svcDef,
"form": null,
"formXml": formXml,
"txn": null,
"request": request,
"user": null
]

Document result = (Document) new ServiceInvoker(svcDef).invoke(params)

assert result != null

Path path = new Path(result)

assert "9999" == path.val("//Customer/Address/PostCode")
}
}