0

I have my list populating correctly but I don't know how to get the ID value of the selected account back to my controller. Can someone tell me the correct syntax?

Useful bits of my controller:

String[] accounts = new String[]{};

public PageReference test() {
    return null;
}

public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>();
    List<Account> acclist = new list<Account>();
    acclist = [Select ID, Name FROM Account];
    for(Account a:acclist){
        options.add(new SelectOption(a.Id,a.Name));
    }
    return options;
}

public String[] getAccounts() {
    return accounts;
}

public void setAccounts(String[] accounts) {
    this.accounts = accounts;
}

Here is my save operation. Trying to set the ID value for the record.

public void savenew() { 

            admintracking.Account__c = **?????????** 
//tried using accounts string but can't figure out syntax to use ID. 
        try {
            insert adminTracking;
        } catch(DmlException e) {
            ApexPages.addMessages(e);
            System.debug('The following exception has occurred: ' + e.getMessage());
        }

    }
3
  • By your code, it looks like you want to get only one Account ID but in controller you have a list..
    – Ashwani
    Dec 29, 2016 at 21:30
  • The VF page limits them to selecting one value from the list.
    – Matt M
    Dec 29, 2016 at 21:31
  • You should really share the markup...
    – Adrian Larson
    Dec 29, 2016 at 21:43

2 Answers 2

0

The simple approach here would be to use an <apex:selectList> tag almost exactly as laid out in the documentation.

Markup

<apex:selectList value="{!accountId}">
    <apex:selectOptions value="{!accounts}" />
</apex:selectList>

Code

public String accountId { get; set; }
public List<SelectOption> accounts
{
    get
    {
        if (accounts == null)
        { // Lazy Load Pattern
            accounts = new List<SelectOption>();
            for (Account account : [SELECT Name FROM Account])
                accounts.add(new SelectOption(account.Id, account.Name));
        }
        return accounts;
    }
    private set;
}

From there, you would just use adminTracking.Account__c = accountId. Or you can change the markup to bind directly to adminTracking.Account__c, if you have exposed a getter for adminTracking.

<apex:selectList value="{!adminTracking.Account__c}">
1
  • On your For (Account...) line I am getting a "Non-void method might not return a value or might have statement after a return statement." error.
    – Matt M
    Dec 29, 2016 at 21:48
0

The syntax would look like this:-

public void savenew() { 

    if(accounts.size()==1) {
     admintracking.Account__c = accounts[0];
     //tried using accounts string but can't figure out syntax to use ID. 
        try {
            insert adminTracking;
        } catch(DmlException e) {
            ApexPages.addMessages(e);
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
    }
}

This piece of code should work only if accounts has value in it.

Additionally, if you are expecting only one Id then change:-

String[] accounts = new String[]{};

to

String  accountId;

In savenew method you can simple access value as:-

admintracking.Account__c = accounId;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .