+ Start a Discussion
Denise MeinershagenDenise Meinershagen 

Need help creating an Apex Trigger to check for duplicate cases.

Custom field "global id" is unique identifier. Specific case record type.
Best Answer chosen by Denise Meinershagen
MissedCallMissedCall
Hello Denise,

To get you started on your requirement, here is the code snippet. Play with it and let me know if you have any issues.
(Assumption: Global_Id__c is a unique custom field of type Id on case object).
Trigger:
trigger CasesTrigger on Case (before insert, before update) {
    
    if(Trigger.isBefore){
		if(Trigger.isInsert){
			CaseDupeChecker.caseDupeCheck(Trigger.new);
		}
		else if(Trigger.isUpdate){
			CaseDupeChecker.caseDupeCheck(Trigger.new);
		}
	}
}
Class:
public class CaseDupeChecker {

	public static void caseDupeCheck(List<Case> cases){
		map <Id, Case> caseMap = new map <Id, Case>();
		
		for(Case case: cases){
			if(case.Global_Id__c != null){				
				if(caseMap.containsKey(case.Global_Id__c)){
					case.addError('Duplicate Case found in batch.');
				} else {
					caseMap.put(case.Global_Id__c, case);
				}
			}
		}
		
		if(!caseMap.isEmpty()){
			//Include the required fields on case object in order for the below query to execute successfully 
			list <Case> lstCas = [SELECT Id, Global_Id__c 
										  FROM Case
										  WHERE Global_Id__c IN: caseMap.keySet()];
			
			for (Case cas: lstCas){
				if(caseMap.containsKey(cas.Global_Id__c) && (cas.Id != caseMap.get(cas.Global_Id__c).Id)){
					caseMap.get(cas.Email__c).addError('Duplicate Case found in Salesforce: '+cas.Id);
				}
			}
		}
		
	}
}
Cheers! Have fun.

 

All Answers

Balaji BondarBalaji Bondar
Hi Denise,

Below link will help you out to create trigger on the same lines.Please update the trigger logic based on your business requirement.
http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving
MissedCallMissedCall
Hello Denise,

To get you started on your requirement, here is the code snippet. Play with it and let me know if you have any issues.
(Assumption: Global_Id__c is a unique custom field of type Id on case object).
Trigger:
trigger CasesTrigger on Case (before insert, before update) {
    
    if(Trigger.isBefore){
		if(Trigger.isInsert){
			CaseDupeChecker.caseDupeCheck(Trigger.new);
		}
		else if(Trigger.isUpdate){
			CaseDupeChecker.caseDupeCheck(Trigger.new);
		}
	}
}
Class:
public class CaseDupeChecker {

	public static void caseDupeCheck(List<Case> cases){
		map <Id, Case> caseMap = new map <Id, Case>();
		
		for(Case case: cases){
			if(case.Global_Id__c != null){				
				if(caseMap.containsKey(case.Global_Id__c)){
					case.addError('Duplicate Case found in batch.');
				} else {
					caseMap.put(case.Global_Id__c, case);
				}
			}
		}
		
		if(!caseMap.isEmpty()){
			//Include the required fields on case object in order for the below query to execute successfully 
			list <Case> lstCas = [SELECT Id, Global_Id__c 
										  FROM Case
										  WHERE Global_Id__c IN: caseMap.keySet()];
			
			for (Case cas: lstCas){
				if(caseMap.containsKey(cas.Global_Id__c) && (cas.Id != caseMap.get(cas.Global_Id__c).Id)){
					caseMap.get(cas.Email__c).addError('Duplicate Case found in Salesforce: '+cas.Id);
				}
			}
		}
		
	}
}
Cheers! Have fun.

 
This was selected as the best answer
Denise MeinershagenDenise Meinershagen
Thank you for the suggestion..I'm trying now, but getting an error under the class, line 1. "public class". error is unexpected token: public. How do I fix?
MissedCallMissedCall
You need to create a separate trigger and an apex class.

 
Denise MeinershagenDenise Meinershagen
Ahh. That makes sense. For the line { for(Case case: cases) is this where I indicate the case record type?
MissedCallMissedCall
Change line 7 of you class from
if(case.Global_Id__c != null)
to
if(case.RecordTypeId = 'your18DigitRecrodTypeId' && case.Global_Id__c != null)