This code uses triggers to check for duplicate gift card numbers being entered. It collects all gift card numbers from the incoming records, queries the database for existing records with those numbers, and maps them to check for duplicates. Any duplicate numbers found will add an error to the incoming record.
2. Here’s the code to check for Dups;this can be extended as needed.
// Before Insertcheck for Dups
if (Trigger.isbefore) {
if (Trigger.isInsert || Trigger.isUpdate) {
//Start of Dup Check
List<String> uniqueValueList = new List<Id>();
// Get listof all CardNums coming thru Trigger
for (Related_Gift_Cards__c RGC:Trigger.new) {
if (RGC.Gift_Card__c !=null) {
uniqueValueList.add(RGC.Gift_Card__c); ==========Get Listof all Incoming Card Nums
}
}
// Get listof all CardNums coming in againstcard existing in the system and compare
List<Related_Gift_Cards__c> RGCList= [selectid,Gift_Card__c from Related_Gift_Cards__c
where Gift_Card__c IN :uniqueValueList]; ===== Create Listof used CardNums from the Inbound List
Map<String,Related_Gift_Cards__c> uniqueValueMap = new Map<String,Related_Gift_Cards__c>();
for(Related_Gift_Cards__c CVal : RGCList){ ====Create a Map from the above List
uniqueValueMap.put(CVal.Gift_Card__c,CVal );
}
for(Related_Gift_Cards__c CDup : Trigger.new){ ==== Compare Incoming Nums againstused Number for
Dups
if(uniqueValueMap.containsKey(CDup.Gift_Card__c)) {
if( CDup.id<>uniqueValueMap.get(CDup.Gift_Card__c).id){
CDup.adderror('This card has been used.Please selectanother card.');
}
}