Is there any method to get selected record of opportunity line item byt clicking on custom button in opportuntiy detail page ?

my requirement is : from the custom button on opportunity detail page will clone existing opportunity with related opportunity line items (only take product which is selected using checkbox in related list)

i have developed following code to get that all opportuinty info and all related opportunity line item but i am not able to get only thaose records which is slected in relatedlist.

I gone through this link but did not get what exactly i need.

can someone have solutions about this ?

Code that i developed :

public class MoveOpportunityProductController{

    public String oppId{get;set;}
    public List<OpportunityLineItem> ListoppLineitem{get;set;}
    public Opportunity Opportunity{get;set;}
    public List<OpportunityLineitem> Listoli{get;set;}

    public MoveOpportunityProductController(){

        oppId = ApexPages.currentPage().getParameters().get('Id');
        ListoppLineitem = [select Id, PricebookEntry.Name, Description, Manufacturer__c, SOW__c, PrimaryTechnologyArchitect__c, Inside_Sales_Rep__c, Quantity, UnitPrice, ServiceDate from OpportunityLineItem where opportunityId =: oppId order by PricebookEntry.Name];
        Opportunity = [select Id, Name, Amount, NextStep, Description, CloseDate, StageName, Probability, LeadSource, Inside_Sales_Rep__c, PrimaryTechnologyArchitect__c, WinLoss__c, AccountId, Solutions__c from Opportunity where Id =: oppId];

    public pagereference Cancel(){

        return new PageReference('/'+oppId);


    public PageReference SaveOppWithProduct(){
        Opportunity.Id = Null;
        insert Opportunity;
        Listoli = new List<OpportunityLineitem>();
        for(OpportunityLineItem oli : ListoppLineitem){
            oli.id = Null;
            oli.OpportunityId = Opportunity.Id;
        insert Listoli;
        update Opportunity;
        return new PageReference('/'+Opportunity.Id);

From what I understand, you've created a "Detail Page Button" on Opportunity, and wish to figure out which OpportunityLineItem records have been selected from the respective related list.

That is, unfortunately for you, impossible.

The checkboxes that are part of the OpportunityLineItem related list are only accessible through a "List Button" on OpportunityLineItem.

If you can live with having the button on the OpportunityLineItem (I'll be using OLI from here on out) related list, that's the approach I would recommend. The article that you linked to would serve as a decent starting point for this approach.

Your controller would need minimal changes. Using


would give you the Id of the Opportunity, your constructor would need to accept an ApexPages.StandardSetController parameter, and you would access the OLIs that were selected via the getSelected() method of the StandardSetController.

If using a List Button isn't an option, and you need to use the "Detail Page Button", then you'll need to do a bit more work.

Instead of having your users select which OLIs to move before pressing your button, you'll need to display the OLIs that you're querying in your controller on the Visualforce page that appears after pressing your button (and allow users to select OLIs from there). This method usually requires a wrapper class to be able to select individual records, and there are plenty of examples out there on how to do this (take this, for example).

I was also able to find a method that doesn't require a wrapper class in CloudTonic's answer on this question last year.

  • Thanks Derek for reply !! you are correct, this is not possible by detail page button on opportunity. So, i choose reverse solution for this. let me post the answer. again thanks :) – Nail David May 23 '17 at 14:04

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.