Facilio Apps SDK

The Facilio Apps SDK lets your app in the iframe communicate with the Facilio product. You can use the sdk in your apps to listen for events, invoke facilio api or thridparty api.

Getting the SDK

<script type="text/javascript" src="https://static.facilio.com/apps-sdk/latest/facilio_apps_sdk.min.js"></script>
</script>

When you include the SDK on your html, you get access to the FacilioAppSDK object.

Initializing the SDK

Call the init method to setup the SDK. And, it will return instance of FacilioAppSDK object.

var app = FacilioAppSDK.init();

Working with SDK

Methods for listening for events, getting or setting variables, invoking api, and more.

Events

The following core events are available in all widget types.

Event Description
app.loaded As soon as communication with the Facilio is established, the SDK triggers an app.loaded event.
app.destroyed Fires when the app becomes inactive.

Example

app.on('app.loaded', (data) => {
    console.log(data);
})

app.on('app.destroyed', () => {
    console.log('app destroyed');
})

Methods

The following core methods are available in all widget types.

Event Description
getWidget Get current connected app widget object.
getContext Get current record object.
getCurrentUser Get current user object.
getCurrentOrg Get current organization object.

Example

let widget = app.getWidget();
if (widget.widgetType === 'SUMMARY_PAGE') {
    // do this
}

let currentRecord = app.getContext();
console.log('Record Name: '+ currentRecord.name);

let currentUser = app.getCurrentUser();
console.log('Current User Email: '+ currentUser.email);

let currentOrg = app.getCurrentOrg();
console.log('Current Org Domain: '+ currentOrg.domain);

Variables

Some apps need to store and retrieve data (ex: api token). To enable the development of such apps, we provide a variables for apps to set (store) and get (retrieve) variable. Also, variable can be deleted when it is no longer required.

Set variable

app.variables.set(key, value) - Stores the key, value pair in the variables store. UTF-8 characters are supported. If an entry with the key is already present, then the value will be updated.

let key = "apikey";
let value = "xxxx-xxxx-xxx";
app.variables.set(key, value) // will return Javascript Promise
    .then(() => {
        // variable successfully set
    })
    .catch((error) => {
        // error in set variable
    })
Get variable

app.variables.get(key) - Is used to retrieve variable value. If the retrieval is successful, the value can be accessed using the data parameter in the .then function.

let key = "apikey";
app.variables.get(key) // will return Javascript Promise
    .then((data) => {
        // data is "xxxx-xxxx-xxx"
    })
    .catch((error) => {
        // error in get variable
    })
Delete variable

app.variables.delete(key) - Is used to delete stored variable that is no longer needed.

let key = "apikey";
app.variables.delete(key) // will return Javascript Promise
    .then(() => {
        // variable successfully deleted
    })
    .catch((error) => {
        // error in delete variable
    })

Request

Request used to make http requests to Facilio API, Connector API or External API.

Facilio API

app.request.invokeFacilioAPI(url, options) - Is used to call Facilio API. Refer the API Documentation.

Params

Param Description
url Facilio API endpoint.
options These are the available config options for making requests. Requests will default to GET if method is not specified. {method: 'GET', headers: {}, params: {}, data: {}}

Example

let url = "/v2/assets/add";
let options = {
    method: 'POST',
    data: {
        asset: {
            name: 'Chiller - West Area',
            category: 'Chiller'
        }
    }
}
app.request.invokeFacilioAPI(url, options) // will return Javascript Promise
    .then((response) => {
        let assetId = response.data.result.asset.id;
        console.log('Asset added successfully. id: '+assetId);
    })
    .catch((error) => {
        // error in request
    })
Connector API

app.request.invokeConnectorAPI(connector, url, options) - Is used to call api’s of OAuth2 Connectors defined in the Connected Apps.

Params

Param Description
connector Connector namespace.
url API endpoint.
options These are the available config options for making requests. Requests will default to GET if method is not specified. {method: 'GET', headers: {}, params: {}, data: {}}

Example

let connector = "zendesk";
let url = "/api/v2/tickets.json"; // calling Zendesk api which configured as OAuth connector in Facilio.
let options = {
    method: 'POST',
    data: {
        ticket: {
            subject: 'AC not working',
            description: 'The reception are AC seems not working. can you please check?'
        }
    }
}
app.request.invokeConnectorAPI(connector, url, options) // will return Javascript Promise
    .then((response) => {
        let ticketId = response.data.ticket.id;
        console.log('Ticket added successfully. id: '+ticketId);
    })
    .catch((error) => {
        // error in request
    })
External API

app.request.invokeExternalAPI(url, options) - Is used to call thridparty api’s such as fetching public Weather data or fetching geo location data.

Params

Param Description
url API endpoint.
options These are the available config options for making requests. Requests will default to GET if method is not specified. {method: 'GET', headers: {}, params: {}, data: {}}

Example

let url = "https://api.openweathermap.org/data/2.5/weather?q=USA"; // Fetching current weather of United States using OpenWeather API.
app.request.invokeExternalAPI(url) // will return Javascript Promise
    .then((response) => {
        let weatherData = response.data;
        // weatherData is - {"coord":{"lon":-122.09,"lat":37.39},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"base":"stations","main":{"temp":280.44,"pressure":1017,"humidity":61,"temp_min":279.15,"temp_max":281.15},"visibility":12874,"wind":{"speed":8.2,"deg":340,"gust":11.3},"clouds":{"all":1},"dt":1519061700,"sys":{"type":1,"id":392,"message":0.0027,"country":"US","sunrise":1519051894,"sunset":1519091585},"id":0,"name":"Mountain View","cod":200}
    })
    .catch((error) => {
        // error in request
    })

Interface

You can use the Interface to enable an app to trigger certain actions on the Facilio user interface. With these functions, an app can navigate to the cetain page, show dialog boxes and notifications.

openSummary

app.interface.openSummary(options) - Open module record summary.

Options param:

Key Description
module Facilio module link name. (ex: workorder, alarm, asset, etc…)
id Id of the opening record.
let options = {
    module: "workorder",
    id: 1112334
};
app.interface.openSummary(options);
openListView

app.interface.openListView(options) - Open module records list view with or without filters.

Options param:

Key Description
module Facilio module link name. (ex: workorder, alarm, asset, etc…)
view Link name of the view. Default all.
filters Filters json to show only matching records. (Optional)
let options = {
    module: "alarm",
    view: "active",
    filters: {
        message: {operator: "contains", value: "Too cold"}
    }
};
app.interface.openListView(options);
openControlDialog

app.interface.openControlDialog(options) - Function to open control dialog for controling assets realtime.

Options param:

Key Description
parentId Asset/Space Id to control.
readingId Controllable reading id.
let options = {
    parentId: 234234,
    readingId: 1111
};
app.interface.openControlDialog(options) // will return Javascript Promise
    .then((response) => {
        // control success
    })
    .catch((error) => {
        // error in control
    })