2

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. The only thing I can't figure out is when there is more than one duplicate that shows up in the results, the "Select" radio button doesn't link to the correct account id detail page. It always reverts back to the first result id in the list because of this piece of code. Would someone be able to point me in the right direction on how I can get the radio buttons to correspond with the right account ids? Thanks!

public pagereference SelectName() {

Pagereference chkdup = new Pagereference('/' + chkdup[0].Id);
return chkdup;
}

public boolean geterrormsg() {
return errormsg;
}

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
                         &nbsp;&nbsp;<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>

2

As you probably realise, your SelectName method is hard coded to use the first ID chkdup[0].Id.

This code should address that by adding an accountId property that the page can set and using it to go to the right detail page:

public Id accountId {get; set;}
public PageReference SelectName() {
    return new PageReference('/' + accountId);
}

The page needs to have an apex:param added to set the accountId field when the apex:commandlink is clicked:

<apex:commandlink action="{!SelectName}">
    <input type="radio" name="AccountSel"
            onclick="changeValue(this,'{!$Component.RadioButtonValue}')"/>
    <apex:param name="accountId" value="{!acct.Id}" assignTo="{!accountId}"/>
</apex:commandlink> 
  • Thanks Keith C, that worked perfectly! I knew it had to do with the SelectName being hardcoded, but I didn't know how to fix that. – NewbieDeveloper27 Apr 16 '14 at 16:51
1

From a UX perspective, why not just provide a link to the duplicate account's detail page from the pageBlockTable?

For example:

<apex:pageblocktable rendered="{!NOT(ISNULL(existingaccountlist))}" value="{!existingaccountlist}" var="acct">
    <apex:column headervalue="Select">{!URLFOR($Action.Account.view, acct.id)</apex:column>
</apex:pageBlockTable>

There's no need for any Javascript nor additional Apex handling in this case, and it has the same effect of redirecting the user to the duplicate's detail page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.