1

I have done with almost everything. The only thing left is to prevent multi-selection of radio button. It should be a single select only.

---Controller--

public class ContactRoleOnOpp8 {
    public List<WrapOcrClass> wrapOcrList {get; set;}
    public List<OpportunityContactRole> ocrRecord {get; set;}
    public List<SelectOption> statusOptions { get; set;}


    public List<Id> conId {get; set;}    
    public List<Contact> conIds {get; set;}

    //Constructor
    public contactRoleOnOpp8(ApexPages.StandardController controller){

        wrapOcrList = new List<WrapOcrClass>();
        for(OpportunityContactRole ocrList : [SELECT Contact.Id, Contact.Name, IsPrimary, Role FROM OpportunityContactRole]){
            wrapOcrList.add(new WrapOcrClass(ocrList));
        }
        autorun();
    }

    //Wrapper Class
    public class WrapOcrClass{
        public OpportunityContactRole ocr {get; set;}
        public String selectedValue { get; set;}
        public Boolean selected {get; set;}
        public Boolean isSelected {get; set;}

        public wrapOcrClass(OpportunityContactRole ocrWrap){
            ocr = ocrWrap;
            selected = false;
            isSelected = false;
        }
    }

    //Getting all Roles(PickList) from OCR
    public List<SelectOption> autoRun()
    {
        statusOptions = new List<SelectOption>();
        Schema.DescribeFieldResult statusFieldDescription = OpportunityContactRole.Role.getDescribe();
        for (Schema.PicklistEntry pickListEntry : statusFieldDescription.getPicklistValues())
        {
            statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
        }
        return statusOptions;
    }

    public List<SelectOption> getAutoRun2()
    {
        List<SelectOption> statusOptions2 = new List<SelectOption>();

        statusOptions2.add(new SelectOption('true', ''));
        return statusOptions2;
    }

    //Update Roles
    public void updateRole()
    {
        //String selContactId = System.currentPagereference().getParameters().get('id');
        List<OpportunityContactRole> roleUpdate = new List<OpportunityContactRole>();

        if(wrapOcrList!=Null){
            for(WrapOcrClass wrap : wrapOcrList)
            {
                if(wrap.isSelected==true){
                    wrap.ocr.isPrimary= true;
                    wrap.isSelected = false;
                    roleUpdate.add(wrap.ocr);
                    System.debug('jjj');

                }
                else{
                    wrap.ocr.isPrimary= false;
                    wrap.isSelected = false;

                }
                if(wrap.selected)
                {
                    wrap.ocr.Role = wrap.selectedValue;
                    wrap.selected = false;

                    roleUpdate.add(wrap.ocr);
                    System.debug('hhh');
                }
            }
            Update roleUpdate; 
            System.debug('iii');
        }
    }
}

--VFPage--

<apex:page extensions="ContactRoleOnOpp8" standardController="OpportunityContactRole" sidebar="false">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
                           <script>
function changeValue(radio button)
{
    var a = document.getElementById("c1")
    for (var i=0; i<a.length; i++) 
    {
        if(radioButton.value == a[i].value) 
        {
            a[i].checked = true;
        }
        else
        {
            a[i].checked = false;
        }
    }

    </script>
<apex:form >
    <apex:pageBlock mode="edit">
        <apex:pageBlockButtons location="top">
            <apex:commandButton value="Update" id="inputButton" action="{!updateRole}" />
        </apex:pageBlockButtons>
        <!--<apex:repeat value="{!wrapOcrList}" var="rep">
            <apex:selectRadio value="{!rep.isSelected}"> 
                <apex:selectOptions value="{!Autorun2}"  >
                </apex:selectOptions></apex:selectRadio>
        </apex:repeat>-->

        <table style="width:70%" id="consel">
            <tr>
                <th style="width:1%">Action</th>
                <th style="width:15%">Name</th>
                <th style="width:35%">Role</th>
                <th style="width:20%" >Select the Roles</th>
                <th style="width:5%">Checkbox</th>
                <!--<th style="width:5%">Primary</th>-->
            </tr>
            <apex:repeat value="{!wrapOcrList}" var="rep">
                <tr>
                    <td style="width:1%">
                        <apex:selectRadio value="{!rep.isSelected}" id="c1"> 
                            <apex:selectOptions value="{!Autorun2}"  >
                            </apex:selectOptions></apex:selectRadio>
                    </td>
                    <td style="width:15%">{!rep.ocr.Contact.Name}</td>
                    <td style="width:35%">{!rep.ocr.Role}</td>
                    <td style="width:20%"><apex:selectList size="1" value="{!rep.selectedValue}" multiselect="true">
                        <apex:selectOptions value="{!statusOptions}" />
                        </apex:selectList></td>
                    <td style="width:5%"><apex:inputCheckbox value="{!rep.selected}" id="inputCheckbox"/></td>
                   <!-- <td style="width:5%" id="primary">{!rep.ocr.isPrimary}</td>-->
                </tr>
            </apex:repeat> 
        </table>
    </apex:pageBlock>
</apex:form>

  • See this answer for a potential solution. I explain what the problem is as well. – sfdcfox Aug 19 at 6:15
  • I am not getting it. Please Elaborate. – Berry Allen Aug 19 at 11:56
1

Use Input Tag for Radio Button as shown below and no need to use Script.. it can be done by Apex Controller itself

<td>
      <input name="isRadio" type="radio" value="{!rep.ocr.Contact.Id}" /><br/>
</td>
public void updateRole()
    {
        List<OpportunityContactRole> roleUpdate = new List<OpportunityContactRole>();
        if(wrapOcrList!=Null){
            for(WrapOcrClass wrap : wrapOcrList)
            {
                if(System.currentPagereference().getParameters().get('isRadio') == wrap.ocr.Contact.Id)
                {
                    wrap.ocr.isPrimary = true;
                    roleUpdate.add(wrap.ocr);
                }
             Update roleUpdate;
            }
    }

And also there is no need of getAutoRun2(). You can remove this.

0

If you want to show the radio button against each record in repeat tag, then you will have to use the html input tag with type as radio i.e.

<input type="radio" name="namefield" value="{!value}" />

Where name attribute determines the group and having all the radio buttons with same name makes sure that only one record is selected from the list of records displayed.

In your case, you should replace the below code

<td style="width:1%">
    <apex:selectRadio value="{!rep.isSelected}" id="c1"> 
        <apex:selectOptions value="{!Autorun2}"  >
        </apex:selectOptions>
    </apex:selectRadio>
</td>

with

<td style="width:1%">
    <input type="radio" name="accId" value="{!rep.ocr.Id}" onchange="updateHidden(event)"/>
</td>

And to get the selected value in controller, you can use <apex:inputHidden> component and set its value with selected record which can be accessed in controller.

To do that, you need to make below changes in your code

  • Create a property in controller to hold the selected value.
  • Add an <apex:inputHidden> component in visualforce page to hold the selected record.
  • Add javascript which updates the selected value in <apex:inputHidden> tab on change of selection of radio button.

Creation of property

public string selectedValue {get;set;}

Addition of <apex:inputHidden> attribute before the start of repeat tag

<apex:inputHidden value="{!selectedValue}" id="selectedValue" />

And then javascript to update the selected value

<script>
function updateHidden(event) {
    document.querySelector("[id$=selectedValue]").value = event.target.value;
    console.log(event.target.value);
}
</script>
  • I have to use apex tags. If I am using html tag input the functionality of getting primary contact is not working. – Berry Allen Aug 19 at 12:21
  • @BerryAllen Updated the answer, that should work for you. – Vijay Ganji Aug 19 at 13:19
  • Isn't there any way I can get expected result with the help of apex:selectradio.. – Berry Allen Aug 20 at 9:14

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.