Use the following script to give every active resource the same weekly hours, for example 8 hours Monday through Friday, for one or more calendar years. It’s the quickest way to load capacity for the first time, and to add capacity for new resources.
This is one of the scripts in Create Capacity Records in Bulk. Read Before you run a script first.
What the script does
For each active resource, the script creates one RH Capacity record per calendar year, or one per month, with the hours you set for Sunday through Saturday.
- A year (or month) is skipped for a resource that already has any capacity record in it. The script never creates overlapping capacity, and it’s safe to run more than once.
- Resources with no capacity at all get every year (or month).
- Archived resources are skipped unless you change the setting.
To extend the hours each resource already has, instead of giving everyone the same hours, use Copy Each Resource’s Latest Capacity.
Settings
DRY_RUN– Leave astrueto see what the script would create without creating anything. Set tofalseto create the records.FIRST_YEAR– The first calendar year to create, for example2027. To include the current year, set it to the current year.NUMBER_OF_YEARS– The number of calendar years to create. The default is3.ONE_RECORD_PER_MONTH– Set totrueto create 12 records per year, one for each month, instead of one record per year.SUNDAYthroughSATURDAY– The hours per day for the new records. The defaults are 8 hours Monday through Friday and 0 on Saturday and Sunday.ROLE– Optional. A value from the Role picklist on RH Capacity, in quotes, for example'Developer'. Leave asnullto create records without a role.MAX_RECORDS_PER_RUN– The number of records created in one run. The default and the maximum are200.INCLUDE_ARCHIVED_RESOURCES– Set totrueto also create capacity for archived resources.
A run also stops early when the new records would make it slow to finish, so a run can create fewer records than MAX_RECORDS_PER_RUN. With ONE_RECORD_PER_MONTH set to true, each run creates about one year of monthly records for 10 resources, so expect more runs.
The script
// Creates capacity with the same weekly hours for every active resource: one RH Capacity record
// per calendar year, or one per month, for one or more years.
//
// A year (or month) is skipped for a resource that already has any capacity record in it, so the
// script never creates overlapping capacity and is safe to run more than once. Resources with no
// capacity at all get every year (or month).
//
// How to run (Developer Console > Debug > Open Execute Anonymous Window, tick "Open Log"):
// 1. Run with DRY_RUN = true and read the summary (filter the log to "Debug Only").
// 2. Set DRY_RUN = false and run again to create the records.
// 3. If the summary says resources are still waiting, run again until it says none are.
//
// Resource Hero builds the daily capacity behind each new record in the background. Watch the
// Async Job Status field on the RH Capacity records; a blank or "Completed" status means done.
// ---- Settings ----
Boolean DRY_RUN = true; // true = report only, nothing is created
Integer FIRST_YEAR = 2027; // first calendar year to create
Integer NUMBER_OF_YEARS = 3; // how many calendar years to create
Boolean ONE_RECORD_PER_MONTH = false; // true = 12 records per year instead of 1
Decimal SUNDAY = 0; // hours per day for the new records
Decimal MONDAY = 8;
Decimal TUESDAY = 8;
Decimal WEDNESDAY = 8;
Decimal THURSDAY = 8;
Decimal FRIDAY = 8;
Decimal SATURDAY = 0;
String ROLE = null; // optional Role for the new records, for example 'Developer'
Integer MAX_RECORDS_PER_RUN = 200; // keeps each run inside Salesforce limits; 200 at most
Boolean INCLUDE_ARCHIVED_RESOURCES = false;
// ---- Script ----
// The periods to create: whole calendar years, or calendar months.
List<Date> periodStarts = new List<Date>();
List<Date> periodEnds = new List<Date>();
for (Integer year = FIRST_YEAR; year < FIRST_YEAR + NUMBER_OF_YEARS; year++) {
for (Integer month = 1; month <= 12; month += (ONE_RECORD_PER_MONTH ? 1 : 12)) {
Date periodStart = Date.newInstance(year, month, 1);
periodStarts.add(periodStart);
periodEnds.add(ONE_RECORD_PER_MONTH ? periodStart.addMonths(1).addDays(-1) : Date.newInstance(year, 12, 31));
}
}
Date windowStart = periodStarts[0];
Date windowEnd = periodEnds[periodEnds.size() - 1];
List<ResourceHeroApp__Resource__c> resources = INCLUDE_ARCHIVED_RESOURCES
? [SELECT Id, Name FROM ResourceHeroApp__Resource__c ORDER BY Name]
: [SELECT Id, Name FROM ResourceHeroApp__Resource__c WHERE ResourceHeroApp__Archive__c = FALSE ORDER BY Name];
// Resource + period combinations that already have capacity, keyed as "<resourceId>:<period index>".
Set<String> coveredPeriods = new Set<String>();
for (ResourceHeroApp__RH_Capacity__c existing : [
SELECT ResourceHeroApp__Resource__c, ResourceHeroApp__Start_Date__c, ResourceHeroApp__End_Date__c
FROM ResourceHeroApp__RH_Capacity__c
WHERE ResourceHeroApp__Resource__c IN :resources AND ResourceHeroApp__Start_Date__c <= :windowEnd AND ResourceHeroApp__End_Date__c >= :windowStart
]) {
for (Integer i = 0; i < periodStarts.size(); i++) {
if (existing.ResourceHeroApp__Start_Date__c <= periodEnds[i] && existing.ResourceHeroApp__End_Date__c >= periodStarts[i]) {
coveredPeriods.add(existing.ResourceHeroApp__Resource__c + ':' + i);
}
}
}
List<List<ResourceHeroApp__RH_Capacity__c>> recordsByResource = new List<List<ResourceHeroApp__RH_Capacity__c>>();
Integer recordsThisRun = 0;
List<String> reviewNotes = new List<String>();
Integer resourcesThisRun = 0;
Integer resourcesWaiting = 0;
Integer resourcesAlreadyDone = 0;
for (ResourceHeroApp__Resource__c resource : resources) {
List<Integer> periodsToCreate = new List<Integer>();
for (Integer i = 0; i < periodStarts.size(); i++) {
if (!coveredPeriods.contains(resource.Id + ':' + i)) {
periodsToCreate.add(i);
}
}
if (periodsToCreate.isEmpty()) {
resourcesAlreadyDone++;
continue;
}
// A resource with capacity in only some of the periods is worth a look: the existing record may
// cover part of a period, which leaves the rest of that period without capacity.
if (periodsToCreate.size() < periodStarts.size()) {
reviewNotes.add(resource.Name + ': already has capacity in ' + (periodStarts.size() - periodsToCreate.size()) + ' of ' + periodStarts.size() + (ONE_RECORD_PER_MONTH ? ' months' : ' years') + ' (not changed)');
}
// A resource's records all go in the same run; the first resource always fits.
if (resourcesWaiting > 0 || (resourcesThisRun > 0 && recordsThisRun + periodsToCreate.size() > Math.min(MAX_RECORDS_PER_RUN, 200))) {
resourcesWaiting++;
continue;
}
resourcesThisRun++;
List<ResourceHeroApp__RH_Capacity__c> newRecords = new List<ResourceHeroApp__RH_Capacity__c>();
for (Integer i : periodsToCreate) {
newRecords.add(
new ResourceHeroApp__RH_Capacity__c(
ResourceHeroApp__Resource__c = resource.Id,
ResourceHeroApp__Role__c = ROLE,
ResourceHeroApp__Start_Date__c = periodStarts[i],
ResourceHeroApp__End_Date__c = periodEnds[i],
ResourceHeroApp__Sunday__c = SUNDAY,
ResourceHeroApp__Monday__c = MONDAY,
ResourceHeroApp__Tuesday__c = TUESDAY,
ResourceHeroApp__Wednesday__c = WEDNESDAY,
ResourceHeroApp__Thursday__c = THURSDAY,
ResourceHeroApp__Friday__c = FRIDAY,
ResourceHeroApp__Saturday__c = SATURDAY
)
);
}
recordsByResource.add(newRecords);
recordsThisRun += newRecords.size();
}
// Measured in a v16.2 org: new records that add up to fewer than about 9,000 days are processed while
// the run is still going, and near that size they can exceed Salesforce's query limit. Larger runs
// are processed in the background. A run that would land between 4,000 and 9,000 days is therefore
// cut back to 4,000.
Integer daysThisRun = 0;
for (List<ResourceHeroApp__RH_Capacity__c> records : recordsByResource) {
for (ResourceHeroApp__RH_Capacity__c newCapacity : records) {
daysThisRun += newCapacity.ResourceHeroApp__Start_Date__c.daysBetween(newCapacity.ResourceHeroApp__End_Date__c) + 1;
}
}
while (daysThisRun > 4000 && daysThisRun <= 9000 && recordsByResource.size() > 1) {
for (ResourceHeroApp__RH_Capacity__c newCapacity : recordsByResource.remove(recordsByResource.size() - 1)) {
daysThisRun -= newCapacity.ResourceHeroApp__Start_Date__c.daysBetween(newCapacity.ResourceHeroApp__End_Date__c) + 1;
}
resourcesThisRun--;
resourcesWaiting++;
}
List<ResourceHeroApp__RH_Capacity__c> capacitiesToCreate = new List<ResourceHeroApp__RH_Capacity__c>();
for (List<ResourceHeroApp__RH_Capacity__c> records : recordsByResource) {
capacitiesToCreate.addAll(records);
}
if (!DRY_RUN && !capacitiesToCreate.isEmpty()) {
insert capacitiesToCreate;
}
List<String> summary = new List<String>();
summary.add('');
summary.add('==== Standard capacity ' + FIRST_YEAR + '-' + (FIRST_YEAR + NUMBER_OF_YEARS - 1) + (ONE_RECORD_PER_MONTH ? ', monthly records' : '') + (DRY_RUN ? ' (DRY RUN - nothing created)' : '') + ' ====');
summary.add('Capacity records ' + (DRY_RUN ? 'that would be created: ' : 'created: ') + capacitiesToCreate.size() + ' for ' + resourcesThisRun + ' resources');
summary.add('Resources already complete: ' + resourcesAlreadyDone);
summary.add('Resources still waiting (run again): ' + resourcesWaiting);
summary.add('Resources to review: ' + reviewNotes.size());
for (String note : reviewNotes) {
summary.add(' ' + note);
}
System.debug(LoggingLevel.ERROR, String.join(summary, '\n'));
Run the script
Run the script with the steps in Run a script. After the records are created, change the hours on any resource that works different hours.
Read the summary
The script prints a summary each time it runs:
- Capacity records created – The number of RH Capacity records created in this run, and the number of resources they belong to. In a dry run, this line reads “that would be created”.
- Resources already complete – Resources that already have capacity in every year (or month). Nothing is created for them.
- Resources still waiting (run again) – Resources that didn’t fit in this run. Run the script again to create their records.
- Resources to review – Resources that already had capacity in some of the years (or months). Those years are not changed. Open each resource and confirm that the existing record covers the whole year. This list includes the resources still waiting, so review it in the dry run.