You need to sign in to do that
Don't have an account?
Nirupama Sharma
Apex Trigger to avoid Duplicate Records upon creations and updation
Hello all
i am trying to Design a Trigger where it should throw an error upon duplicate record creation, i know this can be achived with Duplicate management, but i have Custom Lookup field(User) which is not showing in the matching rules, Hence i am trying to write trigger, basically i have 3 Custom fields User(Assignedto__c ) which is
Lookup(User) and other fields are
Quater_Year__c which is picklist and Month (
Month__c) which is picklist field,
Please Help with the trigger
i am trying to Design a Trigger where it should throw an error upon duplicate record creation, i know this can be achived with Duplicate management, but i have Custom Lookup field(User) which is not showing in the matching rules, Hence i am trying to write trigger, basically i have 3 Custom fields User(Assignedto__c ) which is
Lookup(User) and other fields are
Quater_Year__c which is picklist and Month (
Month__c) which is picklist field,
Please Help with the trigger
trigger contactDuplicatePreventer on Contact(before insert, before update) { Set<String> setEmailID = new set<String>(); Set<Id> setContID = new set<ID>(); for (Contact Contact : System.Trigger.new) { if ((Contact.Email != null) && (System.Trigger.isInsert || (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) { setEmailID.add(Contact.Email); setContID.add(Contact.id); } } List<Contact> lstCOntact = [select id ,email from contact where email in :setEmailID and id not in :setContID ]; Map<String, Contact> contactMap = new Map<String, Contact>(); for(Contact cont : lstCOntact) { contactMap.put(cont.email, cont); } for (Contact Contact : System.Trigger.new) { if ((Contact.Email != null) && (System.Trigger.isInsert || (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) { if(contactMap.containsKey(Contact.Email)) { Contact.Email.addError('A Contact with this email address already exists.'); } } } }
{
if((trigger.isinsert || trigger.isupdate) && trigger.isbefore)
{
for(Account acc : trigger.new)
{
integer lstaccount=[select count() from Account where name=:acc.Name Limit 1];
if(lstaccount > 0)
{
acc.adderror('duplicated record found with same name');
}
}
}
}