This has already been asked and answered and is working except for the errorCase.SuppliedName returns Null after my error message. How do I write a Test Class for this. I'm a newbie and this has helped me tremendously.
trigger PreventCloseServiceRequestTrigger on Case (after update) {
// Find all service request that are being closed
Set<Id> newlyClosedCaseIds = new Set<Id>();
for (Id caseId : Trigger.newMap.keySet()) {
if (Trigger.newMap.get(caseId).IsClosed &&
!Trigger.oldMap.get(caseId).IsClosed) {
newlyClosedCaseIds.add(caseId);
}
}
// For every newly closed service request that has at least one
// not closed task add an error to the Trigger.newMap entry.
// Adding the error to the Trigger.new entry will prevent the save
// and add the error message to the page messages that get displayed on
// the page.
for (AggregateResult aggResult : [
Select Count(Id), WhatId
From Task
Where WhatId In :newlyClosedCaseIds
And IsClosed = false
Group by WhatId
Having Count(Id) > 0
]) {
Id caseId = (Id) aggResult.get('WhatId');
Case errorCase = Trigger.newMap.get(caseId);
// add my error message to the coordinator goes down here
errorCase.addError('Please complete all OPEN TASK before trying to CLOSE this SERVICE REQUEST: ' +
errorCase.SuppliedName);
}
}