{"id":16876327,"url":"https://github.com/rsoesemann/apex-chainable","last_synced_at":"2026-02-07T18:01:22.705Z","repository":{"id":151300432,"uuid":"166958987","full_name":"rsoesemann/apex-chainable","owner":"rsoesemann","description":"Chain Asynchronous Apex in a readable and flexible way without hardcoding the successor. ","archived":false,"fork":false,"pushed_at":"2024-09-04T14:17:14.000Z","size":45,"stargazers_count":56,"open_issues_count":2,"forks_count":13,"subscribers_count":4,"default_branch":"master","last_synced_at":"2026-01-29T16:37:09.092Z","etag":null,"topics":["apex-batches","asynchronous-programming","batch","batch-chains","clean-code","fluent-api","framework","queueable","salesforce-apex","schedulable","successor-batch"],"latest_commit_sha":null,"homepage":"","language":"Apex","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rsoesemann.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-22T08:45:02.000Z","updated_at":"2025-08-31T14:48:43.000Z","dependencies_parsed_at":"2024-09-05T18:04:24.493Z","dependency_job_id":"640b2b90-bda0-449d-972e-34ccb9d705e4","html_url":"https://github.com/rsoesemann/apex-chainable","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/rsoesemann/apex-chainable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsoesemann%2Fapex-chainable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsoesemann%2Fapex-chainable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsoesemann%2Fapex-chainable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsoesemann%2Fapex-chainable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rsoesemann","download_url":"https://codeload.github.com/rsoesemann/apex-chainable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsoesemann%2Fapex-chainable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29202988,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T17:44:10.191Z","status":"ssl_error","status_checked_at":"2026-02-07T17:44:07.936Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apex-batches","asynchronous-programming","batch","batch-chains","clean-code","fluent-api","framework","queueable","salesforce-apex","schedulable","successor-batch"],"created_at":"2024-10-13T15:39:03.033Z","updated_at":"2026-02-07T18:01:22.655Z","avatar_url":"https://github.com/rsoesemann.png","language":"Apex","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Apex Chainable [![Codacy Badge](https://app.codacy.com/project/badge/Grade/7024ec2e01c24c03a323e565e029a5a6)](https://www.codacy.com/gh/rsoesemann/apex-chainable/dashboard?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=rsoesemann/apex-chainable\u0026amp;utm_campaign=Badge_Grade)\n\n\u003ca href=\"https://githubsfdeploy.herokuapp.com?owner=rsoesemann\u0026repo=apex-chainable-batch\"\u003e\n  \u003cimg alt=\"Deploy to Salesforce\"\n       src=\"https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/src/main/webapp/resources/img/deploy.png\"\u003e\n\u003c/a\u003e\n\nApex Batches can be chained by calling the successor batch from the `finish()` method of the previous batch. \nBut such hardcoding makes this model inflexible. It's hard to build the chain from outside, neighter from a central class \nnor on runtime dependant on business logic.\n\nThe same applies when the `execute()` method of `Schedulable` or `Queueable` classes call other classes.\n\n## With `Chainable`\n\nThe `Chainable` wrapper class of this repository overcomes those drawbacks.\n\n - No need to hardcode successor batch in `finish()` method\n - Created batch chains of arbitrary length without changing existing Batch classes\n - Support `Batchable`, `Queueable` and `Schedulable` classes as chain members\n - Allows sharing and passing of variables between chain members\n\n```java\n      new FirstBatch().setShared('result', new Money(0))\n            .then(AnotherBatch())\n            .then(QueueableJob())\n            .then(ScheduledJob())\n            ...\n            .execute();\n```\n\n## Without `Chainable`\n\n```java\nclass FirstBatch implements Batchable\u003cSObject\u003e {\n    Iterator\u003cSObject\u003e start(BatchableContext ctx) { ... }\n\n    void execute(BatchableContext ctx, List\u003cAccount\u003e scope) { ... }\n\n    void finish(BatchableContext ctx) {\n        Database.enqueueBatch(new SecondBatch()); \n    }\n}\n```\n\n```java\nclass AnotherBatch implements Batchable\u003cSObject\u003e {\n    Iterator\u003cSObject\u003e start(BatchableContext ctx) { ... }\n\n    void execute(BatchableContext ctx, List\u003cAccount\u003e scope) { ... }\n\n    void finish(BatchableContext ctx) {\n        System.schedule('name', cron, new ScheduledJob()); \n    }\n}\n```\n\n## Deferring\n\nWhen all the \"links\" in the chain cannot be completely identified beforehand in order to assemble them in a single chain and trigger its execution, the chain execution can be *deferred* until the end of the transaction. All chainable processes that have been *deferred* will be automatically chained together in a **single** chain and executed sequentually.\n\n```java\n\n// automation 1\nnew FirstBatch()\n        .then(AnotherBatch())\n        .setShared('result', new Money(0)) // shared variables will be available across other following deferred chainables\n        .executeDeferred();\n\n// automation 2\nnew QueueableJob()\n        .then(ScheduledJob())\n        ...\n        .executeDeferred();\n\n// the framework would internally build the chain in a separate transaction with a definition like this\nnew FirstBatch()\n        .then(AnotherBatch())\n        .setShared('result', new Money(0))\n        .then(QueueableJob())\n        .then(ScheduledJob())\n        .execute();\n\n```\n\n### Considerations\n\nIn order to leverage the deferring of the Chainable instances there are some nuances compared to its direct execution.\n\n* Override the `getDeferArgs` and `setDeferredArgs` if the class receives external input before its execution via constructor parameters or setters to serialize and deserialize them (See the `SampleDeferArgQueueable`).\n* Must have a no-arg constructor (if no explicit constructor exists, the default implicit one will be used)\n* Must not be an inner class (due to difficulty on dynamic name inferring)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frsoesemann%2Fapex-chainable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frsoesemann%2Fapex-chainable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frsoesemann%2Fapex-chainable/lists"}