+ Start a Discussion
withoutmewithoutme 

Dupe check before contact entry

Hi All

We have a huge issue of duplicates. So I'm creating a contact dupe check VF page. Whenever the user clicks on New, the user has to be directed to this dupe check page, where in after a search, he either skips entering a contact, or continues to the actual create new contact page.

 

I have created the Dupe Check Page (below) with an extension. Now I want to be able to direct the user to the create new page when he clicks continue. I'm unable to understand how to do this. The problem is

 

1. When I replace the New button . On Clicking New, SF takes me to the record type selection page. And then the VF page. Now how do I save the recordtype that the user selected?

 

2. When I click on continue, how do I pass the parameters - First Name, Last Name, Email, Account Name, Record type to the create new page? And most importantly how do I go to the actual create New page?

Any help will be greatly appreciated. Thank you. 

 

<apex:page standardController="Contact" extensions="ContactDupeCheck"><apex:form >

<apex:pageBlock title="Quick Dupe Check">

<apex:pageBlockSection columns="2">

<apex:pageBlockSectionItem >

<apex:outputLabel value="First Name" for="fname"/>

<apex:inputtext value="{!fName}" id="fname"/>

</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem >

<apex:outputLabel value="Last Name" for="lname"/>

 

<apex:inputText value="{!lName}" id="lname"/>

</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem >

<apex:outputLabel value="Email" for="email"/>

<apex:inputtext value="{!eemail}" id="email"/>

</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem >  

<apex:outputLabel value="Account Name" for="accName"/>  

<apex:inputText value="{!aname}" id="accname"/> 

</apex:pageBlockSectionItem>  

</apex:pageBlockSection>  

<apex:commandButton action="{!dupecheck}" value="Find Dupes" rerender="listing" status="status">

<apex:actionSupport event="onchange" rerender="listing"/></apex:commandButton>

<apex:actionstatus id="status" startText="processing...">

<apex:facet name="stop"></apex:facet> </apex:actionstatus>

</apex:pageBlock>

<apex:pageBlock title="Possible Dupes" >

<apex:pageBlockSection id="listing" columns="1">

<apex:pageBlockTable rows="10" value="{!conList}" var="c" border="0" cellspacing="0" cellpadding="10">

<apex:column ><a href = "/{!c.Id}">Click Here</a></apex:column>

<apex:column value="{!c.FirstName}"/>

<apex:column value="{!c.LastName}"/>

<apex:column value="{!c.Email}"/>  

<apex:column value="{!c.Title}"/>  

<apex:column value="{!c.AccountId}"/>  

<apex:column value="{!c.Account.Account_Manager__c}"/>  

<apex:column value="{!c.Account.NBR__c}"/>  

<apex:column value="{!c.Account.Territory__c}"/>  

<apex:column value="{!c.Account.OwnerId}"/>  

</apex:pageBlockTable>  

</apex:pageBlockSection>  

<apex:commandButton action="{!cancel}" value="Cancel"></apex:commandButton>

<apex:commandButton action="{!add}" value="Continue"></apex:commandButton>

</apex:pageBlock>

</apex:form>

</apex:page>

 

 

---Extension

 

 

public without sharing class ContactDupeCheck {

private final Contact cont;

public ContactDupeCheck(ApexPages.StandardController controller){

this.cont = (Contact)controller.getRecord();

}

Public List<Contact> conList = new List<Contact>();

public List<Contact> getConList(){

 

return conList;

}

public void setConList(List<Contact> conList){

this.conList = conList;

}

String aname;

public String getaname(){

 

return aname; }

public void setaname(String aname){

this.aname = aname;

}

String lname;

public String getlname(){

return lname;

}

 

public void setlname(String lname){

 

this.lname = lname;

}

String fname;

	public String getfname(){
		return fname;
	}
	public void setfname(String fname){
		this.fname = fname;
	}	
String eemail;	public String geteemail(){
		return eemail;	
}	public void seteemail(String eemail){
		this.eemail = eemail;	}	
public PageReference DupeCheck(){
		system.debug('IM IN');
		if(conList==null)
			conList = new List<Contact>();
		String querystring = 'select Id, FirstName, LastName, Title, AccountId, Email, Contact.Account.OwnerId, 						Contact.Account.Account_Manager__c, Contact.Account.NBR__c, Contact.Account.Territory__c from Contact where ';				String lnamefilter = 'LastName LIKE \'%' + String.escapeSingleQuotes(lName) + '%\'';		
		String emailfilter = 'Email LIKE \'%' + String.escapeSingleQuotes(eemail) + '%\'';		
		String fnamefilter = 'FirstName LIKE \'%' + String.escapeSingleQuotes(fName) + '%\'';		
		String anamefilter = ' Contact.Account.Name Like \'%' + String.escapeSingleQuotes(aname) + '%\'';		
		String conqstring  = querystring;		
		Set<Id> cids = new Set<Id>();		
		if(eEmail.length()!=0){			
			System.debug('IN E');			
			conqstring+=emailfilter;			
			conList = Database.query(conqstring);			
			for(Contact c :conList)				
			cids.add(c.Id);		}			
	if(aname.length()!=0 && lName.length()!=0 && fName.length()!=0){
			System.debug('IN A');			
			querystring+=anamefilter+' and '+ lnamefilter+' and ' + fnamefilter;
			for(Contact c :Database.query(querystring)){
				if(!cids.contains(c.Id)){
					conList.add(c);	
				cids.add(c.Id);		
		}	
		}		
}		
else{			
		System.debug('IN N');			
		if(lName.length()!=0 && fName.length()!=0){
				querystring+=lnamefilter + ' and ' + fnamefilter;
				for(Contact c :Database.query(querystring))
					if(!cids.contains(c.Id)){
					conList.add(c);	
				cids.add(c.Id);	
 
}			
}		
}								
		System.debug('FINAL QUERY STRING'+querystring);				
return null;	
}	
public PageReference add(){
		//PageReference rtpage = new PageReference();		
	Map<String,String> BaseURL = ApexPages.currentPage().getHeaders();
		for(String s :BaseUrl.keyset())			
System.debug('BaseUrl::::::::'+BaseUrl.get(s));		
return null;	

} 

 

 

Message Edited by withoutme on 07-30-2009 02:25 AM
Message Edited by withoutme on 07-30-2009 02:30 AM
wesnoltewesnolte

Hey

 

I'll use your add/continue button as an eg.

 

The method would need to look like this

 

public PageReference add(){

  PageReference pr = System.Page.MyPageName;

pr.getParameters().put('fName',fname);

// pr.getParameters().put.... etc.

 

// other logic

 

return pr; 

 

 

withoutmewithoutme

Thanks for the quick response. where in here am I saying "Create New COntact". I don't see any declaration for that when you are instantiating the pagereference variable. What about record  types?

 

Also, where do I find the list of keys in the getParameters() map? 

Message Edited by withoutme on 07-30-2009 04:54 AM
wesnoltewesnolte

Hey

 

I was just showing you the basics so that could build on them. I'd suggest that your methods that write lastname, firstname etc work directly with the contact property you have declared. This could be done like this,

 

at the top change your declaration of cont to this

 

public Contact cont{get;set;} //variable is now accessible by page

 

next remove all your methods that get fields of the contact e.g getFname, getLname etc. They are unnecessary and make you code bulky.

 

Also change code like this

 

String querystring = 'select Id, FirstName, LastName, Title, AccountId, Email, Contact.Account.OwnerId, 						Contact.Account.Account_Manager__c, Contact.Account.NBR__c, Contact.Account.Territory__c from Contact where ';				String lnamefilter = 'LastName LIKE \'%' + String.escapeSingleQuotes(lName) + '%\'';		

 to this

 

String querystring = 'select Id, FirstName, LastName, Title, AccountId, Email, Contact.Account.OwnerId, 						Contact.Account.Account_Manager__c, Contact.Account.NBR__c, Contact.Account.Territory__c from Contact where ';				String lnamefilter = 'LastName LIKE \'%' + String.escapeSingleQuotes(cont.lastname) + '%\'';		

 


Then change your visualforce page to interact with the contact directly, eg.

 

Change this

 

<apex:pageBlockSectionItem >

<apex:outputLabel value="First Name" for="fname"/>

<apex:inputtext value="{!fName}" id="fname"/>

</apex:pageBlockSectionItem>

 to this

<apex:inputField value="{!cont.firstname}"/> <!--all formatting and labelling in one line! -->

 

Now that your code is a bit cleaner I would implement the add method something like this

 

public PageReference add(){

  PageReference pr = System.Page.MyPageName;

pr.getParameters().put('fName',cont.firstname); // adds the key-value pair fname=whatever to the URL

// pr.getParameters().put.... etc.

 

// other logic

 

return pr;  // once the add method has completed it returns this page reference which forces the page to move to this url (including parameters)

 

}

 

I may not understand your requirements completely. From you message the flow of what you're tryign to achieve is something like this,

 

1. On a contact view page click a new button.

2. The new button will direct the user to this duplicate checking page?

3. The user then enters firstname, lastname etc and checks for duplicates?

4. If they want to skip the contact creation they do that, otherwise they click continue?

5. Clicking continue adds the currently captured contact details to the URL parameter list of th next page.

6. The next page reads the params and populates the 'new contact' form in part.

 

Is this more or less what you'r trying to acheive?

 

Wes

 

withoutmewithoutme

Wes,

Thank you for the detailed explanation. Couple of things

1. The reason I used inputtext instead of inputfield that binds to the cont variable in extension: last name is a required field that I cannot change. Account is a lookup - I want to provide a text interface as everyone do NOT have access to all accounts - we have on demand sharing using apex. Hence the use of text fields to grab inputs.

 

2. Your description of flow is right. But when you override the new button in SF with a VF page. Salesforce changes the flow to

 Click new --> Pick Record Type --->VF page.

 

3. How do I get the record type that the user selected before entering the VF page? - I need this info so that, if the user decides to create a contact (no dupes) I need to pass that to the create new page.

 

4. Using fname or lname doesn't work - This will not get passed to the Create new page. I looked into the "View source" of the page and they use name_firstcon2. So here is my add() method - and it works great

 

public PageReference add(){ Schema.DescribeSObjectResult r = Contact.sObjectType.getDescribe(); PageReference ref = new PageReference('/' + r.getKeyPrefix() + '/e'); if(fname.length()!=0) ref.getParameters().put('name_firstcon2',fname); if(lname.length()!=0) ref.getParameters().put('name_lastcon2',lname); if(eemail.length()!=0) ref.getParameters().put('con15',eemail); if(aname.length()!=0) ref.getParameters().put('con4',aname); System.debug('PR IS '+ref); return ref; }

 Now the issue is - what was the record type the user selected? If its not possible, I could recreate another RT page  - before I direct the suer to the create new page.

Thanks again. 

 

meerameera

Hi ,

 

In your controller you can get the recordType selected by the user using:

 

recordType=System.currentPageReference().getParameters().get('RecordType');       

 

And when the user needs to create a new contact(no dupes), you can pass this recordType as pa parameter in tha same way as you are passing other parameters.

 

ie ref.getParameters().put('RecordType',recordType);

 

Hope this will help you.

 

 

withoutmewithoutme

I have hit into a problem I did not think about.

 

When you Override the New button with your VF page, and try to redirect the user to the create new page - SF just redirects back to your quick dupe checkpage. It does not go to the original SF UI create new page!!!!

I rellay dont want to recreate the page - cos we have loads and loads of fields.

ANy help? 

meerameera

If you are passing value 1 to the parameter nooverride, that will get solved.

 

ref.getParameters().put('nooverride','1');

withoutmewithoutme

That worked beautifuly. Thank you so much. 

 

I'm writing my testmethod right now. Would this be the right way to go abt. I'm getting the error saying "Argument Cannot be Null". I think the problem is in setting the extension parameter value ( = VF page form field values) and passing them from the page to the extension and then retrieving it in the testMethod.

 

private class ContactDupeCheckTest { static testMethod void myUnitTest() { // TO DO: //1. Create a few dupes //2. insert them //3. create an instance of the vf page //4. pass IDs?? //5. instantiate extension //6. create new contact //7. call extension methods //8. delete contacts created Account a = new Account(Name = 'ABC Corp'); insert a; List<Contact> contlist = new List<Contact>(); for(Integer i=0; i<5; i++){ contlist.add(new Contact(FirstName = 'ABC'+i, LastName = '123', Email = 'abc.123@abc123.com', Phone = '12345678', Title='King of Queens'+i, AccountId = a.id)); } contList.add(new Contact(FirstName = 'XYZ',LastName='456', Phone = '87654321', Title='Queen of Kings',AccountId = a.id, email='123@xyz.com')); insert contList; PageReference testpage = Page.ContactDupeCheck; Test.setCurrentPageReference(testpage); ApexPages.currentPage().getParameters().put('fname','AB'); ApexPages.currentPage().getParameters().put('lname','23'); ApexPages.currentPage().getParameters().put('eemail','abc.123@abc123.com'); ApexPages.currentPage().getParameters().put('aname','ABC Corp'); ApexPages.StandardController sc = new ApexPages.StandardController(new Contact()); ContactDupeCheck ext = new ContactDupeCheck(sc); ext.DupeCheck(); System.assertEquals(ext.conList.size(),contlist.size()-1); } }