Home > Articles > Home & Office Computing > The Web/Virtual Worlds/Social Networking

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

2.4 Creating Your First Service: LoanPaymentService

Using some of the posted samples as guidelines, let’s create a new service. You won’t call an external service here; instead, you’ll build one using JavaScript. A familiar example is a service that calculates one’s monthly mortgage payment based on principal, interest rate, and length of loan (years).

Here’s a summary of the steps you’ll follow.

  1. Create a new service. Give it a name and a description.
  2. Add parameters to the service (optional).
  3. Provide JavaScript code that returns data to the caller.
  4. Add any error types (optional).
  5. Test drive your service and modify as necessary.
  6. Capture example return data (optional).
  7. Publish your service.

Let’s start. To create a service, click 26inline01.jpg at the top of the page and select Service as shown in Figure 2.23. You’ll see a new page that asks you to provide a description of the service. The default service name is NewService, which you should change to something meaningful. Many times, service names end in “Service,” but this is not a requirement. Call the service LoanPaymentService.

Figure 2.23

Figure 2.23 Creating a Service

This service requires three input parameters and returns a single numerical result. Error handling for input validation is handled completely by zembly; we discuss this further in the next section. Here’s the JavaScript that provides the service.

Listing 2.2. LoanPaymentService (JavaScript)

// LoanPaymentService
// Input parameters are all NUMBERs and all Required

var principal = Parameters.principal;
var interest = Parameters.interest;
var interest_rate = interest / 1200;
var years = Parameters.years;

//Perform the calculation
var months = years * 12;
var x = Math.pow(1 + interest_rate, months);
var payment = (principal * x * interest_rate)/(x-1);

return payment.toFixed(2);

Specifying Parameters in a Service

When you create a web service, you tell the service interaction page about the parameters for your service. To add parameters, click Add a new parameter in the Call - Parameters window. You specify a parameter’s characteristics in a dialog box.

When you add a new parameter you choose its type. By using the appropriate type, you take advantage of zembly’s built-in parameter validation. Table 2.1 lists the types supported.

Table 2.1. Parameter Types for Services


Additional Fields







true, false






{"firstName":"John", "lastName":"Smith"}


Keyset Provider

(Depends on provider)

Number (integer, real, or floating point)

Min Value, Max Value

55, 25.3


Max Length, Escape value

any string <= Max Length







For this service, specify three parameters (principal, interest, and years). Make them all required and Type Number. With Number you also specify the minimum and maximum values. For principal use minimum 10 and maximum 2 million (2,000,000). For interest specify minimum 1 and maximum 20. Finally, for years use minimum 1 and maximum 99. Figure 2.24 shows the Parameter Editor for parameter years.

Figure 2.24

Figure 2.24 Creating and editing a web service parameter

Note that if you make a parameter required, zembly flags an error if the caller doesn’t provide a value. If you want the parameter to be optional, uncheck Must use this parameter in the call.

Once you’ve specified the parameters, you can access them in JavaScript. For example, you access the LoanPaymentService principal parameter with Parameters.principal.

Error Handling

When you detect a problem in your service, error codes can communicate status to the caller. You specify error codes in the service’s Error Codes section. To add an error code, click Add a new error type. Figure 2.25 shows the dialog box that lets you specify a new error type. (You may also edit error codes that you have already defined.)

Figure 2.25

Figure 2.25 Creating and editing web service error codes

The error code, description, and HTTP status code all appear on your web service’s documentation page. Note that you don’t need to define an error code for the LoanPaymentService, since all error handling is performed by the built-in parameter validation.

Testing LoanPaymentService

Once you’ve built a service, you’ll want to test it. Use the Call tab located next to the source editor window. You must provide values for any required parameters and click Test drive now. This calls the service with the parameter values you’ve provided and displays any results (or error codes) in the window. Figure 2.26 shows an example with a successful test.

Figure 2.26

Figure 2.26 Testing a service

Figure 2.27 shows the built-in parameter validation when you provide a value outside the range for parameter years.

Figure 2.27

Figure 2.27 zembly’s parameter validation

Capturing Example Return Data

To help others use your service, you can capture the return data after testing your service. Simply click the Capture example button (as shown in Figure 2.26). zembly creates a new heading on your service’s documentation page and displays the output. This helps users, especially if the return data contains specific formatting (such as XML or JSON data). Figure 2.28 shows an example for LoanPaymentService.

Figure 2.28

Figure 2.28 You can display sample output on your service’s documentation page

Saving Drafts

Each time you edit your code and test drive the service, your current code is automatically saved in a draft for you. zembly displays a small bar to indicate the current draft (the bars are displayed on the right with the most recent modification saved on top of the stack).

You can force a saved draft by clicking the Save Code icon at the bottom of the editor (or typing Ctrl+Alt+S). You can return to any previously saved draft or published version simply by clicking the bar. Also, you can see the timestamp and draft or version number by holding the cursor over the bar. See “Drafts, Versions, and Timelines” on page 42 for a more detailed discussion.

Using the JavaScript Editor

The JavaScript editor color codes key words, comments, and objects. The editor includes icon commands in the lower right window (as shown in Figure 2.29) to save your draft (Ctrl+Alt+S), toggle full screen editing (Ctrl+1), format code (Ctrl+Alt+F), undo editing (Ctrl+Z), redo editing (Ctrl+Y), or create a code snippet (Ctrl+Shift+N). You can also invoke code completion with Ctrl+Space.

Figure 2.29

Figure 2.29 JavaScript editor command icons

Publishing LoanPaymentService

Click the 32inline01.jpg button to publish your service. This is the magic step that zembly provides to make services and widgets available to others. When you publish your service, zembly creates a deployable web service and deploys it in its own managed container. As you modify your service, zembly keeps track of drafts (unpublished edits) and versions (published edits). With each version you are encouraged to specify how the new version has improved (why it is cool).

Calling LoanPaymentService

Once you’ve tested and published your service, you’ll want to call it from another service or widget. The page provides the code you need to call the service from another service, from a widget, or through the browser. However, the easiest way to call your service is to use zembly’s Find & Use feature which automatically adds the code as a template in the editor. The Find & Use feature is context sensitive, so it will import the correct code depending on whether you’re currently developing a widget or service. In addition, with Find & Use you’ll see documentation about the service and its parameters.

Use the following (JavaScript) code to call your service from another service. zembly generates the comments for each parameter from the documentation you provide.

var result = Things.ganderson.LoanPaymentService({
     principal: 0, // The principal of the loan (in dollars)
     interest: 0, // The interest rate (per cent) (e.g., 6.5)
     years: 0 // How long your loan will endure (in years)

Use the following template (JavaScript) code to call your service from a widget.

    principal: 0, // The principal of the loan (in dollars)
    interest: 0, // The interest rate (per cent) (e.g., 6.5)
    years: 0 // How long your loan will endure (in years)
    onSuccess: function(data) {
    onFailure: function(error) {
        Log.write("Error: " + error.code + " : " + error.message);

A third way to call a service is to cut and paste the URL provided on the service page into the address line of your browser. Pasting the URL in your browser address line calls the service from HTTP. You must specify the parameter values in place of each [value] marker. Here is the LoanPaymentService URL with values replacing [value] in the URL.


Figure 2.30 shows the browser window after calling the LoanPaymentService.

Figure 2.30

Figure 2.30 Calling a service with HTTP using its URL in the browser

  • + Share This
  • 🔖 Save To Your Account