function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kishore cmxkishore cmx 

In contact object i enter the last name field is kiran and Email is kiran123@gmail.com , and next time i enter the same name and email so this type of combinations not repeated? How can i write a validation rule or Workflow?

Best Answer chosen by kishore cmx
SantoshChitalkarSantoshChitalkar
trigger dupContactPreventer on Contact (After insert, After update){

      List<Contact> lstContact = [Select Id, LastName, Email from Contact];
      for(Contact con : trigger.New){
             for(Contact c : lstContact){
                   if(con.LastName == c.LastName && con.Email == c.Email)
                             con.Email.addError('Duplicate Found');
             }
       }

}

All Answers

pankaj kabra 8pankaj kabra 8
You cannot achieve this using workflow or validation rule. You have to write a trigger on contact object and there you can add whatever logic you want.
kishore cmxkishore cmx
Hi pankaj,
can you plz Write a trigger?
 
kishore cmxkishore cmx
Hi pankaj,
my question is Once i enter the last name and Email (last name=raju ,email=raju123@gmail.com) next time i enter the same values it will show error (Not repeated the same time of combinations, Suppose last name is raju Email is different ,Email is Raju123@gmail.com Last name  is different)? how to write validation rule or Workflow?
kishore cmxkishore cmx
*Hi pankaj,my question is Once i enter the last name and Email (last name=raju ,email=raju123@gmail.com ) next time i enter the same values it will show error (Not repeated the same time of combinations, Suppose last name is raju Email is different ,Email is Raju123@gmail.com Last name is different)? how to write validation rule or Workflow?*
pankaj kabra 8pankaj kabra 8
You have to code it on your own. Nobody will be giving you the complete code. Attempt it and if you stcuk anywhere feel free to ask.
Mudasir WaniMudasir Wani
Hi Kishore,

For this purpose you need to fetch the existing records that is the reason you cannot do it in validation rule.
If you donot want duplicate records based on one field you can make that field unique at field level.
Hope this makes sense.

Let us know if you have any question.
Your trigger logic should be on before insert logic and you need to fetch all the records whose name amd email matches your criteria.

Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
SantoshChitalkarSantoshChitalkar
trigger dupContactPreventer on Contact (After insert, After update){

      List<Contact> lstContact = [Select Id, LastName, Email from Contact];
      for(Contact con : trigger.New){
             for(Contact c : lstContact){
                   if(con.LastName == c.LastName && con.Email == c.Email)
                             con.Email.addError('Duplicate Found');
             }
       }

}
This was selected as the best answer
Mudasir WaniMudasir Wani
Hi Kishore,
 
Your Trigger

trigger contactTrigger on Contact (beforeinsert, beforeupdate){
        String errorMessage = '';     
        if(Trigger.IsInsert){
          errorMessage = contactHandler.handleBeforeInsert(trigger.new);
        if(trigger.IsUpdate)
          errorMessage = contactHandler.handleBeforeUpdate(trigger.new);
        if(errorMessage == 'Duplicate'){
          //Add error here 
        }

}
public class contactHandler{

	public static String handleBeforeInsert(List<Contact> contactListToInsert){
         Set<String> lastNameSet = new Set<String>();
         Set<String> emailSet = new Set<String>();
         for(Contact conRec : contactListToInsert){
          if(conRec.lastName != null) 
           lastNameSet.add(conRec.lastName);
           if(conRec.email!= null)
           emailSet.add(conRec.email);
        }
		String errorString = '';
		if((lastNameSet != null && lastNameSet.size() > 0) && (emailSet != null && emailSet.size() > 0))
			errorString = checkDuplicateRecord(lastNameSet,lastNameSet,contactListToInsert);
		return errorString;
    }
	public static string handleBeforeUpdate(List<Contact> contactListToInsert){
         Set<String> lastNameSet = new Set<String>();
         Set<String> emailSet = new Set<String>();
         for(Contact conRec : contactListToInsert){
          if(conRec.lastName != null) 
           lastNameSet.add(conRec.lastName);
           if(conRec.email!= null)
           emailSet.add(conRec.email);
        }
		String errorString ='';
		if((lastNameSet != null && lastNameSet.size() > 0) && (emailSet != null && emailSet.size() > 0))
			errorString = checkDuplicateRecord(lastNameSet,lastNameSet,contactListToInsert);
		return errorString;	
    }
	public static String checkDuplicateRecord(Set<String> lastNameSet,Set<String> emailSet , List<Contact> contactListToInsert){
		String errorString ='';
		List<Contacts> existingConList = [Select id, lastName, email from Contact where lastName IN :lastNameSet and email IN : emailSet ];
		for(Contact con : contactListToInsert){	
			for(Contact existingConRec : existingConList){
			if(con.LastName == existingConRec.LastName && con.Email == existingConRec.Email)
				errorString = 'Duplicate';
			}
		}
		return errorString;
	}
}

Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
 
surasura
Simply create a custom field called unique check or any name you prefer of type text and mark that field as unique (tick unique check box) . then define and workflow rule with filed update action to populate unique check field with value lastname + ' '+Email.
done 
SantoshChitalkarSantoshChitalkar
Hi Kishor,

Sura's answer is best solution for your requirement.

Santosh
Mudasir WaniMudasir Wani
Hello Guys,

It can't be achieved using validation rule any ways because we need to compare it with existing records.

Also the best practice to check the validation is on before insert and before update trigger actions.

Thanks.