Use the following script to end the capacity of resources who have left. Archiving a resource doesn’t change its RH Capacity records, so its future capacity stays in place until you end it.
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 archived resource, or each resource you name, the script ends capacity after LAST_DAY:
- A record that starts after
LAST_DAYis deleted. - A record that runs past
LAST_DAYis shortened to end onLAST_DAY. - Records that end on or before
LAST_DAYare not changed, so past capacity stays in place for reporting. - Running the script again changes nothing.
Settings
DRY_RUN– Leave astrueto see what the script would change without changing anything. Set tofalseto change the records.LAST_DAY– The last day of capacity to keep. The default is today. To use a specific date, enter it asDate.newInstance(2026, 9, 30)for September 30, 2026.RESOURCE_NAMES– Leave empty to end capacity for every archived resource. To end capacity for specific resources, archived or not, enter their names, for examplenew List<String>{ 'Jane Example' }. Resources who left on different dates need separate runs, one for eachLAST_DAY.MAX_DAYS_PER_RUN– The total length, in days, of the records shortened in one run. The default is3000, about 8 records of one year each. Deleting records doesn’t count toward this limit.
The script
// Ends the capacity of resources who have left, so they stop counting as available.
//
// For each archived resource (or each resource named in RESOURCE_NAMES), every RH Capacity record
// that starts after LAST_DAY is deleted, and a record that runs past LAST_DAY is shortened to end on
// LAST_DAY. Records that end on or before LAST_DAY are not changed, so past capacity stays in place
// for reporting. Running the script again changes nothing.
//
// 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 change the records.
// 3. If the summary says records are still waiting, run again until it says none are.
//
// Resource Hero rebuilds a shortened record's daily capacity across its whole original date range,
// so each run is limited by the total length of the records it shortens. Deleting is quick. 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 changed
Date LAST_DAY = Date.today(); // last day of capacity to keep
List<String> RESOURCE_NAMES = new List<String>(); // empty = every archived resource; or names, archived or not
Integer MAX_DAYS_PER_RUN = 3000; // total length of the records shortened in one run; keeps each run inside Salesforce limits
// ---- Script ----
List<ResourceHeroApp__Resource__c> resources = RESOURCE_NAMES.isEmpty()
? [SELECT Id, Name FROM ResourceHeroApp__Resource__c WHERE ResourceHeroApp__Archive__c = TRUE]
: [SELECT Id, Name FROM ResourceHeroApp__Resource__c WHERE Name IN :RESOURCE_NAMES];
List<String> notFound = new List<String>();
Set<String> foundNames = new Set<String>();
for (ResourceHeroApp__Resource__c resource : resources) {
foundNames.add(resource.Name.toLowerCase());
}
for (String name : RESOURCE_NAMES) {
if (!foundNames.contains(name.toLowerCase())) {
notFound.add(name);
}
}
List<ResourceHeroApp__RH_Capacity__c> capacitiesToDelete = new List<ResourceHeroApp__RH_Capacity__c>();
List<ResourceHeroApp__RH_Capacity__c> capacitiesToUpdate = new List<ResourceHeroApp__RH_Capacity__c>();
List<String> changed = new List<String>();
Integer recordsWaiting = 0;
Integer daysThisRun = 0;
for (ResourceHeroApp__RH_Capacity__c capacity : [
SELECT Id, ResourceHeroApp__Resource__r.Name, ResourceHeroApp__Start_Date__c, ResourceHeroApp__End_Date__c
FROM ResourceHeroApp__RH_Capacity__c
WHERE ResourceHeroApp__Resource__c IN :resources AND ResourceHeroApp__End_Date__c > :LAST_DAY
ORDER BY ResourceHeroApp__Resource__r.Name, ResourceHeroApp__Start_Date__c
]) {
String dates = capacity.ResourceHeroApp__Start_Date__c.format() + '-' + capacity.ResourceHeroApp__End_Date__c.format();
if (capacity.ResourceHeroApp__Start_Date__c > LAST_DAY) {
capacitiesToDelete.add(capacity);
changed.add(capacity.ResourceHeroApp__Resource__r.Name + ': ' + dates + ' deleted');
} else {
// The first record always fits; after that, stop at the limit.
Integer days = capacity.ResourceHeroApp__Start_Date__c.daysBetween(capacity.ResourceHeroApp__End_Date__c) + 1;
if (recordsWaiting > 0 || (daysThisRun > 0 && daysThisRun + days > MAX_DAYS_PER_RUN)) {
recordsWaiting++;
continue;
}
daysThisRun += days;
capacity.ResourceHeroApp__End_Date__c = LAST_DAY;
capacitiesToUpdate.add(capacity);
changed.add(capacity.ResourceHeroApp__Resource__r.Name + ': ' + dates + ' now ends ' + LAST_DAY.format());
}
}
if (!DRY_RUN) {
if (!capacitiesToUpdate.isEmpty()) {
update capacitiesToUpdate;
}
if (!capacitiesToDelete.isEmpty()) {
delete capacitiesToDelete;
}
}
List<String> summary = new List<String>();
summary.add('');
summary.add('==== End capacity after ' + LAST_DAY.format() + (DRY_RUN ? ' (DRY RUN - nothing changed)' : '') + ' ====');
summary.add('Resources checked: ' + resources.size() + (RESOURCE_NAMES.isEmpty() ? ' (all archived resources)' : ''));
summary.add('Capacity records ' + (DRY_RUN ? 'that would be shortened: ' : 'shortened: ') + capacitiesToUpdate.size() + ', ' + (DRY_RUN ? 'that would be deleted: ' : 'deleted: ') + capacitiesToDelete.size());
for (String note : changed) {
summary.add(' ' + note);
}
summary.add('Records still waiting (run again): ' + recordsWaiting);
summary.add('Names not found: ' + notFound.size());
for (String name : notFound) {
summary.add(' ' + name);
}
System.debug(LoggingLevel.ERROR, String.join(summary, '\n'));
Run the script
Run the script with the steps in Run a script.
Read the summary
The script prints a summary each time it runs:
- Resources checked – The number of resources the script looked at.
- Capacity records shortened, deleted – The number of records shortened to end on
LAST_DAYand the number deleted, with each record listed by resource and dates. In a dry run, these read “that would be shortened” and “that would be deleted”. - Records still waiting (run again) – Records that didn’t fit in this run. Run the script again to change them.
- Names not found – Names in
RESOURCE_NAMESthat don’t match a resource. Check the spelling.
Note:
A deleted RH Capacity record can be restored from the Recycle Bin for 15 days. Resource Hero rebuilds its daily capacity when it’s restored.