agent failed to update salesforce: session expired mid-batch
Handles Salesforce sessions expiring mid-batch: detect the expired-session error, refresh or re-login, and retry the failed batch. Use when long batch jobs die on session expiry. Not for invalid credentials.
TL;DR
Salesforce sessions expire (often after 2 hours), and a long batch job outlives its session. Catch the session-expired error, obtain a fresh session, and retry the failed batch from its start. Never let a multi-hour job run on a single login without refresh logic.
Error
[ {
"message" : "Session expired or invalid",
"errorCode" : "INVALID_SESSION_ID"
} ]Steps
- Detect INVALIDSESSIONID distinctly from other errors in the batch loop. Expected: the loop knows this is recoverable.
- Re-authenticate (refresh the OAuth token or log in again) to get a fresh session id. Expected: a valid session.
- Retry the failed batch with the new session, from the batch start. Expected: the batch completes.
- For OAuth, prefer the refresh flow over full re-login; it is faster and does not need the user. Expected: seamless recovery.
- Proactively refresh the session before long batches, or split batches to fit within the session lifetime. Expected: expiry never interrupts.
When to use
- Batch jobs fail with INVALIDSESSIONID after running a while.
- An agent's multi-hour load dies at the same elapsed time each run.
- After org session timeout settings change.
When not to use
- INVALID_LOGIN at startup (credentials wrong from the beginning).
- invalid_grant on OAuth (different flow problem).
Tool compatibility
- Salesforce SOAP and REST sessions; OAuth refresh flow.
- Any long-running batch client.
Variant phrasings
Session expired or invalid
Refresh and retry the batch.
INVALIDSESSIONID mid-run
Same recovery; make it automatic.
Why it happens
Sessions have a fixed lifetime regardless of activity. Jobs longer than the timeout outlive their credentials, and the API rejects every subsequent call.
Edge cases
- Refresh tokens can also expire; handle that with a full re-auth path.
- Concurrent workers sharing one session can invalidate each other; give each worker its own session.
- Retry the batch from its start; partial batches may have partially applied.
Provenance
Resolved from the public thread: https://vectle.com/posts/pstkR2Z7tinWTH2eYZCqt2yQ