Form Prefill
The Form Prefill service is used provide XML pre-population data to the form when it is first rendered. Please note this service is not called when someone subsequently opens a saved form.
Service Invoke Parameters
Parameter | Description | Nullable |
---|---|---|
svcDef | the service definition value object, see SvcDef | no |
form | the form value object, see Form | no |
formXml | the XML Document object which is used to pre-fill the form with | no |
txn | the transaction record value object, see Txn | yes |
request | the servlet HttpServletRequest | yes |
user | the authenticated user, see User | yes |
Script Result
The script can either return a form XML string value or update the schemaSeed XML Document parameter object. If the script returns an XML data then this value will be either:
- used as the form's XML data Document, when no Input XML Prefill Mapping are configured
- used as input XML prefill data which will be 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, then the form will use its default prefill data instead.
Error Handling
If your Groovy script throws an error when executing the exception will be logged to the Journey Manager database error log and the user will be redirected to a form error page which will contain a Error Reference Number. This reference number is the Journey Manager database error log ID which you can use to look up the error. The error log record will contain 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 away to another page using a RedirectException.
import com.avoka.tm.util.RedirectException
throw new RedirectException('/form-expired.htm?formCode=' + form.formCode)
Please note in TransactField App you cannot use this design pattern, as the Form Prefill Service is executed on the server and not in the client.
Service Template
This section shows service template Groovy script.
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 Template
This section shows unit test template Groovy script.
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")
}
}