Stale Bulk Job Auto-Cleanup
Salesforce action: CloseOrAbortJob. Scheduled daily flow calls GetAllJobs to find bulk jobs running longer than 24 hours, aborts them using CloseOrAbortJob, and posts a cleanup summary to a Teams admin channel.
Provided as-is, without warranty of any kind. Review and test each pattern in a non-production environment before deploying it to live automations. See our Terms.
Overview
This scheduled daily flow monitors Salesforce bulk jobs for stale operations running longer than a configurable threshold (default 24 hours). When stale jobs are detected, the flow automatically aborts them and posts a detailed cleanup summary to a Microsoft Teams admin channel. If no stale jobs are found, it posts an all-clear confirmation message.
Use Case
Salesforce bulk API jobs can occasionally get stuck in an InProgress state due to API timeouts, lock contention, or malformed data batches. Left unattended, these stale jobs consume API limits and can block subsequent bulk operations. This flow automates the detection and cleanup, ensuring Salesforce bulk job queues remain healthy without manual intervention.
The flow is ideal for teams that:
- Automatic daily scan for stale jobs — no manual monitoring needed
- Automatic abort on detected staleness — frees up API limits and queue space
- Detailed cleanup report to Teams — ops team stays informed
- Configurable staleness threshold — adapt to your environment's needs
- Zero-error flow — uses only hard-coded object names and env vars for flexibility
Flow Architecture
Recurrence - Daily at 6:00 AM EST
RecurrenceFires once daily at 6:00 AM Eastern.
Initialize Variables (5x parallel)
Initialize variableLoad env vars and counters: ThresholdHours, TeamsGroupId, TeamsChannelId, ITTeamEmail, AbortedCount.
Compose Cutoff Timestamp
ComposeCompute addHours(utcNow(), mul(varThresholdHours, -1)) to get the cutoff datetime used to detect stale jobs.
Get All Bulk Jobs
Salesforce GetAllJobsRetrieve all bulk jobs in the Salesforce environment.
Filter Stale Running Jobs
Filter arrayKeep only jobs where state = 'InProgress' AND createdDate is earlier than the cutoff timestamp.
Check If Stale Jobs Found
If conditionBranch on whether any stale jobs were returned by the filter.
- For Each Stale Job — Loop through the filtered stale jobs.
- Abort Stale Job — Salesforce CloseOrAbortJob — set state to 'Aborted'.
- Increment Aborted Count — Track the number of aborted jobs for the summary.
- Select Job Summary Rows — Build HTML table rows with Job ID, Object, Operation, Created Date, and State.
- Compose Cleanup Summary HTML — Build a styled HTML report including threshold, aborted count, and the detail table.
Environment Variables
| Schema name | Type | Default | Description |
|---|---|---|---|
| flowlibs_SalesforceStaleJobThresholdHours | String | 24 | Age in hours after which an InProgress bulk job is considered stale and eligible for auto-abort. |
| flowlibs_AdminTeamsGroupId | String | <configure> | Teams group (team) GUID for admin notifications. Set to the GUID of the team that owns the cleanup channel. |
| flowlibs_AdminTeamsChannelId | String | <configure> | Teams channel ID inside the admin group where the cleanup summary and all-clear messages are posted. |
| flowlibs_ITTeamEmail | String | alerts@yourcompany.com | IT team distribution email used for escalation notifications. |
Connectors & Connections
| Connector | API name | Actions used |
|---|---|---|
| Salesforce | shared_salesforce | GetAllJobs (list bulk jobs) CloseOrAbortJob (abort stale job) |
| Microsoft Teams | shared_teams | PostMessageToConversation (admin channel notifications) |
Note — All connections are referenced as solution connection references; the flow is portable between environments as long as a connection is mapped at import time.
Customization Guide
Almost every realistic variant of this flow can be implemented by changing environment variable values. A few cases require small edits inside the flow definition — those are called out explicitly below.
- Deploying to another environment
- Import the solution, update the four env vars with your target Teams group/channel and IT team email, bind the Salesforce and Teams connection references, then turn the flow on. The first run happens daily at 6:00 AM EST.
- Adjust the stale threshold
- Change flowlibs_SalesforceStaleJobThresholdHours (for example, 12 for a 12-hour threshold or 48 for a 2-day threshold).
- Change notification channel
- Update flowlibs_AdminTeamsGroupId and flowlibs_AdminTeamsChannelId to point at your preferred admin Teams channel.
- Filter by a different job state
- Modify the Filter Array condition to target other states (for example, 'Queued' instead of 'InProgress').
- Add an email notification
- Insert an Outlook SendEmailV2 action after the Teams post to send an email alert to the IT team in addition to the channel message.
- Change schedule
- Adjust the Recurrence trigger frequency — for example, run every 2 hours for more aggressive monitoring instead of once daily.
Key Expressions
The flow is intentionally light on Power Fx / WDL gymnastics — the heaviest expressions are the branch-name concatenation and the approval outcome check. They are listed below in the order they appear in the flow.
EXPR.01Cutoff timestamp
Computes the datetime threshold used to detect stale InProgress bulk jobs.
EXPR.02Compound filter (state AND age)
Filter Array condition — keep jobs that are still InProgress and older than the cutoff.
EXPR.03HTML table unwrap
Strips the JSON wrapping from a Select action so HTML row fragments can be concatenated into a single table.
EXPR.04Job count check
Used in the If condition to branch on whether any stale jobs were found.
EXPR.05Convert threshold to integer
Coerces the string env var to an integer for arithmetic.
Comments
Sign in to join the conversation.
Sign inNo comments yet. Be the first to share your experience with this flow.