1

Is there any way to for loop through many records and put a new radio button + name/id/something of that record?

Also, is this done with VF page or controller?

Last, is it be possible to get a small demo of this thing? I'm not able to search my problem by google, Im having problem putting it in words.

Thanks to you.

EDIT :

I'm trying to get a list of contacts associated to 1 account record, put radio button next to each, and display. User will tick one radio button, click submit button and it will update special field on account record with that value of contact next to radio button ticked.

Im sorry if question not well understood. I try to add more info.

(o) Contact 1
(o) Contact 2
.
.
.
(*) Contact 9 (we pick this one)
(o) Contact 10

[Submit] [Cancel]

Above will update field X on account record to "Contact 9".

  • 2
    Welcome to Salesforce StackExchange. I'm afraid you will need to provide some additional information on what your exact requirement is. Possibly update your question with a mockup of what you want to see on the screen. – Guy Clairbois Jun 29 '16 at 11:43
1

Heres a quick example, based off the SelectRadio demo code.

This controller provides a list of SelectOption's, which is populated with the contact name and ids from a given accountId. Adding ?AccountId= with a valid Id after it to the url of the page will provide the needed Id. For this example, the Save method puts the selected contact id inside a non-existent field on the account.

public class TestPageController {

    public Id AccountId { get; set; }
    public Id selectedContactId { get; set; }

    public TestPageController() {
        AccountId = ApexPages.CurrentPage().getParameters().get('AccountId');
    }

    public List<SelectOption> getContacts() {
        List<Contact> contacts = [SELECT Name, ID FROM Contact WHERE AccountId = :AccountId];
        List<SelectOption> contactOptions = new List<SelectOption>();

        for (Contact contact:contacts) {
            contactOptions.add(new SelectOption(contact.Id, contact.Name));
        }

        return contactOptions;
    }

    public PageReference Save() {
        Account account = new Account(Id= AccountId);

        account.put('Some_Custom_Field__c', selectedContactId);

        update account; 

        return new PageReference('/' + AccountId);
    }
}

The page used is fairly simple, its mostly just a form with little to no styling. The SelectRadio tag is where we need to pass the list of SelectOption's we create inside the controller. We can do this via the SelectOptions tag, nested in the SelectRadio. Additionally, we specify where the selected value will be saved on the SelectRadio tag through the value attributte. Finially, the save button runs the custom save method to update the account.

<apex:page controller="TestPageController">
    <apex:form>
        <apex:selectRadio value="{!selectedContactId}">
            <apex:selectOptions value="{!contacts}" />
        </apex:selectRadio>

        <apex:commandButton value="Save" action="{!Save}" />
    </apex:form> 
</apex:page>
  • Hello battery.cord! – fraglord Jun 30 '16 at 5:52
  • I think I understood what your code is doing! Just a few Q's. (1) in line 10, why is WHERE AccountId = :AccountId ? Its same. (2) in line 24 account.put('Some_Custom_Field__c', selectedContactId); account var is not collection, how is this possible? (3) in line 28 return new PageReference('/' + AccountId); what does this do? Thanks for helping! EDIT: how to insert enter? It saves comment instead of enter?? – fraglord Jun 30 '16 at 5:58
  • Also, now Im 101% sure how to do Selectioption radio buttons. Just need to see how to make VF page button and how to save selected contact in new custom account field. Thannks very much1 – fraglord Jun 30 '16 at 6:09
  • Tentative answer marked as yes, it is semi-usable with some testing :) Thanks battery.cord once again! – fraglord Jun 30 '16 at 7:29
  • 1
    Id like to avoid any further discussion in comments (its generally bad form on stackexchange, since it requires any future readers to sift through the comments instead of just reading the answer), but I would like to recommend that you run through the Developer Trailhead. Its an excellent introduction to the force.com platform and features. – battery.cord Jun 30 '16 at 14:38

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.