1

Goal: I am attempting to create an 'edit' option for objects in a table, that when clicked, will take you to the appropriate object page. I'm currently trying to tie the link information to each object in the table, and when the button is clicked, it will return a PageReference object and open the appropriate page. More info: the visualforce page is being placed in the page layout of Accounts.

Problem: I'm not too sure how to set this up. This is how I have the page:

<apex:page standardController="Account" extensions="OpportunityAccountLinkController" docType="html-5.0">

<apex:form>
        <apex:pageBlockTable value="{!oppHolders}" var="holder">
            <apex:column>
                <apex:facet name="header">Action</apex:facet>
                <apex:commandButton action="{!holder.toEdit}" value="Edit"/>
            </apex:column>
            <apex:column value="{!holder.opp.Name}">
                <apex:facet name="header">Opportunity Name</apex:facet>
            </apex:column>
       </apex:pageBlockTable>
<apex:form>

Im holding the info in a wrapper class, found in OpportunityAccountLinkController:

public class OppHolder {

        public Opportunity opp {get;set;}
        public PageReference editUrl {get;set;}

        public OppHolder(Opportunity o, PageReference pr) { 
            this.opp = o;
            this.editUrl = pr;
        }

        public PageReference toEdit() {
            return this.editUrl;
        }
    }

And this is how pageReferences are formed in that controller:

PageReference pr = new PageReference(opp.Id + '/e?retURL=' + opp.AccountID);

This question may be a bit open ended, but how do I open this link, in a new page? Right now, everything else displays correctly, except for the link. It opens within the injected visualforce page, and it takes me to an incorrect page. It gives an error on that page: "Formula Expression is required on the action attributes", which confuses me as I thought I called the method correctly.

Thanks

2
  • If it is the fields in the table that your users are editing, you could do the editing in place instead of in a separate page by using apex:inlineEditSupport.
    – Keith C
    Sep 17, 2015 at 20:29
  • Thanks for the info, but its more to gain access to the entire object. The fields in the table are just for user convenience.
    – lisolm
    Sep 17, 2015 at 20:31

1 Answer 1

1

You can construct your PageReference more programatically:

PageReference toEdit = new ApexPages.StandardController(opp).edit();
toEdit.getParameters().put('retURL', opp.AccountId);

If you are willing to change your component to actually be a "link" you can force it to open in a new tab by setting target="_blank".

<apex:outputLink value="{!holder.toEdit}" target="_blank">Edit</apex:outputLink>

This value can be generated without an Apex as follows:

{!URLFOR($Action.Opportunity.Edit, opp.Id, [retUrl='/' & Account.Id])}
1
  • 2
    Adrian -- couldn't this also be done with a URLFOR and no need for controller variable toEdit ?
    – cropredy
    Sep 18, 2015 at 1:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .