SFDC_M
SFDC_M

Reputation: 61

I am trying to update isPrimary Field from Radio Button in VF. Code is not working for Radio Button but same code is working fine for Checkbox

I have 2 Contact Record Stored. and using below code when i am trying to update Opportunities Contact Role from VF Dropdown as selected in Checkbox it is Working and updating value from the Contact Record (created in Wrapper Class as said) now by selecting radio button that Contact's IsPrimary should be checked. I've also added this in Wrapper Class as you said butwrap.isSelected``` this says true if i don't even check radio button

Why is that the same code is working fine for checkbox but won't working for Radio button

<table style="width:50%">
                    <tr>
                        <th>Action</th>
                        <th>Name</th>
                        <th>Role</th>
                        <th>Select the Roles</th>
                        <th>Checkbox</th>
                    </tr>
                    <apex:repeat value="{!wrapOcrList}" var="rep">
                        <tr>
                            <td><input type="radio" name="isSelected" value="{!rep.isSelected}"/></td>
                            <td>{!rep.ocr.Contact.Name}</td>
                            <td>{!rep.ocr.Role}</td>
                            <td>
                                <apex:selectList size="2" value="{!rep.selectedValue}" multiselect="true">
                                    <apex:selectOptions value="{!statusOptions}" />
                                </apex:selectList>
                            </td>
                            <td><apex:inputCheckbox value="{!rep.selected}" id="inputCheckbox"/></td>
                        </tr>
                    </apex:repeat>
                </table>        

Apex Controller

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

    //Constructor
    public ContactRoleOnOpp7(ApexPages.StandardController controller)
    {
        conId = new List<Id>();
        for(Contact con : [SELECT Id FROM Contact]){
            conId.add(con.Id);
        }
        if(wrapOcrList == Null)
        {
            wrapOcrList = new List<WrapOcrClass>();
            for(OpportunityContactRole ocrList : [SELECT  Contact.Id, IsPrimary, Contact.Name, 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;} //for Checkbox
        public Boolean isSelected {get; set;} //for Radio Button

        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;
    }   

    //Update Roles
    public void updateRole()
    {
        List<OpportunityContactRole> roleUpdate = new List<OpportunityContactRole>();
        if(wrapOcrList!=Null){
            for(WrapOcrClass wrap : wrapOcrList)
            {
                if(wrap.isSelected = true)
                {
                    wrap.ocr.isPrimary = true;
                    System.debug('wrap.ocr.isPrimary is :' + wrap.ocr.isPrimary+  'and wrap.isSelected is :' +wrap.isSelected);
                    roleUpdate.add(wrap.ocr);
                } 
                if(wrap.selected)
                {
                    wrap.ocr.Role = wrap.selectedValue;
                    wrap.selected = false;
                    System.debug('wrap.selectedValue is :' + wrap.selectedValue+  'wrap.selected is :' +wrap.isSelected);
                    roleUpdate.add(wrap.ocr);
                    System.debug('hhh');
                }           
            }
            Update roleUpdate;
        }
    }
}

Upvotes: 0

Views: 475

Answers (1)

eyescream
eyescream

Reputation: 19622

You're displaying same field ({!selectedValue}) in table in every row? Then when Salesforce will parse the variables on the server it will evaluate it multiple times and well, the last version of the values will "win". You have overcomplicated your form.

Put the multiplicklist outside of the table (in page header) and then you just set the picklist & checkboxes, save. Set picklist to something else, checkboxes, save.

Or if you want it really on row level - you'd have to have a separate public String selectedValue {get;set;} in your wrapper class and then reference "this wrapper's" field in the loop, not the "global" one.


Edit to answer question edits & comments...

<input type="radio"> is wrong. I mean you can use it on the page, it's valid HTML... but you'd need some javascript to read it & do something with it. It won't be sent back to Apex if it's not <apex:something...> tag. Check out https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectRadio.htm (yes, it's driven out of same data that apex:selectList, one out of X strings, not Booleans!... Like dropdowns let you select one of X options, same idea.

Except I really don't understand what are you trying to achieve. I mean you'll have 1 radio per record (not one for all, potentially everyone can be ticked and because it's only one in each group (in each record) - it can never be unticked back. It feels like you're overcomplicating it but I don't know what's the end goal.

Upvotes: 0

Related Questions