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
Rafael.Martins.SantosRafael.Martins.Santos 

How to insert a list in a custom field of type Select List with Apex?

Hi,
I'm trying add in my custom field of type Select List, all the Campaign that I have.
So I want know how do that using apex?

I'm new with apex, and I don't found anything that explain how I insert a value in a field.

Thanks
Rafael
Sean McMickle 10Sean McMickle 10
objectName.FieldName = 'Insert your data here';

For instance, updating the contact's first name..

contact.firstName = 'Sean'

Don't forget to update the record!

update contact;
 
Rafael.Martins.SantosRafael.Martins.Santos
Hi Sean thanks,
How I add a list of values in a select list field?

I don't know what I'm doing wrong, but I'm using a code like that:

public List<SelectOption> getCampanhas(){
    List<SelectOption> options = new List<SelectOption>();
    List<Campaign> campanhas = [SELECT Id, Name, IsActive FROM Campaign WHERE IsActive = true];
    for(Campaign c:campanhas){
        options.add(new SelectOption(c.Name, c.Name));
    }
    return options;   
}

Now I must add the values of this list in my custom field(Select List).

the name of my custom field is Campanha__c.

Opportunity opty = new Opportunity();
Opportunity.Campanha__c = 
update opty;

This is the example that you say above.

Do you know how I can populate this field with these values?

Regards
Rafael
Sean McMickle 10Sean McMickle 10

Ahh ok, a visualforce page I see. 

Check out this page :: 


https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectOptions.htm

Essentially what you will do is assign the selected value to a variable from your controller. 

Then you will do something like this

Opportunity opty = new Opportunity();
opty.Campanha__c = variableName;
insert opty;

It appears as if you are trying to create a new opportunity. Remember that you will need to insert a value for all the required fields as well.

I would honestly look at some trailheads for apex if I were you, they are super handy for someone at your level.

Rafael.Martins.SantosRafael.Martins.Santos
Hi Sean,
Sorry for the late,
I will see these documents and I let you know the results.
Rafael.Martins.SantosRafael.Martins.Santos
Hi Sean,

I don't understand yet how I make the relationship between Apex code with the Opportunity page (Not a VisualForce Page).

I see in the documents that show how create visual pages and apex code, but I don't see anything or an example of how to create APEX CODE and using in a standard page for example opportunity page.

Like I said above, in this topic, I want fill a Custom Choice List on the opportunity page. And you give me a code, and I tested in the console, and it work.
But I don't have any idea of how I test in the page, trying populate the field.

Can you help me on this case?

Regards
Rafael