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
Shreya JainShreya Jain 

I have a custom object Staging__c with fields Firstname__c, Lastname__c, PostalCode__c . Now, I have to check or search for existing contacts with matching FirstName,Lastname,PostalCode.

I have a custom object Staging__c with fields Firstname__c, Lastname__c, PostalCode__c . Now, I have to check or search for existing contacts with matching  FirstName,Lastname,PostalCode. If contact not found , then we have to create a contact. This have to be done in Batch Class.
Abhishek BansalAbhishek Bansal
Hi Shreya, 

You can follow the steps mentioned below to achieve your requiremnet:

1. Create a set of String that will hold the Contact First Name + Last Name + Postal Code. For Eg:
Set<String> setOfContactInformation = new Set<String>();
for(Contact con : [Select FirstName, LastName, PostalCode from Contat]){
    String conInformation = con.FirstName + con.LastName + con.PostalCode;
    setOfContactInformation.add(conInformation);
}
2. In the start method of your batch class, you have to query all the Staging records.
3. In the exectute method, you need to if the setOfContactInformation contains the information from the staging record then skip it else create a contact record. For Eg:
List<Contact> listOfNewContacts = new List<Contact>();
Contact newContact;
for(Staging__c staging : scope){
	String stagingInformation = staging.Firstname__c + staging.Lastname__c + staging.PostalCode__c ;
	if(!setOfContactInformation.contains(stagingInformation)) {
		newContact = new Contact();
		newContact.Firstname = staging.Firstname__c;
		newContact.Lastname = staging.Lastname__c;
		newContact.PostalCode = staging.PostalCode__c;
		listOfNewContacts.add(newContact);
	}
}

if(listOfNewContacts.size() > 0 ){
	insert listOfNewContacts;
}

Please let me know if you need any further help or information on this.

Thanks,
Abhishek Bansal.
Shreya JainShreya Jain
Thanku @Abhishek Bansal
Abhishek BansalAbhishek Bansal

Hi Shreya,

Please close this question if the solution worked for you.

Thanks,
Abhishek Bansal.