Single Page Suitelet without a Restlet

As you architect a custom Suitelet, if you decide to include client-to-server “Restful interactions”, you can accomplish this without a Restlet.

This is the classic model: Suitelet delivers a page to the browser. Without reloading the page, client-side JavaScript interacts with the server to update the page in place. Classic restful behavior.

This is the model I’m proposing, A single Suitelet that functions as both the Suitelet and the Restlet.

The trick is to call your Suitelet from a client-side JavaScript AJAX request and include a parameter (in my case it is the “method” parameter) which tells the Suitelet to respond like a Restlet. The server’s response is a Jasonized object delivered to the client as a string. Note below that I’m adding a context.response.setHeader to change the content type to “application/json”.

In the following example, I’m demonstrating a Restful interaction between a jQuery UI auto-complete control and the Suitelet. My example is slightly more complicated than using auto-complete “out of the box”, where I’d pass the method parameter along with a second parameter “term”. I’m also passing some filter criteria in addition to the term which limits responses beyond what is typed in the auto-complete field. Hopefully that makes sense!

The auto-complete field is the one at the bottom under “Select a Contact & Company”. The filters are clearly marked and should be self explanatory. Sorry, I’m overly complicating my example. I’m not going to redo it just to include in this post.

Here is a version of the same client-to-server interaction with Chrome Dev Tools showing the network traffic. It’s lightweight and performant.

Finally, this is client-side JavaScript which calls the Suitelet. The URL might look like this:

/app/site/hosting/scriptlet.nl?script=[customscript_your_script_id_here]&deploy=[customdeploy_your_deployment_id_here]&method=[your_method_here]&… additional parameters.

The response is an object, “data”, which is NOT a Jasonized string. It can be referenced as-is, as an object, no parsing required.

In summary: I like the idea of having one script to both serve the single-page app and interact with the client-side JavaScript. If you prefer the classic model which includes the Restlet, be my guest. Potato, Po-tot-o, it’s all the same to me!

Leave a comment