SlideShare a Scribd company logo
1 of 2
Download to read offline
Checking for Dups using Triggers
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.');
}
}

More Related Content

What's hot

Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful softwareJorn Oomen
 
SPFx: Working with SharePoint Content
SPFx: Working with SharePoint ContentSPFx: Working with SharePoint Content
SPFx: Working with SharePoint ContentVladimir Medina
 
SPFx working with SharePoint data
SPFx working with SharePoint dataSPFx working with SharePoint data
SPFx working with SharePoint dataVladimir Medina
 
Modern JavaScript Engine Performance
Modern JavaScript Engine PerformanceModern JavaScript Engine Performance
Modern JavaScript Engine PerformanceCatalin Dumitru
 
Meta programming
Meta programmingMeta programming
Meta programmingantho1404
 
Hidden in plain site – joomla! hidden secrets for code monkeys
Hidden in plain site – joomla! hidden secrets for code monkeysHidden in plain site – joomla! hidden secrets for code monkeys
Hidden in plain site – joomla! hidden secrets for code monkeysNicholas Dionysopoulos
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Colin O'Dell
 
Inventory aging report using oracle discoverer desktop
Inventory aging report using oracle discoverer desktopInventory aging report using oracle discoverer desktop
Inventory aging report using oracle discoverer desktopAhmed Elshayeb
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to ProductionMark Baker
 
Perl gui
Perl guiPerl gui
Perl guiumnix
 
TestFest - Respect\Validation 1.0
TestFest - Respect\Validation 1.0TestFest - Respect\Validation 1.0
TestFest - Respect\Validation 1.0Henrique Moody
 
The Truth About Lambdas in PHP
The Truth About Lambdas in PHPThe Truth About Lambdas in PHP
The Truth About Lambdas in PHPSharon Levy
 
Open Selector
Open SelectorOpen Selector
Open Selectorjjdelc
 

What's hot (20)

Sql-Injection
Sql-InjectionSql-Injection
Sql-Injection
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful software
 
SPFx: Working with SharePoint Content
SPFx: Working with SharePoint ContentSPFx: Working with SharePoint Content
SPFx: Working with SharePoint Content
 
SPFx working with SharePoint data
SPFx working with SharePoint dataSPFx working with SharePoint data
SPFx working with SharePoint data
 
Modern JavaScript Engine Performance
Modern JavaScript Engine PerformanceModern JavaScript Engine Performance
Modern JavaScript Engine Performance
 
Meta programming
Meta programmingMeta programming
Meta programming
 
Hidden in plain site – joomla! hidden secrets for code monkeys
Hidden in plain site – joomla! hidden secrets for code monkeysHidden in plain site – joomla! hidden secrets for code monkeys
Hidden in plain site – joomla! hidden secrets for code monkeys
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016
 
Formal methods
Formal methods Formal methods
Formal methods
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Inventory aging report using oracle discoverer desktop
Inventory aging report using oracle discoverer desktopInventory aging report using oracle discoverer desktop
Inventory aging report using oracle discoverer desktop
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to Production
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Perl gui
Perl guiPerl gui
Perl gui
 
TestFest - Respect\Validation 1.0
TestFest - Respect\Validation 1.0TestFest - Respect\Validation 1.0
TestFest - Respect\Validation 1.0
 
The Truth About Lambdas in PHP
The Truth About Lambdas in PHPThe Truth About Lambdas in PHP
The Truth About Lambdas in PHP
 
Open Selector
Open SelectorOpen Selector
Open Selector
 

Check Dups Triggers

  • 1. Checking for Dups using Triggers
  • 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.'); } }