Show Menu
TOPICS×

API to invoke form data model service from adaptive forms

Overview

AEM Forms enables form authors to further simplify and enhance the form filling experience by invoking services configured in a form data model from within an adaptive form field. To invoke a data model service, you can either create a rule in the visual editor or specify a JavaScript using the guidelib.dataIntegrationUtils.executeOperation API in the code editor of the rule editor .
This document focuses on writing a JavaScript using the guidelib.dataIntegrationUtils.executeOperation API to invoke a service.

Using the API

The guidelib.dataIntegrationUtils.executeOperation API invokes a service from within an adaptive form field. The API syntax is as follows:
guidelib.dataIntegrationUtils.executeOperation(operationInfo, inputs, outputs)

The structure of the guidelib.dataIntegrationUtils.executeOperation API specifies details about the service operation. The syntax of the structure is as follows.
var operationInfo = {
formDataModelId,
operationTitle,
operationName
};
var inputs = {
inputField1,
inputFieldN
};
var outputs = {
outputField1,
outputFieldN
}


The API structure specifies the following details about the service operation.
Parameter Description
operationInfo Structure to specify form data model identifier, operation title, and operation name
formDataModelId Specifies the repository path to the form data model including its name
operationName Specifies the name of the service operation to execute
inputs Maps one or more form objects to the input arguments for the service operation
Outputs Maps one or more form objects to output values from the service operation to populate form fields
success Returns values based on the input arguments for the service operation. It is an optional parameter used as a callback function.
failure Displays an error message if the success callback function fails to display the output values based on the input arguments. It is an optional parameter used as a callback function.

Sample script to invoke a service

The following sample script uses the guidelib.dataIntegrationUtils.executeOperation API to invoke the getAccountById service operation configured in the employeeAccount form data model.
The getAccountById operation takes the value in the employeeID form field as input for the empId argument and returns employee name, account number, and account balance for the corresponding employee. The output values are populated in the specified form fields. For example, the value in name argument is populated in the fullName form element and value for accountNumber argument in account form element.
var operationInfo = {
"formDataModelId": "/content/dam/formsanddocuments-fdm/employeeAccount",
"operationName": "getAccountDetails"
};
var inputs = {
"empid" : employeeID
};
var outputs = {
"name" : fullName,
"accountNumber" : account,
"balance" : balance
};
guidelib.dataIntegrationUtils.executeOperation(operationInfo, inputs, outputs);


Using the API with callback function

You can also invoke the form data model service using the guidelib.dataIntegrationUtils.executeOperation API with a callback function. The API syntax is as follows:
guidelib.dataIntegrationUtils.executeOperation(operationInfo, inputs, outputs, callbackFunction)

The call back function can have success and failure callback functions.

Sample script with success and failure callback functions

The following sample script uses the guidelib.dataIntegrationUtils.executeOperation API to invoke the GETOrder service operation configured in the employeeOrder form data model.
The GETOrder operation takes the value in the Order ID form field as input for the orderId argument and returns order quantity value in the success callback function. If the success callback function does not return the order quantity, the failure callback function displays the Error occured message.
If you use the success callback function, the output values do not populate in the specified form fields.
var operationInfo = {
    "formDataModelId": "/content/dam/formsanddocuments-fdm/employeeOrder",
    "operationTitle": "GETOrder",
    "operationName": "GETOrder"
};
var inputs = {
    "orderId" : Order ID
};
var outputs = {};
var success = function (wsdlOutput, textStatus, jqXHR) {
order_quantity.value = JSON.parse(wsdlOutput).quantity;
 };
var failure = function(){
alert('Error occured');
};
guidelib.dataIntegrationUtils.executeOperation(operationInfo, inputs, outputs, success, failure);