0

I took this code from cookbook but the problem is it's throwing error message before the record gets inserted.Do we have an option of writing an trigger including (Before and After events ).While using the Before Events , it should throw an Error as it is duplicate record and when using the After Events , it should save the Duplicate record and then Close it. Any help is very much appreciated.

Here is the Code for the Before Event :

trigger CaseDuplicatePreventer on Case(before insert, before update) {  
  Map<String, Case> CaseMap = new Map<String, Case>();   
 for (Case c : System.Trigger.new) {                
 // Make sure we don't treat an Subject address that         // isn't changing during an update as a duplicate.              
if ((c.Subject != null) &&               
 (System.Trigger.isInsert || (c.Subject != System.Trigger.oldMap.get(c.Id).Subject))) {                    
 // Make sure another new Case isn't also a duplicate                  
if (CaseMap.containsKey(c.Subject)) {                
c.Subject.addError('Another new Case has the ' + 'same Subject address.');

  } else {                
CaseMap.put(c.Subject, c);           
 }       }    }         
// Using a single database query, find all the Cases in          // the database that have the same Subject address as any          // of the Cases being inserted or updated.         
 for (Case c : [SELECT Email,Subject FROM Case WHERE Subject IN :CaseMap.KeySet()]) {        
Case newCase = CaseMap.get(c.Subject);        
newCase.Subject.addError('A Case with this Subject'  + 'address already exists.'); 

 }
}
0

I think in your first line you need to populate the case map first with a query, so that it can be used in the line: if (CaseMap.containsKey(c.Subject)) ....

Also, what errors are you getting?

  • :Thanks for your reply.This code works fyn for the Before Events.Similarily i would like to have a code for After Events.Where we can include the Before and After Events and execute the code. – nikkey Sep 24 '14 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.