Write Code for Batch Jobs

Import LS library for writing logs and accessing variable values in the code. For the full details of SDK for offline developments, see https://pypi.org/project/lsbatch/.


Writing Logs

Examples for writing logs are also included as comments in the sample code.

Writing a simple log –


Passing an additional JSON object (e.g., support email address) to a log –

LS.log.info('Hello', {"Support Email": "support@leadsquared.com"}

Accessing a variable value – LS.settings.<KeyName> (Key-Pair eg. Key = mykey) –


Writing a debug log –

LS.log.debug('Debug Log Example')

Errors and exceptions –

LS.log.error(type(err).__name__, traceback.format_exc())

LS.log.fatal(type(err).__name__, traceback.format_exc())


Using Variables

Used to store key/value pair with sensitive/private data. For example API key for some third-party service or your SFTP password. It can be retrieved using the following key –



Sending Notification Emails

Automated notification emails with the default template can be configured directly from the UI, see the settings section of Create a Batch Job.

To create your own custom email content, use –

LS.email.send_email_notification(<EmailType>, <Subject>, <EmailBody>, isHTML =<True/False>), attachments =<ListofAttachments>)


  • EmailType one of “Success” or “Failure” which corresponds to success of failure of execution status
  • Subject can be maximum 255 character, above which it will be trimmed
  • EmailBody plain text or HTML content
  • isHTML is an optional parameter with default value as True
  • attachments is a list of attachments that can be passed as file names. These files are expected to be present in the user folder of Batch Jobs executions.

Note: This function can only be called max 3 times in one Batch Job execution.


Connecting to Database

With Batch Jobs, you can directly communicate with the LeadSquared database or the LeadSquared ETL database for your account. You can use this feature to create your own customized reports.

Note: Connecting to Database is by default disabled in Batch Jobs. If you require it, please connect us at support@leadsquared.com. If it’s disabled, “Test Queries” (shown below) won’t be visible.

Use the following function to query your account database or ETL database-

LS.db.execute_DB_query(<query>, multi=False)
LS.db.execute_ETL_query(<query>, multi=False)

To test the database connection and queries, under Tools, click Test Queries.

test queries

Select the database as either Tenant DB or Tenant ETL DB. Enter a query, then click Run Query. The results will display a sample of only 10 rows.

test queries


Next Steps

Learn how to Create a Batch Job.