---
schema_version: "1"
id: 19302b9162baecb9eeb698ed31e0c9058477a19269eae0592427a330535dc1c7
title: Batch Job Instance ID
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/mule-runtime/4.6/batch-job-instance-id.md
canonical_url: https://docs.mulesoft.com/mule-runtime/4.6/batch-job-instance-id
source_site: docs.mulesoft.com
content_type: documentation
content_hash: c04e049078bf2c8b522c8c330135d3412efbeb4651865e1213db923000de160f
description: MuleSoft Documentation Site
release: "4.6"
latest: false
---
---
product: Mule Runtime
version: 4.6
keywords: connectors, anypoint, studio, batch, batch processing
---

# Batch Job Instance ID

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Users of batch processing frequently need the ability to determine a Batch job’s instance ID during
the execution phases of a Batch job.

The Batch job instance ID is useful to:

- Pass the local job instance ID to an external system for referencing and managing data

- Improve the job’s custom logging

- Send email or SMS notifications for meaningful events

## Example

Mule exposes the batch job instance ID through a flow variable of key `batchJobInstanceId`. That
flow variable is available at the beginning of the input phase. The flow variable is also available
in every step and in the on-complete phase.

![batch job instance id c4362](/mulesoft/current/en-us/mule-runtime/4.6/_images/batch-job-instance-id-c4362.png)

The log output produces the following:

```text
com.mulesoft.mule.runtime.module.batch.internal.engine.DefaultBatchEngine: Created instance '7dc5cad0-ab09-11e8-a790-9801a7b055d3' for batch job 'CreateLeadsBatch'
```

## Custom Batch Job Instance ID

You can use a DataWeave expression to specify a unique identifier for each batch job by passing a
**Job Instance Id** attribute that takes a DW expression.  
Note that constant values are not allowed for batch jobs and if the DW expression returns a
previously seen value, Mule throws an exception and does not create the job instance. If you don’t
set a job instance ID, Mule auto generates a UUID to assign to your instance.

The UUID generated for the job instance described above is `ba01e1a0-f5c7-11e4-9414-10ddb1daeb6d`.
As you can tell, it’s not a human readable identifier making it hard to correlate to an actual
execution and considering you could be running multiple jobs at the same time, this ID is not
meaningful at all.

To guarantee uniqueness, you can define the Job Id as the server’s current date by using the
following DataWeave expression:  
`#['Job From ' ++ now() as String {format: 'dd/MM/yy hh:mm:ss'}]`.

This names the execution instance `Job From 15/01/16 05:23:12`.

```text
com.mulesoft.module.batch.engine.DefaultBatchEngine: Created instance Job From 15/01/16 05:23:12 for batch job contacts-to-SFDCBatch
com.mulesoft.module.batch.engine.DefaultBatchEngine: Starting input phase
com.mulesoft.module.batch.engine.DefaultBatchEngine: Input phase completed
com.mulesoft.module.batch.engine.queue.BatchQueueLoader: Starting loading phase for instance 'Job From 15/01/16 05:23:12' of job 'contacts-to-SFDCBatch'
com.mulesoft.module.batch.engine.queue.BatchQueueLoader: Finished loading phase for instance Job From 15/01/16 05:23:12 of job contacts-to-SFDCBatch. 3 records were loaded
com.mulesoft.module.batch.engine.DefaultBatchEngine: Started execution of instance 'Job From 15/01/16 05:23:12' of job 'contacts-to-SFDCBatch'
```

The logs of a properly identified instance are easier to read.

```xml
<batch:job jobName="CreateLeadsBatch" maxFailedRecords="1000" jobInstanceId="#['Job From ' ++ now() as String {format: 'dd/MM/yy hh:mm:ss'}]">
	<batch:process-records >
		<batch:step name="LeadExistsStep">
			...
		</batch:step>

		<batch:step name="LeadInsertStep" acceptExpression="vars.leadNotFound">
			...
		</batch:step>
	</batch:process-records>
	<batch:on-complete >
		...
	</batch:on-complete>
</batch:job>
```

## See Also

- [Configuring Batch Components](https://docs.mulesoft.com/mule-runtime/4.6/batch-filters-and-batch-aggregator)
