Can any one help me out.I shall appreciate your help.
I have trigger written for the Duplicate case to be closed once it is identified as a duplicate with same subject line.But my trigger throws an
ERROR : as Error: Invalid Data. Review all error messages below to correct your data.Apex trigger Case_DuplicateClose caused an unexpected exception, contact your administrator:Case_DuplicateClose: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.Case_DuplicateClose: line 30, column 1.
If i just comment the set id line :it throws an ERROR for my first case itself.I think Somewhere my trigger is wrong
CODE :
trigger Case_DuplicateClose on case (after insert)
{
Map<String, List<Case>> subjectToCaseList = new Map<String, List<Case>>();
//set<id> CaseID = new set<id>();
for(Case c : trigger.new)
{ //CaseID.add(c.id);
// System.debug('@@@@@@@@@@@@@@@@@@@@@@@' + );
if(subjectToCaseList.get(c.Subject) != null)
{
subjectToCaseList.get(c.Subject).add(c);
}
else
{
subjectToCaseList.put(c.Subject, new List<Case> {c});
}
}
// any results from this query are dupes based solely on subject
for(Case existingCase : [select Id, Subject from Case where Subject in : subjectToCaseList.keySet()])
{
for(Case newCase : subjectToCaseList.get(existingCase.Subject))
{
newCase.addError('This case is a duplicate.');
//newCase.Status = 'Closed';
}
}
}