You need to sign in to do that
Don't have an account?
tulasiram ch
How to restrict null values taking as Duplicates
When i am trying to display error message when user try to insert/update a phone field with duplicate values code is fine but , when i try to update a phone field with null value its giving error as this phone already exists...what can i do for solving this error...
Code: trigger duplicatePhoneOnCandidate on Candidate__c (before insert ,before update)
{
Map<String, Candidate__c> candidateMap = new Map<String, Candidate__c>();
Set<Id> setId = new Set<ID>();
for(Candidate__c candidates: System.Trigger.new)
{
setId.add(candidates.Id);
if( (candidates.Phone__c != null && System.Trigger.isInsert ) ||
( System.Trigger.isUpdate && candidates.Phone__c != System.Trigger.oldMap.get(candidates.Id).Phone__c ) )
{
if(candidateMap.containsKey(candidates.Phone__c))
{
candidates.Phone__c.addError('Phone Already exists');
}
else
{
candidateMap.put(candidates.Phone__c, candidates) ;
}
}
}
// Using a single database query, find all the Candidate in
// the database that have the same Phone address as any
// of the Candidate being inserted or updated.
for ( Candidate__c can : [SELECT Phone__c FROM Candidate__c WHERE Phone__c IN :candidateMap.KeySet() and Id not in :setId ] )
{
Candidate__c newCan = candidateMap.get(can.Phone__c);
newCan.Phone__c.addError('Candidate record have already this pone number.');
}
}
Code: trigger duplicatePhoneOnCandidate on Candidate__c (before insert ,before update)
{
Map<String, Candidate__c> candidateMap = new Map<String, Candidate__c>();
Set<Id> setId = new Set<ID>();
for(Candidate__c candidates: System.Trigger.new)
{
setId.add(candidates.Id);
if( (candidates.Phone__c != null && System.Trigger.isInsert ) ||
( System.Trigger.isUpdate && candidates.Phone__c != System.Trigger.oldMap.get(candidates.Id).Phone__c ) )
{
if(candidateMap.containsKey(candidates.Phone__c))
{
candidates.Phone__c.addError('Phone Already exists');
}
else
{
candidateMap.put(candidates.Phone__c, candidates) ;
}
}
}
// Using a single database query, find all the Candidate in
// the database that have the same Phone address as any
// of the Candidate being inserted or updated.
for ( Candidate__c can : [SELECT Phone__c FROM Candidate__c WHERE Phone__c IN :candidateMap.KeySet() and Id not in :setId ] )
{
Candidate__c newCan = candidateMap.get(can.Phone__c);
newCan.Phone__c.addError('Candidate record have already this pone number.');
}
}
with
Let me know if this solves your issue.