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
RSButtarRSButtar 

code for checking Duplicate Email and Save contact, create Case if contact with Email don't exist

I am designing a VF Page for force.com sites This page is actually a Form to be filled by te user for submitting queries.

Before storing the Contact Record that the user enter in the form, I have to check weather the Email entered by the user already exist. If it exist, then just case will be created and it will be linked with the existing Contact having the same Email Id that the user entered. If the Email entered by the user doesn't exist, I have to create both the Contact and the Case. I need to wriete the logic with in my Apex Class and don't need the trigger for it. I have completed this second part, but don't know how to implement the first one. The code for this second part that creates both the contacts and case without checking the Email is listed below:

 

public class TechnicalServiceController { public String companyName{set; get;}

public String firstName{set; get;}

public String lastName{set; get;}

public String title{set; get;}

public String phoneNumber{set; get;}

public String emailId{set; get;}

public String department{set; get;}

public String firstAddress{set; get;}

public String secondAddress{set; get;}

public String cityOrTown{set; get;}

public String state{set; get;}

public String postalCode{set; get;}

public String country{set; get;} public String typeOfRequest{set; get;}

 

//Fields that goes only to the Case Record

 public String catalogNumber{set; get;}

public String lotNumber{set; get;}

public String productName{set; get;} public String queryBox{set; get;}

 

//This is the code where I am trying to search for Duplicate Records.

List<Contact> oldContacts = [Select Email From Contact];

{ // Can't understand WHY I NEED This Bracket here. If I don't give it, It will give me the error -

//Error: Compile Error: expecting right curly bracket, found 'for' at line 60 column 4

 for(Contact c : oldContacts){ if(c.Email != emailId)

//Here I have to call the method -- public PageReference saveRecords() written below, to save the Contact

//Information if duplicate Email Doesn't exist

saveRecords();//This gives me the error -- System.Exception: DML currently not allowed.

}

}

 

public PageReference saveRecords() {

try

{

Contact contactToStore = new Contact();

contactToStore.FirstName = firstName;

contactToStore.LastName = lastName;

contactToStore.Phone = phoneNumber;

contactToStore.Email = emailId;

contactToStore.MailingStreet = firstAddress;

contactToStore.MailingCity = cityOrTown;

contactToStore.MailingState = state;

contactToStore.MailingPostalCode = postalCode;

contactToStore.MailingCountry = String.valueOf(country);

insert contactToStore;

 

//new Object of type Account Standard Object Account accountToCreate = new Account();

accountToCreate.Legal_Entity__c = String.valueOf(country);

accountToCreate.Name = companyName;

accountToCreate.PostalCode__c = postalCode;

accountToCreate.Name = companyName; insert accountToCreate;

 

Contact tempContact = [Select id from Contact Where id =: contactToStore.Id]; Account tempAccount = [Select id from Account Where id =: accountToCreate.Id];

//new Object of type Case Standard Object

Case caseToCreate = new Case();

caseToCreate.ContactId = contactToStore.id;

caseToCreate.AccountId = tempAccount.id;

caseToCreate.Subject = 'This is the Web To Case';

caseToCreate.Serial_Number__c = catalogNumber;

caseToCreate.Lot_Number__c = lotNumber;

caseToCreate.Product_Code__c = productName;

caseToCreate.Description = queryBox;

insert caseToCreate;

 

return null;

}

 

catch(Exception e)

{

ApexPages.addMessages(e);

return null;

}

 

}

 

 

I have main problem with the following section of the code from above:

//This is the code where I am trying to search for Duplicate Records.

List<Contact> oldContacts = [Select Email From Contact];

{ // Can't understand WHY I NEED This Bracket here. If I don't give it, It will give me the error -

//Error: Compile Error: expecting right curly bracket, found 'for' at line 60 column 4 -- Can anyone tell me why I need this bracket  //here

 for(Contact c : oldContacts){ if(c.Email != emailId)

//Here I have to call the method -- public PageReference saveRecords() written ABOVE, to save the Contact

//Information if duplicate Email Doesn't exist

saveRecords();//This gives me the error -- System.Exception: DML currently not allowed.

}

}

 

Is there anyone who can help me to search for duplicate Email record and save the contact detail if email record don't exists.