You need to sign in to do that
Don't have an account?
Allow User to Click on Account Name as Link in Custom Search Page
I want to allow users to click on the account name (or have a link in a fourth column that says 'View this Account') and be directed to the record.
Have searched and it's probably right in front of me, but I can't figure it out.
Source of vf page template: http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_methods.htm
<apex:page controller="SearchBeforeAddingController">
<apex:form >
<apex:pageBlock mode="edit" id="block">
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel for="searchText">Search for:</apex:outputLabel>
<apex:panelGroup >
<apex:inputText id="searchText" value="{!searchText}"/>
<apex:commandButton value="Go!" action="{!doSearch}"
rerender="block" status="status"/>
</apex:panelGroup>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:actionStatus id="status" startText="searching existing accounts... please wait..."/>
<apex:pageBlockSection title="These Accounts Potentially Match:" id="results" columns="1">
<apex:pageBlockTable value="{!results}" var="a"
rendered="{!NOT(ISNULL(results))}">
<apex:column value="{!a.name}"/>
<apex:column value="{!a.site}"/>
<apex:column value="{!a.AKA__c}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
An even simpler way to do it would be to assign the url to the action attribute, that is:
<apex:commandLink action="/{!a.id}" target="_blank">{!a.Name}</apex:commandLink>
All Answers
<apex:commandLink action="{!ViewAccount}" target="_blank">{!a.Name}
<apex:param value="{!a.Id}" name="accid"></apex:param>
</apex:commandLink></apex:column>
In the controller you have to write the ViewAccount method as follows.
{
Id Accountid = ApexPages.currentPage().getParameters().get('accid');
PageReference pg = new PageReference('/'+Accountid);
return pg;
}
An even simpler way to do it would be to assign the url to the action attribute, that is:
<apex:commandLink action="/{!a.id}" target="_blank">{!a.Name}</apex:commandLink>
Srini:
Thanks for the suggestion!
Initially, this worked for adding the link to the account, but I was getting an "Invalid Id: An unexpected error has occurred. Your development organization has been notified" error.
I discovered that the code executed as expected when I added the account id field as a column to the page.
So, here is my current controller:
public class SearchBeforeAddingController { String searchText; List<Account> results; public String getSearchText() { return searchText; } public void setSearchText(String s) { searchText = s; } public List<Account> getResults() { return results; } public PageReference doSearch() { results = (List<Account>)[FIND :searchText RETURNING Account(Id, Name, Site, AKA__c)][0]; return null; } public PageReference ViewAccount() { Id Accountid = ApexPages.currentPage().getParameters().get('accid'); PageReference pg = new PageReference('/'+Accountid); return pg; } }
And my page:
<apex:page controller="SearchBeforeAddingController"> <apex:form > <apex:pageBlock mode="edit" id="block"> <apex:pageBlockSection > <apex:pageBlockSectionItem > <apex:outputLabel for="searchText">Search for:</apex:outputLabel> <apex:panelGroup > <apex:inputText id="searchText" value="{!searchText}" /> <apex:commandButton value="Go!" action="{!doSearch}" rerender="block" status="status" /> </apex:panelGroup> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:actionStatus id="status" startText="searching existing accounts... please wait..." /> <apex:pageBlockSection title="These Accounts Potentially Match:" id="results" columns="1"> <apex:pageBlockTable value="{!results}" var="a" rendered="{!NOT(ISNULL(results))}"> <apex:column > <apex:commandLink action="{!ViewAccount}" target="_blank">{!a.Name} <apex:param value="{!a.Id}" name="accid"></apex:param> </apex:commandLink> </apex:column> <apex:column value="{!a.site}" /> <apex:column value="{!a.AKA__c}" /> <apex:column value="{!a.id}" /> </apex:pageBlockTable> </apex:pageBlockSection> <apex:pageBlockSection title="Available Actions"> <apex:commandButton value="Continue with Adding a New Account" /> </apex:pageBlockSection> <apex:pageBlockSection > <apex:commandButton value="Cancel" /> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
Thanks again!
rhsummer:
You're right! Much simpler. And I don't have to add the account id as a column to get the link to work correctly. Thank you very much!
This is my preferred method, though both solutions worked.