1

So I'm trying to build some custom functionality revolving around opportunities and products.

What I have right now is a button on the OpportunityLineItem related list that is on our opportunities. When a user presses that button they are brought to a custom visualforce page with the same opp line items that are on the opportunity. From there, users can change a specific field on each of these that laucnhes some custom code.

What I really want though, is for only one selected item to be brought over to the visualforce page. I want to let users select that item with the action checkboxes on the opportunity line items.

Here's what I'm talking about:

select line item

So my question is: how can I tell what item is selected, and dictate my visualforce to only display that line item? I can't find anything in the API referncing these selection boxes, but I'm sure there must be something.

Here's the current state of my visualforce page:

<apex:page standardController="Opportunity" extensions="Opportunity_extendedController" >
<apex:pageBlock title="Line Items">
    <apex:form >
        <apex:commandButton action="{!exchangeInv}" value="Exchange Inventory" rerender="fake" />
        <apex:commandButton action="{!cancel}" immediate="true" value="Cancel"/>
        <apex:pageBlockTable value="{!Opportunity.opportunityLineItems}" var="oli">
            <apex:column value="{!oli.Id}" />
            <apex:column value="{!oli.Inventory__c}"/>
            <apex:column value="{!oli.Name_Description__c}"/>
            <apex:column headerValue="Inventory to exchange">                
                <apex:inputField value="{!oli.Inventory__c}" />
            </apex:column>
        </apex:pageBlockTable>
    </apex:form>
</apex:pageBlock>

  • I was writing up an answer using a List Controller, but, when I tried to save my example, Salesforce gave me an error saying that "List Controllers are not supported for OpportunityLineItem". Yet another WTF Salesforce moment for me... I think you may be out of luck trying to figure out precisely which OLIs were selected in the related list (even with a StandardSetController, you couldn't restrict how many OLIs a user could select, nor reliably tell the order in which they were selected). – Derek F Aug 3 '17 at 14:15
0

If you don't have any solution then I have a work around for this if you want to try. Below is the code for that

You can either use cookies to set value and then retrieve in your VF page or you can pass the value in URL. Below is the example of passing value to URL.

var allProducts = document.getElementsByName('ids');
            var str = '';
            for(var i=0; i<allProducts.length; i++){
                if(allProducts[i].checked){
                    str += allProducts[i].value + '-';
                }
            }
    window.open('/apex/PageName?AllIds='+str);

Visual force Page

    <apex:page standardController="opportunity" recordSetVar="opportunitylineItems" extensions="ShowOppLineItemsController">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!LineItems}" var="Item">
            <apex:column value="{!Item.product2.name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Apex Class

public class ShowOppLineItemsController{

    //List to display on page
    public list<opportunityLineItem> LineItems{get;set;}

    //Standard set controller
    public ShowOppLineItemsController(ApexPages.StandardSetController controller){
        LineItems = new list<opportunityLineItem>();
        list<string> allIdsList = new list<string>();

        //Getting ids from the url
        string AllIds = apexpages.currentpage().getparameters().get('AllIds');
        if(AllIds != null && AllIds != ''){
            //Spliting all ids in list to make a query
            allIdsList = AllIds.split('-');
            if(allIdsList != null && allIdsList.size() > 0){
                //final query on line items
                LineItems = [select id, name, product2.name from opportunityLineItem where id In : allIdsList];
            }
        }
    }

}
  • Oh nice I never even thought of doing it this way, the javascript is grabbing the correct elements. However, I'm having some trouble actually getting those line items to appear on the visualforce page. By adding my line item IDs onto the end of that URL, I just end up with an empty table on my visualforce page. How can I change my visualforce markup to accept the items I want to display from the URL like this? – Adam O. Aug 3 '17 at 17:53
  • you know have to use controller side to take all the line items ids and make a query and then finally display on the table. – Rishi Golyan Aug 3 '17 at 18:08
  • Okay gotcha, I'm kind of new to visualforce and custom controllers, could you direct me to some documentation on doing something like this? – Adam O. Aug 3 '17 at 19:09
  • Please use the salesforce docs to go through them. Here is the link developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/…. Also I am adding another answer with full code below to your reference , If you find it helpful please select it as a best answer – Rishi Golyan Aug 4 '17 at 5:49
0

Let's do this task as following (you can improvise the solution)

  1. Use javscript button and put it in the related list as stated
  2. On button click you can get the list of selected items (I believe this still works with button JS)
  3. Once you have refined the list of items, form the next page URL (I mean VF page URL)
  4. All I am trying to convey is that, use URL param to pass those selected values and use them as you wanted.
  • Do you know how I would get that list of selected items with JS? I can't find anything that references the status of their selection. – Adam O. Aug 3 '17 at 14:30

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.