0

I have written a visualforce page with a button and i want that when it's pressed there is a redirect to the standard page ADD PRODUCT!(the page that is opened when you click OPPORTUNITY--->ADD PRODUCT)

The URL is salesforce.com/p/opp/SelectSearch?addTo=006E000000AVjws&retURL=%2F006E000000AVjws

I have written the following code but it doesn't work:

<apex:form >
    <apex:commandButton value="Add Product" action="{!addProduct}">
    </apex:commandButton>
</apex:form>

public class tempsection {

public PageReference addProduct(){

Id conids='006E000000AVjwsIAD';

String newPageUrl = '/p/opp/SelectSearch?addTo='+conids;

PageReference p = new PageReference(newPageUrl);
p.setRedirect(true);
return p;
}

}  

Is the Opportunity ID the only necessary?

Thank you in advance for any advice.

BR

5
  • Are you setting addProductsURL elsewhere in the code ? Commented May 16, 2013 at 10:11
  • addProductsURL never seems to be initialised to p/opp/SelectSearch? in your code ? You would need to set the initial value of addProductsURL to '/p/opp/SelectSearch?' as you've written in your question. Commented May 16, 2013 at 10:35
  • i have updated my code, please take a look,it isn't working.
    – Enry
    Commented May 16, 2013 at 10:48
  • Could you paste your entire VF page? I've copied your sample and it is working fine for me. Commented May 16, 2013 at 12:03
  • I think that i can't access the page add product at the address (/p/opp/SelectSearch?addTo=006b0000002yBpB) because i have overrided the standard button named ADD PRODUCT. After the click i am only able to see /apex/REDIRECTADD?id=006E000000AVjws that is the name of the custom visual force page. am i right? Thanks
    – Enry
    Commented May 16, 2013 at 12:20

2 Answers 2

2

Is the Opportunity Id available from the VF page context? If so, you could save yourself some Apex code and do this:

<apex:form >
    <apex:commandButton value="Add Product" action="{!URLFOR($Action. OpportunityLineItem.AddProduct,<ref to opportunity id>)}"/>    
</apex:form>

I would always go for the Action global variable above hacking the raw URL from the browser I can.

If you can't just pick up the URL from the VF page context (std controller or url param) then just have a simple controller property to retrieve the opportunity id:

<apex:form >
    <apex:commandButton value="Add Product" action="{!URLFOR($Action. OpportunityLineItem.AddProduct,OppId)}"/>
</apex:form>

-

public class tempsection {

    public string getOppId(){
        //whatever logic to get the actual opp Id
        return '006E000000AVjwsIAD';
    }

}   

And I've not tested this exact usage of the Action global variable, so I have some questions myself about how this gets the list of products in the first place. But this might send you on your way.

0

The best code example I've found to do this is at http://www.michaelforce.org/recipeView?id=a0G30000006eVxVEAU

You must log in to answer this question.

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