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
Sravan PSravan P 

Radio button to select an Opportunity Product from a list on a VF Page

Hi,

I am trying to develop a VF page Using Standard Controller as Opportunity. I want to display all the opportunity Products on the VF page along with radio buttons. User should be able to select only one opportunity product and the id of that line item has to be stored in an hidden field. How can I achieve that in Visual Force?

Thank You Inadvance for your help!!
Best Answer chosen by Sravan P
hitesh90hitesh90
Hi Sravan,

see Below example for your requirement,

Visualforce Page:
<apex:page standardController="Opportunity" extensions="extOpportunityProduct">
    <script>
        function setOppProduct(prId){
            document.getElementById('{!$Component.frm.pbMain.hdnOppProduct}').value = prId;
        }
    </script>
    <apex:form id="frm">
        <apex:pageBlock id="pbMain">
            <apex:inputhidden id="hdnOppProduct" value="{!strhdnOppProduct}"/>
            <apex:pageBlockTable value="{!Opportunity.OpportunityLineItems}" var="oli">
                <apex:column headerValue="Select">
                    <input type="radio" name="Productitem" value="{!oli.id}" onchange="setOppProduct('{!oli.id}');"/> 
                </apex:column>
                <apex:column>
                    <apex:outputLabel value="{!oli.Opportunity.Name}"></apex:outputLabel>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:
Public class extOpportunityProduct{
    Public string strhdnOppProduct {get; set;}   
    Public extOpportunityProduct(apexpages.standardcontroller con){   
    }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

All Answers

hitesh90hitesh90
Hi Sravan,

see Below example for your requirement,

Visualforce Page:
<apex:page standardController="Opportunity" extensions="extOpportunityProduct">
    <script>
        function setOppProduct(prId){
            document.getElementById('{!$Component.frm.pbMain.hdnOppProduct}').value = prId;
        }
    </script>
    <apex:form id="frm">
        <apex:pageBlock id="pbMain">
            <apex:inputhidden id="hdnOppProduct" value="{!strhdnOppProduct}"/>
            <apex:pageBlockTable value="{!Opportunity.OpportunityLineItems}" var="oli">
                <apex:column headerValue="Select">
                    <input type="radio" name="Productitem" value="{!oli.id}" onchange="setOppProduct('{!oli.id}');"/> 
                </apex:column>
                <apex:column>
                    <apex:outputLabel value="{!oli.Opportunity.Name}"></apex:outputLabel>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:
Public class extOpportunityProduct{
    Public string strhdnOppProduct {get; set;}   
    Public extOpportunityProduct(apexpages.standardcontroller con){   
    }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
This was selected as the best answer
Sravan PSravan P
Hi Hitesh,

This worked. Thank you so much. 
vswayvsway
Hi Hitesh - I am trying to achieve a similar requirement - adding the Opportunity Line Items to a Case record. Would that be possible.

My requirement is this: Create a Case from an Opportunity and add the related Opportunity Line Items to the Case related list or page layout. The Opportuntiy can be changed, add / remove Opportunity Line Items but not the Case Opportunity Products. Another Case can than be created with the updated Opportunity Line Items.

Thanks!