function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CuriousJohnCuriousJohn 

How do you assign to a Picklist programmatically?

I'm trying to upsert an Object that has required Picklist Fields.

 

I've written code that constructs a list of Ooportunities objects that I want to upsert in an Apex controller:

 

public List<Opportunity> DonationData = new List<Opportunity>();
upsert DonationData;

 

But there's a required Picklist field called Stage in the Opportunity Object and I must load this field or the upsert operation fails with this error:

 

Error:
Required fields are missing: [Opportunity Name, Stage]

 

The Stage field is not important to me, and so. I have been trying put stub data in it:

* Integers 0 & 1 don't work

* String 'Close Won' doesn't work

 

There is no PickList object, to my knowledge, so there are no Picklist methods.

 

QUESTION: How do you assign a picklist value programmatically?

 

Saurabh DhobleSaurabh Dhoble
It should be taking the "Close Won" value - can you post your code here. Also, if you could post the exact error message from your data set that would help as well.
Rakesh KumarRakesh Kumar
According to you post I think you are upserting new object. So you assign those list to newly created list object. Otherwise you should post your code and error.
Shailesh DeshpandeShailesh Deshpande
I dont see anywhere you setting the name and the stage field from what you have posted.

Picklists values are stored as String in the system. So assgning a string should work. Along with the stage dield there are several other fields which are mandatory. You need to set them too.
SharathChandraSharathChandra

And you list is also not containing any data that shpould be updated?????

in the provided code you list DontationData is empty.

pluviosillapluviosilla

My discussion of upserting only misled & confused everyone, and it is not really relevant. Apologies!!

 

I was getting compiler errors when trying to assign to the Stage Picklist:

 

Error: MyController Compile Error: Invalid field Stage for SObject Opportunity at line 239 column 17

And I assumed my problem was a data type mismatch. But it was simply that I was using the wrong API name for the Stage field. I should have written: op.StageName = "Closed Won" instead of op.Stage = "Closed Won".

 

Sorry for the wild goose chase. This is what happens when a 60 year old who hasn't programmed in 15 years tries his hand at a technical role in a new technology.

 

Thanks to all of you, especially Saurabh Dhoble, for clarifying that I ought to be able to assign a string to the StageName Picklist field. Wouldn't hurt if that were mentioned in the documentation page for the Opportunities Object.