You need to sign in to do that
Don't have an account?
tulasiram ch
I wrote a trigger for posting an error message when user try to insert/update phone field with duplicate values those were already existed...
Insertion throwing error working fine but updation is not working can anyone give me a solution...
trigger duplicatePhoneOnCandidate on Candidate__c (before insert)
{
Map<String, Candidate__c> candidateMap = new Map<String, Candidate__c>();
for(Candidate__c candidates: System.Trigger.new)
{
// Make sure we don't treat an Phone address that isn't changing during an update as a duplicate.
if(candidates.Phone__c != null && (System.Trigger.isInsert && System.Trigger.isUpdate || (candidates.Phone__c != System.Trigger.oldMap.get(candidates.Id).Phone__c)))
//if(candidates.Phone__c != null && System.Trigger.isInsert)
{
// Make sure another new Candidate isn't also a duplicate
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()]) {
Candidate__c newCan = candidateMap.get(can.Phone__c);
newCan.Phone__c.addError('Candidate record have already this pone number.');
}
}
trigger duplicatePhoneOnCandidate on Candidate__c (before insert)
{
Map<String, Candidate__c> candidateMap = new Map<String, Candidate__c>();
for(Candidate__c candidates: System.Trigger.new)
{
// Make sure we don't treat an Phone address that isn't changing during an update as a duplicate.
if(candidates.Phone__c != null && (System.Trigger.isInsert && System.Trigger.isUpdate || (candidates.Phone__c != System.Trigger.oldMap.get(candidates.Id).Phone__c)))
//if(candidates.Phone__c != null && System.Trigger.isInsert)
{
// Make sure another new Candidate isn't also a duplicate
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()]) {
Candidate__c newCan = candidateMap.get(can.Phone__c);
newCan.Phone__c.addError('Candidate record have already this pone number.');
}
}
Let us know if this will help you
All Answers
trigger duplicatePhoneOnCandidate on Candidate__c (before insert ,before update)
Let us know if this will help you
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger duplicatePhoneOnCandidate caused an unexpected exception, contact your administrator: duplicatePhoneOnCandidate: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.duplicatePhoneOnCandidate: line 9, column 1
And can You tell me How it works :
if( (candidates.Phone__c != null && System.Trigger.isInsert ) || ( System.Trigger.isUpdate && candidates.Phone__c != System.Trigger.oldMap.get(candidates.Id).Phone__c ) )
we are inserting candidates then why we are checking that statement with candidates.Id
and what it means Trigger.oldMap.get(candidates.id).Phone__c
Hi tulasiram,
Below is the code, which will help you:)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Your task in only as finding the duplicate means you can use the below code.
trigger CandidateTrigger on Candidate__c (before insert, before update) {
Map<string,id> candidateMap = new Map<string, id>();
set<string>candidatephoneSet = new set<string>();
for(Candidate__c Candidate :Trigger.new){
if(Candidate.CandidatePhone__c !=null){
candidatephoneSet.add(Candidate.CandidatePhone__c);
}
}
for(Candidate__c Can :[SELECT id,Name,CandidatePhone__c FROm Candidate__c WHERE CandidatePhone__c In : candidatephoneSet]){
candidateMap.put(Can.CandidatePhone__c, Can.Id);
}
for(Candidate__c candidateNew : Trigger.new){
if(candidateMap.containsKey(candidateNew.CandidatePhone__c) && candidateMap.get(candidateNew.CandidatePhone__c) !=candidateNew.Id){
candidateNew.CandidatePhone__c.addError('Already Phone Exist');
}
}
}
Please let me know if still facing any issues.
Thanks
Dinesh
Also, I have seen Amit Chaudhary code it also working.Please add the event before the update.
I have added my code for the simple for finding the duplication.
Please let me know if this will help or not.
Thanks
Dinesh
Hi TulsiRam,
Amit code will work best in cas eof update operation, but when you will try for insert opeartion, It will give null pointer exception error, as Record Id is not genertaed yet in before insert operation and you are trying to check "candidates.Id" in logic, That's why It gives null pointer exception.
Please let me know, in case of any help :)
Thanks
Even i tested above code which is working fine for me
Please try below code for email and Phone
Let us know if this will helps you
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger duplicateTriggeronCandidate caused an unexpected exception, contact your administrator: duplicateTriggeronCandidate: execution of BeforeInsert caused by: System.StringException: Invalid id: (996) 565-6565: External entry point