Writing Code in Lapps
The Lapps Editor
Lapps comes with a simple built-in editor 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
Log Levels
Log levels tell you how important a specific log message is. The following log levels are available when writing logs in Lapps –
Debug | Detailed tracing level used by application developers. |
Info | Informational messages to highlight the progress of the application. |
Warn | Potentially harmful situations that indicate potential problems. |
Error | Events that will prevent normal program execution, but might allow the application to continue running. |
Fatal | Severe error events that may cause the application to terminate. |
Note: For all Lapps created before 17th October 2022, all log levels will be enabled by default. For new Lapps created after 17th October 2022 –
- Info, Error, and Fatal are the default log levels for the Test environment.
- Error and Fatal are the default log levels for the Live environment.
Select the log levels for the test and live environments you want to view in your execution logs once you invoke a Lapp.
- For the test environment, when you disable/enable log levels, first click the Save Now button that appears under the log levels, then click . This ensures that the Lapp is republished with the new log level settings.
- For the live environment, when you enable/disable log levels,
- Click the Save Now button that appears under the log levels.
- Re-publish the Lapp to the live environment –
- Navigate to the Code tab.
- Hover your cursor over the Publish button, then click Live Environment.
Memory
Choose the required memory to execute your Lapps code, depending on the type and volume of operations performed. Options include 128 MB, 256 MB, and 512 MB.
The changes will take effect only once the Lapp is published to the Test and/or Live environments.
Keys
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 –
ls.SETTINGS.KEY_NAME
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
- To learn how to connect to your database with a Fixed IP Address, see
- 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.