Skip to content

Instantly share code, notes, and snippets.

@afawcett
Last active December 29, 2022 17:12
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save afawcett/6204330 to your computer and use it in GitHub Desktop.
Save afawcett/6204330 to your computer and use it in GitHub Desktop.
Sample code from
/**
* Copyright (c) 2013, Andrew Fawcett
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - The names of its contributors may not be used to endorse or
* promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
public abstract with sharing class BatchWorker implements Database.Batchable<Object>
{
private List<Object> workList;
public Id BatchJobId {get; private set;}
public BatchWorker()
{
this.workList = new List<Object>();
}
public BatchWorker(List<Object> workList)
{
this.workList = workList;
}
public BatchWorker addWork(Object work)
{
workList.add(work);
return this;
}
public abstract void doWork(Object work);
public BatchWorker run()
{
BatchJobId = Database.executeBatch(this, 1);
return this;
}
public Iterable<Object> start(Database.BatchableContext BC) { return workList; }
public void execute(Database.BatchableContext info, List<Object> workList) { doWork(workList[0]); }
public void finish(Database.BatchableContext info) { }
}
public with sharing class ProjectMultiWorker extends BatchWorker
{
public ProjectMultiWorker addWorkCalculateCosts(Date effectiveDate, Id projectId)
{
return (ProjectMultiWorker) addWork(new CalculateCostsWork());
}
public ProjectMultiWorker addWorkBillingGeneration(Date effectiveDate, Id projectId, Id accountId)
{
return (ProjectMultiWorker) addWork(new BillingGenerationWork());
}
public override void doWork(Object work)
{
if(work instanceof CalculateCostsWork)
{
CalculateCostsWork setupProject = (CalculateCostsWork) work;
// Do work
// ...
}
else if(work instanceof BillingGenerationWork)
{
BillingGenerationWork resetProject = (BillingGenerationWork) work;
// Do work
// ...
}
}
private class CalculateCostsWork { }
private class BillingGenerationWork { }
}
public with sharing class ProjectMultiWorkerDemo
{
public static void demo()
{
Id selectedProjectId;
Id selectedAccountId;
// Process the selected Project
Id jobId =
new ProjectMultiWorker()
.addWorkCalculateCosts(System.today(), selectedProjectId)
.addWorkBillingGeneration(System.today(), selectedProjectId, selectedAccountId)
.run()
.BatchJobId;
}
}
public with sharing class ProjectWorker extends BatchWorker
{
public ProjectWorker addWork(Date startDate, Date endDate, Id projectId)
{
// Construct a worker object to wrap the parameters
return (ProjectWorker) super.addWork(new ProjectWork(startDate, endDate, projectId));
}
public override void doWork(Object work)
{
// Parameters
ProjectWork projectWork = (ProjectWork) work;
Date startDate = projectWork.startDate;
Date endDate = projectWork.endDate;
Id projectId = projectWork.projectId;
// Do the work
// ...
}
private class ProjectWork
{
public ProjectWork(Date startDate, Date endDate, Id projectId)
{
this.startDate = startDate;
this.endDate = endDate;
this.projectId = projectId;
}
public Date startDate;
public Date endDate;
public Id projectId;
}
}
public with sharing class ProjectWorkerDemo {
private class SelectedProject
{
public Date startDate;
public Date endDate;
public Id projectId;
}
public static void demo()
{
List<SelectedProject> selectedProjects;
// Create worker to process the project selection
ProjectWorker projectWorker = new ProjectWorker();
// Add the work to the project worker
for(SelectedProject selectedProject : selectedProjects)
projectWorker.addWork(
selectedProject.startDate,
selectedProject.endDate,
selectedProject.projectId);
// Start the workder and retain the job Id to provide feedback to the user
Id jobId = projectWorker.run().BatchJobId;
}
}
public with sharing class SimpleBatchApex implements Database.Batchable<String>
{
public Iterable<String> start(Database.BatchableContext BC)
{
return new List<String> { 'Do something', 'Do something else', 'And something more' };
}
public void execute(Database.BatchableContext info, List<String> strings)
{
// Do something really exepense with these strings!
}
public void finish(Database.BatchableContext info) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment