I have an apex class that brings up duplicates when a user enters in an account name in the search box on the visualforce page. Right now, the search takes the exact letters written into the search text box. What I would like to happen is to have the word being searched on be somewhere in the name of the account and not necessarily starts with the word. I would also like to put conditions in so that if a user enters a name and a street address it will search on both of them combined and bring up all accounts that have the address or the name. Thanks!
Full Class code below:
public class AccountDupeCatch {
public boolean errormsg=false;
public String AccountId=System.currentPageReference().getParameters().get('id');
public String aname;
public List<Account> chkdup;
public Account newaccount= new Account();
public AccountDupeCatch(ApexPages.StandardController controller) {
}
public account getnewaccount() {
return newaccount;
}
public account getexistingaccount() {
if (chkdup!= NULL)
{
if(chkdup.size()>0)
return chkdup[0];
else
return null;
}
else
return null;
}
public list<account> getexistingaccountlist()
{
if (chkdup!= NULL)
{
if(chkdup.size()>0)
return chkdup;
else
return null;
}
else
return null;
}
public pagereference Next() {
chkdup=[Select name,id,recordtype.name,CustomerMasterID__c,shippingstreet,shippingcity,shippingstate from Account where Name=:newaccount.name];
if(chkdup.size()==0)
{
String aname=newaccount.Name;
Pagereference newaccount1 = new Pagereference('/001/e?nooverride=true&acc2='+aname+'');
return newaccount1;
}
else
{
errormsg=true;
return null;
}
}
public pagereference SelectName() {
Pagereference chkdup = new Pagereference('/' + chkdup[0].Id);
return chkdup;
}
public boolean geterrormsg() {
return errormsg;
}
}
Visualforce Page:
<apex:page standardController="Account" extensions="AccountDupeCatch">
<apex:pageBlock title="Account Name">
<apex:form >
Enter the Account Name Here
<apex:inputField value="{!NewAccount.Name}"/>
<apex:commandButton action="{!Next}" value="click here to proceed" status="status"/>
<!-- Display error message -->
<apex:pagemessage strength="2" title="Duplicate!!" severity="error" detail="This Account Name Already Exists !!!" rendered="{!errormsg}"/><!-- End of error message -->
<apex:pageblocktable rendered="{!NOT(ISNULL(existingaccountlist))}" value="{!existingaccountlist}" var="acct">
<apex:column headervalue="Select">
<apex:commandlink action="{!SelectName}">
<input type="radio" name="AccountSel" onclick="changeValue(this,'{!$Component.RadioButtonValue}')"/>
</apex:commandlink>
</apex:column>
<apex:column headervalue="Account Name"> <apex:outputtext value="{!acct.Name}"/> </apex:column>
<apex:column headervalue="Type"> <apex:outputtext value="{!acct.recordtype.name}"/> </apex:column>
<apex:column headervalue="Customer ID"> <apex:outputtext value="{!acct.CustomerMasterID__c}"/> </apex:column>
<apex:column headervalue="Shipping Street"> <apex:outputtext value="{!acct.shippingstreet}"/> </apex:column>
<apex:column headervalue="City"> <apex:outputtext value="{!acct.shippingcity}"/> </apex:column>
<apex:column headervalue="State"> <apex:outputtext value="{!acct.shippingstate}"/> </apex:column>
</apex:pageblocktable>
</apex:form>
Attribution to: NewbieDeveloper27
Possible Suggestion/Solution #1
You can easily change
[Select name,id,recordtype.name,CustomerMasterID__c,shippingstreet,shippingcity,shippingstate from Account where Name=:newaccount.name];
to
Database.query('Select name,id,recordtype.name,CustomerMasterID__c,shippingstreet,shippingcity,shippingstate from Account where Name like \'% + newaccount.name + '%\'' OR (ShippingStreet like \'% + newaccount.shippingstreet + '%\' AND ShippingCity like \'% + newaccount.shippingcity + '%\' AND ShippingState like \'% + newaccount.shippingstate + '%\'));
That will look for any accounts where:
- the
name
they enter is contained in the name field of any existing account OR - the
shippingstreet
is contained in an existing account's street address AND theshippingcity
is contained in an existing account's city AND theshippingstate
is contained in an existing account's state
... the % signs in conjuntion with like
act like a wildcard search and the right combination of boolean AND/OR logic helps (my AND/OR was based on what i understood you wanted, but can be easily tweaked)
Attribution to: Nathan Williams
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33076