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
Daniel KDaniel K 

fetch selected item from vfpage to standard page

Hi,
     I have a standard Product page with few fields but here we talk about two fields: 
     1.Formula field with hyperlink to open vfpage on same tab.
     2.Text field to hold Product name

     Now, when I click on formula's hyperlink, a vf page should open and through that vfpage I should search for particular product and select one.
     When I select product from vfpage, the text field in standard page should be filled with the product name.

     What is the ideal way to do this without having JS in formula field's hyperlink ?

Thanks in Advance.
Best Answer chosen by Daniel K
Damon ShawDamon Shaw
Hi Daniel, 

I didn't notice earlier but you're not passing the Id of the account to be updated to your controller, you'll need this to know whaich account to update.

you've not provided your link code used to open the visualforce page but if you update it to look something link this, you need to make sure you are giving the page the Id of the account you want to update
HYPERLINK("/apex/AccountSearch?id="+Id, "Account Search")

then update your controller, in your constructor find the Id passed to the page and use it to look up the record you have come from, then in the updateAccount method you have something to update and go back to. 
 
public with sharing class AccountSearch {
	
    public Account account {get;set;}
    public List<Account> results {get;set;}
    public String searchString {get;set;}
    public Boolean showmsg {get;set;}
    public String selectedAccountName { get; set; }

    public AccountSearch() {
	
        this.account = [SELECT Id, Acc_Name__c FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        this.results = new List<Account>();
        this.showmsg = false;
	}

    public PageReference search() 
    {        
        results = performSearch(searchString);
        return null;
    }
    
    private List<Account> performSearch(string searchString)
    {
        List<Account> SearchResult = new List<Account>();
        {
            String soql = 'SELECT Id, Name, Site, Industry, Type, Phone FROM Account';
            if(!String.isBlank(searchString)){
                soql = soql + ' WHERE Name LIKE \'%' + searchString +'%\'';
            }

            List<Account> tempacclist = database.query(soql+' limit 100');

            if(tempacclist.size() == 0)
            {                
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.WARNING,'No Data Found');
                ApexPages.addMessage(myMsg);
                showmsg = true;
            }
            else
            {
                showmsg = false; 
                SearchResult = tempacclist;
            }
        }    
        return SearchResult;
    }
    
    public PageReference updateAccount(){   
        account.Acc_Name__c = selectedAccountName;
        update account;

        return new PageReference('/'+account.Id).setRedirect(true);
    }
}

 

All Answers

Damon ShawDamon Shaw
Hi Daniel,

If I have this right, you have a standard page with a formula field hyperlink, when you click the link the page redirects to a visualforce page, then when you select a product on the visualforce page you want to return to the standard page and have the value set.

How to do this would depend on which version of the standard page you wish to see, the edit or the detail page, and without seeing your existing visualforce page/controller it's a little hard to tell you what's needed.

Firstly I would pass your record's Id to the Visualforce page which will tell the controller where you want to return to, your hyperlink should look something like this /apex/YourVFPage?myRecordId

then If you want to return back to the edit page I would use a URL hack to open the edit page and set the required value, there are quite a few blogs on the subject so just google Salesforce URL Hacking, here's one that really helped me out when I first started, http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html

your controller method should have something like this
public PageReference goBackToStdPage(){
    String myRecordId = ApexPages.currentpage().getparameters().containsKey('id');
    return new PageReference('/'+myRecordId+'?fieldid='+selectedValue).setRedirect(true);
}

If you want to return to the detail page with the selected product saved, you will need to update the record in your controller and then return to the record
public PageReference saveAndGoBackToStdPage(){
    Record__c myRecord = [SELECT Id, Product__c FROM Record__c WHERE Id = :ApexPages.currentpage().getparameters().containsKey('id')];
    myRecord.Product__c = selectedProduct;
    
    update myRecord;
    return new PageReference('/'+myRecord.Id).setRedirect(true);
}

 
Daniel KDaniel K
<apex:page controller="AccountSearch" title="Search"
           showHeader="false" 
           sideBar="false" 
           tabStyle="Account" 
           id="pg"
           docType="html-5.0">  
    <apex:form >
        <apex:outputPanel >     
            <apex:actionstatus id="status">
                <apex:facet name="start">
                    <div class="waitingSearchDiv" id="el_loading"> 
                        <div class="waitingHolder">
                            <img class="waitingImage" src="/img/loading.gif" title="Please Wait..." />                  
                            <span class="waitingDescription">Searching...</span>
                        </div>
                    </div>
                </apex:facet>
            </apex:actionstatus>
        </apex:outputPanel>
        
        <apex:outputPanel id="page" layout="block">
            <apex:actionRegion >  
                <apex:outputPanel id="top" layout="block">
                    <apex:outputLabel value="Search Name" for="txtSearch"/>
                    <apex:inputText id="txtSearch" value="{!searchString}" />
                    <span style="padding-left:5px"><apex:commandButton id="btnGo" value="Search" action="{!Search}" rerender="searchResults,msg" status="status"></apex:commandButton></span>                     
                </apex:outputPanel>
                <apex:outputPanel id="msg">
                    <apex:Pagemessages rendered="{!showmsg}"/>
                </apex:outputPanel>
                <apex:outputPanel id="pnlSearchResults" layout="block">
                    <apex:pageBlock id="searchResults">
                        <apex:pageBlockTable value="{!results}" var="a" id="tblResults">
                            <apex:column >
                                <apex:facet name="header">
                                    <apex:outputPanel >Selectone</apex:outputPanel>
                                </apex:facet>
                                <apex:outputlink value="/{!$CurrentPage.parameters.Id}&{!$CurrentPage.parameters.AccName}">Select and Go Back</apex:outputlink>
                            </apex:column>
                            <apex:column >
                                <apex:facet name="header">
                                    <apex:outputPanel >Name</apex:outputPanel>
                                </apex:facet>
                                <apex:outputText >{!a.Name}</apex:outputText>  
                            </apex:column>
                            <apex:column >
                                <apex:facet name="header">
                                    <apex:outputPanel >Industry</apex:outputPanel>
                                </apex:facet>
                                <apex:outputText >{!a.Industry}</apex:outputText>     
                            </apex:column>
                            <apex:column >
                                <apex:facet name="header">
                                    <apex:outputPanel >Type</apex:outputPanel>
                                </apex:facet>
                                <apex:outputText >{!a.Type}</apex:outputText>     
                            </apex:column>
                            <apex:column >
                                <apex:facet name="header">
                                    <apex:outputPanel >Phone</apex:outputPanel>
                                </apex:facet>
                                <apex:outputText >{!a.phone}</apex:outputText>     
                            </apex:column>                            
                        </apex:pageBlockTable>
                    </apex:pageBlock>                            
                </apex:outputPanel>
            </apex:actionRegion>
        </apex:outputPanel>
        <script type="text/javascript">
        document.getElementById('{!$Component.txtSearch}').focus();
        </script>
    </apex:form>
</apex:page>
 
public class AccountSearch 
{
    public Account account {get;set;}
    public List<Account> results{get;set;}
    public string searchString{get;set;}
    public boolean showmsg{get;set;}
    public Map<string,boolean> MaptoId{get;set;}
    public string AccName{get;set;}
    public string Id{get;set;}
    
    public AccountSearch()
    {
        account = new Account();
        results=new List<Account>();
        showmsg = false;
    }
    
    public PageReference search() 
    {
        
        results=performSearch(searchString);
        return null;
    }
    
    private List<Account> performSearch(string searchString)
    {
        List<Account> SearchResult=new List<Account>();
        system.debug('SearchString ' +searchString);
        {
            String soql = 'select id,name,site,industry,type,phone from account';
            if(searchString != '' && searchString != null)
                soql = soql +  ' where name LIKE \'%' + searchString +'%\'';
            list<account> tempacclist = database.query(soql+' limit 100');
            system.debug(soql);
            if(tempacclist.size()>100)
            {
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.WARNING, 'Please narrow down your search');
                ApexPages.addMessage(myMsg);
                showmsg=true;
                
            }
            else if(tempacclist.size() == 0)
            {
                
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.WARNING,'No Data Found');
                ApexPages.addMessage(myMsg);
                showmsg=true;
            }
            else
            {
                showmsg=false; 
                SearchResult = tempacclist ;
            }
                ApexPages.currentPage().getParameters().put('Id', Id);
                ApexPages.currentPage().getParameters().put('Acc_Name__c', AccName);
        }    
        return SearchResult;
    }


}

accountsearch
I hope, from the code and screenshots you can understand what I'm trying to achieve.
Now, when I click on "Select and Go Back", I should be able to redirect and fill "Acc Name" field in standard page with account name of selected account.
Acc Name is custom field created for testing purpose only.

