Client JS SDK
While ServiceM8 add-ons are executed server-side, once rendered add-ons are able to interact with the ServiceM8 platform client side by including the ServiceM8 Client JS SDK in your rendered HTML event responses.
To expose the JavaScript API to your app, simply include the sdk.js at the top of your add-on’s <head> element. It has no dependencies but plays well with other libraries like jQuery.
<head>
<script src="https://platform.servicem8.com/sdk/1.0/sdk.js"></script>
...
</head>The SDK should be initialized by calling the init() function immediately after it is included.
<head>
<script src="https://platform.servicem8.com/sdk/1.0/sdk.js"></script>
<script type="text/javascript">
var client = SMClient.init();
</script>
...
</head>Resizing Windows
If your add-on event has been launched in a popup window (Job actions, Client actions, etc), then you can resize the popup window as required.
Method
client.resizeWindow(width, height);Arguments
Width: The width of the popup window
Height: The height of the popup window
Returns
A Promises/A+ conformant promise object.
##Example
<script type="text/javascript">
var client = SMClient.init();
client.resizeWindow(450, 600).then(function(result) {
console.log("Add-on Window Resized");
});
</script>Closing Windows
If your add-on event has been launched in a popup window (Job actions, Client actions, etc), then you can close/destroy the popup window as required.
##Method
client.closeWindow();Returns
A Promises/A+ conformant promise object.
##Example
<script type="text/javascript">
var client = SMClient.init();
client.closeWindow();
</script>Opening the Email Composer
Opens ServiceM8's email composer for the job the add-on was launched from, prefilled for the staff member to review and send. Nothing is sent automatically. The email is sent from the staff member's connected mailbox and recorded in the job diary.
Only available to job action add-ons on web/desktop. Not available in the mobile app, client actions or full-page add-ons.
Method
client.openEmailComposer({ to, subject, body, attachmentUUIDs });Arguments
to: Optional. Comma-separated email addresses. When set, replaces the default job and billing contacts.
subject: Optional. The email subject.
body: Optional. Plain text. Line breaks are kept, HTML is not rendered, and the staff member's signature is added after it.
attachmentUUIDs: Optional. UUIDs of attachments already on the job. Attachments not on the job are skipped.
Returns
A Promises/A+ conformant promise object. It resolves when the composer opens, and rejects with an InvalidRequest error outside a job add-on window or when an email is already being composed.
Example
<script type="text/javascript">
var client = SMClient.init();
client.openEmailComposer({
to: 'client@example.com',
subject: 'Your service report',
body: 'Hi,\n\nYour report is attached.',
attachmentUUIDs: ['<attachment-uuid>']
}).catch(function(err) {
console.log(err.message);
});
</script>Opening the SMS Composer
Opens the job card's SMS composer for the job the add-on was launched from, prefilled for the staff member to review and send. Nothing is sent automatically.
Only available to job action add-ons on web/desktop, and the job card must still be open.
Method
client.openSMSComposer({ to, message });Arguments
to: Optional. A mobile number. Defaults to the first job contact.
message: Optional. Plain text.
Returns
A Promises/A+ conformant promise object. It resolves when the composer opens, and rejects with an InvalidRequest error when the job card isn't open or an SMS is already being composed for that job.
Example
<script type="text/javascript">
var client = SMClient.init();
client.openSMSComposer({
to: '0400 123 456',
message: 'On our way!'
}).catch(function(err) {
console.log(err.message);
});
</script>Refreshing Data
Once your add-on has been rendered into the ServiceM8 UI, you may need to refresh your content, or perform additional server-side processing. The invoke function allows you to pass requests from client side back to your server side function / web service.
##Method
client.invoke(eventName, eventParams);Arguments
EventName: The event name you wish to invoke server-side. Event names are case-insensitive; using lower case consistently is recommended.
EventParams: An object containing key/values you wish to pass to your server-side event
##Returns
A Promises/A+ conformant promise object.
##Example
<script type="text/javascript">
var client = SMClient.init();
client.invoke("add-on_custom_event_here", {
'emailTo': 'test@example.com'
}).then(function(result) {
console.log("Server-side event invoked with result " + result);
});
</script>Updated 5 days ago

