Overview
This article provides details about how to invoke a web service using a custom button created using Form Script.
Prerequisites
To perform the actions here, you should have:
- A web-service ready to be called.
- A record-form ready from where you can call the web-service.
- A basic understanding of JavaScript/JQuery and AJAX call.
Challenges
- Adding a custom button in a record form. For information, see Adding a Custom Button to an Arbitrary Form using Form Script.
- Invoke the web-service method by clicking the custom button.
Procedure
Follow the article Adding a Custom Button to an Arbitrary Form using Form Script, and then put the following code into the button click handler:
// Added a click event listener to the Added button.
$("#customButton").on(‘click’, function () {
var orderNum = getTextFieldValue(_sdForm, ‘orderNum’);
var inputXML = "<platform><execClass>"
+ "<clazz>com.platform.yourCompany.orders.WebServiceProxy</clazz>"
+ "<method>getOrderDetails</method>"
+ "<orderNum>" + orderNum + "</orderNum>"
+ "</execClass></platform>";
$.ajax({
type: 'POST',
url: '/networking/rest/class/operation/exec',
contentType: 'application/xml',
Accept:'application/json',
data: inputXML,
dataType: 'json',
success: function (data) {
if (data.platform.execClass.invoiceNumber) {
setTextFieldValue(_sdForm, "invoice_number", data.platform.execClass.invoiceNumber);
setTextFieldValue(_sdForm, "order_date_1462329883", data.platform.execClass.orderDate);
} else {
// No data found handler;
}
}
});
});