Thank You.
Damon ShawDamon Shaw
Hi Daniel,

By the look of it you just want to set the name of the account to that field and be sent back to the detail page, so what we'll need is a method in your controller to update the account and then send you back to the record,

add these to your controller
public String selectedAccountName { get; set; }
public PageReference updateAccountName(){    
    account.Acc_Name__c = selectedAccountName;
    return new PageReference('/'+account.Id).setRedirect(true);
}

then what we need to do is call our new code from the page, using a commandlink lets us call our new function and the nested param will set the account name into the selectedAccountName variable ready for the method to reference it.
<apex:commandLink value="Process Nickname" action="{!updateAccount}">
    <apex:param name="accountName" value="{!a.Name}" assignTo="{!selectedAccountName}"/>
</apex:commandLink>
Here's a really good link for future use http://blog.jeffdouglas.com/2010/03/03/passing-parameters-with-a-commandlink/


If you do wish to return to the Edit screen with the field filled out and then manually click save, replace your link with this
<apex:outputlink value="/{!$CurrentPage.parameters.Id}&FIELDID={!a.Name}&FIELDID_lkid={!a.Id}">Select and Go Back</apex:outputlink>
replace the FIELDID with the id of the field on the page as described in the URL hacking link (http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html)
Daniel KDaniel K
Thank You.

Here should I use commandlink or outpulink ? or both ?
Damon ShawDamon Shaw
Use the command link if you want users to go back to the detail page, use the output link if you want users to go back to the edit page, 

you can put them both on the page if you wish and then test them to see the difference, then just remove the one you don't want
Daniel KDaniel K
I'm sorry, still I didn't get this.
public class AccountSearch 
{
    public Account account {get;set;}
    public List<Account> results{get;set;}
    public string searchString{get;set;}
    public boolean showmsg{get;set;}
    public Map<string,boolean> MaptoId{get;set;}
    public string AccName{get;set;}
    public string Id{get;set;}
    public String selectedAccountName { get; set; }
    
    public AccountSearch()
    {
        account = new Account();
        results=new List<Account>();
        showmsg = false;
    }
    
    public PageReference search() 
    {
        
        results=performSearch(searchString);
        return null;
    }
    
    private List<Account> performSearch(string searchString)
    {
        List<Account> SearchResult=new List<Account>();
        system.debug('SearchString ' +searchString);
        {
            String soql = 'select id,name,site,industry,type,phone from account';
            if(searchString != '' && searchString != null)
                soql = soql +  ' where name LIKE \'%' + searchString +'%\'';
            list<account> tempacclist = database.query(soql+' limit 100');
            system.debug(soql);
            if(tempacclist.size()>100)
            {
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.WARNING, 'Please narrow down your search');
                ApexPages.addMessage(myMsg);
                showmsg=true;
                
            }
            else if(tempacclist.size() == 0)
            {
                
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.WARNING,'No Data Found');
                ApexPages.addMessage(myMsg);
                showmsg=true;
            }
            else
            {
                showmsg=false; 
                SearchResult = tempacclist ;
            }
                ApexPages.currentPage().getParameters().put('Id', Id);
                ApexPages.currentPage().getParameters().put('Acc_Name__c', AccName);
        }    
        return SearchResult;
    }
    
    public PageReference updateAccount(){   
       account.Acc_Name__c = selectedAccountName;
       return new PageReference('/'+account.Id).setRedirect(true);
}


}
 
