The Lapps Editor
Lapps comes prebuilt with a simple editor for you to write your code.
The Editor Does | The Editor Doesn’t |
Basic colour coding. | Identify spelling errors. |
Identify missing braces and brackets. | Identify logical errors. |
Let you expand or collapse sections of your code. | |
Give you the ability to format your code. | |
Let you expand to a full-screen view. |
Keyboard Shortcuts
CTRL + Space | Autocomplete feature that supports Javascript Syntax, suggestions from your local code and shortcodes* to insert code snippets. |
CTRL + S | Saves the code written in your editor. |
<shortcode> + TAB | Autopopulate the available code snippets with shortcodes. |
For the complete list of shortcuts, see Lapps Keyboard Shortcuts.
Here are some helpful shortcodes –
getrequest, postrequest, putrequest, lscreatelead, lsupdatelead, lsgetleadbyid, lsgetleadbyemail, lsgetleadbyphone, lscreateactivity, lsupdateactivity, lscreatetask, lssendemail, loginfo, logdebug, logwarning, logerror, logfatal, dbget, dbput, settings
Writing Code in Lapps
When you create a new Lapp, you’ll see some sample code pre-populated in the editor. This sample code contains all the instruction you’ll need to write your first Lapp.
In a nutshell, the sample code is doing a few things –
- Creating a ‘main‘ function.
- Creating settings.
- Storing a value in the database.
- Retrieving that value from the database.
- Creating logs.
The main Function
This function is the starting point of your Lapp. Whenever your Lapp gets triggered (via the Lapp API URL) your code execution will begin from the main function. There are no restrictions on the number of functions you can write but every Lapp you create must have 1 main function.
This main function accepts 3 parameters (queryString, body, callback)and have the signature –
function main(Object queryString, Object body, function callback) { }
These instructions have also been commented out in the editor –
The queryString Object
It contains the query string parameters in JSON format that will be appended to the Lapp API URL.
If you pass the following query string to the API –
<APIURL>?firstName=john&lastName=doe
You can then access the parameters in your code as queryString.firstName and queryString.lastName.
The body Object
This contains the body you want to pass in the Lapp API as Content-Type: application/json.
So if you pass the following body –
{“FirstName”: “John”, “LastName”:”Doe”}
You can then access the body in your code as is. No special parsing is required. For example –
var firstName = body.FirstName; var lastName = body.LastName;
The Callback Function
This function returns a response to the API caller. It has the following signature –
callback(Error error, Object result) { }
- error is used to provide the results of the failed Lapp execution, for example, callback(error, null). When a Lapp function succeeds, you should pass null as the first parameter, for example, callback(null, data).
- result is used to provide the result of a successful Lapp execution. The result provided must be JSON.stringify compatible. If an error is provided, this parameter should be null.
Here are some sample values you can return using the callback function.
- “some result”
- {“id”: “123”}
- [{“id”: 1}, {“id”: 2}, {“id”: 3, “name”: “John Doe”}]
- [{“id”: 1}, {“id”: 2}, {“id”: 3, “details”: {“firstName”: “John”, “LastName”: “Doe”}]
Accessing Request Data From LeadSquared Events
If you’re using a Lapp behind a LeadSquared Webhook, Rule action or an Automation action, and you need help to access LeadSquared events or posted data, click the Accessing LeadSquared Events link as shown below –
Code Snippets
If you’re having trouble getting started with Lapps, the Code Snippets link will help you pick up speed. You’ll find a ton of ready to use code blocks for interacting with the LeadSquared system. You can directly insert these chunks of code into the appropriate places in the editor –
Settings
Settings are key-value pairs you want to store such as API keys or other confidential data. These will be accessible from the Settings tab on your navigation menu. You can store different values for test and live environments. All values stored here are encrypted and cannot be accessed by anyone but you.
You can create these settings as specified –
Writing Logs
Note: Logs will become visible as an option on your navigation panel after you’ve published your Lapp to the test or live environment.
The signature of the log function is as follows –
ls.log.<LogType>(string Message, Object data=null) where the 2nd parameter data is optional.
To write logs, all you have to do is call the function ls.log where the “Log Type” can be defined as –
- ls.log.Info
- ls.log.Warn
- ls.log.Error
- ls.log.Debug
- ls.log.Fatal
Sample log statements are already commented out in the editor –
Here are some more example log statements you can write –
- ls.log.Info(“Plain Info log”)
- ls.log.Info(“your querystring is:”, queryString)
- ls.log.Warn(“This is a warning message with data: “, { “id” : 0 })
Next Steps
- Instead of writing code in the Lapps editor, you could also upload your own code through the Zip Upload feature.
- Or, if you’re done writing your code, you Publish a Lapp.