Invoke Web Services using Form Script

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

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:

Note: This is an example for your reference, where we are calling a web-service using the added button. To keep the example simple, the data is hard-wired into the script. Replace method name, parameters, and response with the method name, parameters and the response of your application.

// 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;

                              } 

                     }

            });

    });