1

I am trying to set all values of a picklist from a list of selectOptions. I've created the options like this:

List<selectOption> options = new List<selectOption>();

...

for (Account act : providers)
{
    options.add(new Selectoption(act.Name, act.PersonContactId));
}

...but I can't seem to figure out how to assign these options to my picklist.

Any help is greatly appreciated.

2

Based on your comments, let me see if I can address this.

Picklists on page layouts.

These are static values. Yes, there are dependent picklists. Picklists can also be filtered by record type. But there is no hook to dynamically populate a picklist on the user interface using Apex code, unless you use Visualforce.

Triggers

Triggers are invoked on save, delete, and undelete. In order for a trigger to do what you want ("I have a custom picklist field in my Task object that I'm trying to populate by a trigger.") it would somehow have to fire when the UI is rendered. This is not what Triggers do.

Dynamic Picklists and Visualforce

The only way to dynamically populate a picklist in the Salesforce UI with data is in a Visualforce page where the Apex code in the custom controller or extension performs the query and populates the picklist. It isn't possible otherwise.

So what are your options?

...you must be asking at this point.

Well, looking at your code and reading into it a bit, this would be a great opportunity for a lookup relationship. Typically this is the option you use when the related data is coming from another object. Unfortunately you are working with Task. And Task does not support custom fields that are lookups (or any relationship, for that matter).

In reality, the only option I can see is Visualforce.

But...maybe you should take a step back. You described a coding problem. It might be useful to understand what the business problem is that needs to be solved. Often people new to the platform (reading into things here) attempt a solution in code when there is another solution already out there. I'd suggest another post detailing that business problem, how you've tried to solve it, and what hasn't worked. Who knows...there might be something else out there.

  • Thanks for your thorough reply @peter. Since posting this, I have gone the VF route with success (I should've answered my own question here, sorry). I did exactly what you've suggested. I created a custom VF Task page that mimicked the standard Task edit page and created an extension class to retrieve my picklist options and add them to a VF picklist. Worked like a champ. Thank you so much for you help. I'll mark your reply as the correct answer. – Adam H. Mar 11 '14 at 13:35
1

this should help:

http://blogs.developerforce.com/developer-relations/2008/12/using-the-metadata-api-to-retrieve-picklist-values.html

public List<SelectOption> getCountries() {
  List<SelectOption> options = new List<SelectOption>();

  Schema.DescribeFieldResult fieldResult = OfficeLocation__c.Country__c.getDescribe();
  List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

  for( Schema.PicklistEntry f : ple) {
    options.add(new SelectOption(f.getLabel(), f.getValue()));
  }       
  return options;
}

just replace Schema.DescribeFieldResult fieldResult = OfficeLocation__c.Country__c.getDescribe(); with Schema.DescribeFieldResult fieldResult = YourObject__c.YourField__c.getDescribe();

  • Thanks for the quick reply SalesForceGirl. Unfortunately, I'm not using an VisialForce page. I have a custom picklist field in my Task object that I'm trying to populate by a trigger. Is this even possible? Only examples I can find are for VisualForce fields and controllers. – Adam H. Feb 7 '14 at 19:34
  • 1
    Ahhh, I think you are asking the wrong question then. What you want isn't actually possible. You might be looking more for a dependent picklist? – dphil Feb 7 '14 at 19:43
0

This link should describe it pretty good

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_selectList.htm

  • Thank you dphil, but I'm actually trying to populate a custom picklist field that is on the standard Task object via a trigger. Any suggestions? – Adam H. Feb 7 '14 at 19:53
  • Not possible. What is the use case for this anyways? Can't you use more of a dependent picklist for this? – dphil Feb 7 '14 at 20:12
  • Thank you for the reply dphil. Unfortunately, I can't use a dependent picklist. I am trying to create a picklist on my Task layout that I can populate with the contacts related to the account when the task edit page is opened. This is an attempt to work around SFD's lookup filter short-comings. I really want to avoid a visual force page. Do you have any other suggestions? Thanks in advance. – Adam H. Feb 10 '14 at 13:49
  • 1
    Honestly, I do not believe what you want is possible without a Visualforce page. Sorry. – dphil Feb 10 '14 at 16:06

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.