<apex:page controller="AccountSearch" title="Search"
           showHeader="false" 
           sideBar="false" 
           tabStyle="Account" 
           id="pg"
           docType="html-5.0">  
    <apex:form >
        <apex:outputPanel >     
            <apex:actionstatus id="status">
                <apex:facet name="start">
                    <div class="waitingSearchDiv" id="el_loading"> 
                        <div class="waitingHolder">
                            <img class="waitingImage" src="/img/loading.gif" title="Please Wait..." />                  
                            <span class="waitingDescription">Searching...</span>
                        </div>
                    </div>
                </apex:facet>
            </apex:actionstatus>
        </apex:outputPanel>
        
        <apex:outputPanel id="page" layout="block">
            <apex:actionRegion >  
                <apex:outputPanel id="top" layout="block">
                    <apex:outputLabel value="Search Name" for="txtSearch"/>
                    <apex:inputText id="txtSearch" value="{!searchString}" />
                    <span style="padding-left:5px"><apex:commandButton id="btnGo" value="Search" action="{!Search}" rerender="searchResults,msg" status="status"></apex:commandButton></span>                     
                </apex:outputPanel>
                <apex:outputPanel id="msg">
                    <apex:Pagemessages rendered="{!showmsg}"/>
                </apex:outputPanel>
                <apex:outputPanel id="pnlSearchResults" layout="block">
                    <apex:pageBlock id="searchResults">
                        <apex:pageBlockTable value="{!results}" var="a" id="tblResults">
                            <apex:column >
                                <apex:facet name="header">
                                    <apex:outputPanel >Selectone</apex:outputPanel>
                                </apex:facet>
                                <apex:commandLink value="Select and Go Back" action="{!updateAccount}">
                                      <apex:param name="accountName" value="{!a.Name}" assignTo="{!selectedAccountName}"/>
                                </apex:commandLink>
                            </apex:column>
                            <apex:column >
                                <apex:facet name="header">
                                    <apex:outputPanel >Name</apex:outputPanel>
                                </apex:facet>
                                <apex:outputText >{!a.Name}</apex:outputText>  
                            </apex:column>
                            <apex:column >
                                <apex:facet name="header">
                                    <apex:outputPanel >Industry</apex:outputPanel>
                                </apex:facet>
                                <apex:outputText >{!a.Industry}</apex:outputText>     
                            </apex:column>
                            <apex:column >
                                <apex:facet name="header">
                                    <apex:outputPanel >Type</apex:outputPanel>
                                </apex:facet>
                                <apex:outputText >{!a.Type}</apex:outputText>     
                            </apex:column>
                            <apex:column >
                                <apex:facet name="header">
                                    <apex:outputPanel >Phone</apex:outputPanel>
                                </apex:facet>
                                <apex:outputText >{!a.phone}</apex:outputText>     
                            </apex:column>                            
                        </apex:pageBlockTable>
                    </apex:pageBlock>                            
                </apex:outputPanel>
            </apex:actionRegion>
        </apex:outputPanel>
        <script type="text/javascript">
        document.getElementById('{!$Component.txtSearch}').focus();
        </script>
    </apex:form>
</apex:page>

error

Above error occurs when I click on "Select and Go Back" link on vfpage.

Pls suggest.
Damon ShawDamon Shaw
Hi Daniel, 

I didn't notice earlier but you're not passing the Id of the account to be updated to your controller, you'll need this to know whaich account to update.

you've not provided your link code used to open the visualforce page but if you update it to look something link this, you need to make sure you are giving the page the Id of the account you want to update
HYPERLINK("/apex/AccountSearch?id="+Id, "Account Search")

then update your controller, in your constructor find the Id passed to the page and use it to look up the record you have come from, then in the updateAccount method you have something to update and go back to. 
 
public with sharing class AccountSearch {
	
    public Account account {get;set;}
    public List<Account> results {get;set;}
    public String searchString {get;set;}
    public Boolean showmsg {get;set;}
    public String selectedAccountName { get; set; }

    public AccountSearch() {
	
        this.account = [SELECT Id, Acc_Name__c FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        this.results = new List<Account>();
        this.showmsg = false;
	}

    public PageReference search() 
    {        
        results = performSearch(searchString);
        return null;
    }
    
    private List<Account> performSearch(string searchString)
    {
        List<Account> SearchResult = new List<Account>();
        {
            String soql = 'SELECT Id, Name, Site, Industry, Type, Phone FROM Account';
            if(!String.isBlank(searchString)){
                soql = soql + ' WHERE Name LIKE \'%' + searchString +'%\'';
            }

            List<Account> tempacclist = database.query(soql+' limit 100');

            if(tempacclist.size() == 0)
            {                
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.WARNING,'No Data Found');
                ApexPages.addMessage(myMsg);
                showmsg = true;
            }
            else
            {
                showmsg = false; 
                SearchResult = tempacclist;
            }
        }    
        return SearchResult;
    }
    
    public PageReference updateAccount(){   
        account.Acc_Name__c = selectedAccountName;
        update account;

        return new PageReference('/'+account.Id).setRedirect(true);
    }
}

 
This was selected as the best answer
Daniel KDaniel K
Thank you so much @Damon Shaw.