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 Add new value in a Custom Picklist with APEX?

Hi,

I want populate a custom picklist on Opportunity page with all Campaign that exist.

I can get a list of all names of the campaign, but I don't know how populate the custom picklist with these values.

Here is the code with list of the campaign name.

public class PegarCampanhas{

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;   
}
}

Here is what I'm traying to insert:

trigger InserirCampanhas on Opportunity (before insert, before update) {

        List<SelectOption> options = new List<SelectOption>();
        PegarCampanhas pegar = new PegarCampanhas();
        options = pegar.getCampanhas();
        System.debug(options);
        
        for(Opportunity opty : Trigger.new){
            if(options!=null){
                opty.Campanha__c = options.get(i).getValue();
                System.debug(opty.Campanha__c);
            }
        }
}

When I test it occurs this error:
Line: 2, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, InserirCampanhas: data changed by trigger for field Campanha: valor incorreto para campo de lista de opções restrita: GC Product Webinar - Jan 7, 2002: []

I think this occurs because my trigger don't insert the value on the field, but is trying to select a value that don't exist on the picklist.

Can someone help me?

Regards
Rafael

 
RD@SFRD@SF
Hi Rafael,

Are you using this on a Visual force page or standard page

Regards
RD
Rafael.Martins.SantosRafael.Martins.Santos
Hi RD,

I'm using standard page.
RD@SFRD@SF
Rafael,

You are right about the error message its coming because you are not trying to add the picklist values in the object but you are setting the value in the record.

To update the objects picklist. use the metadata api

follow the link (https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_updateMetadata.htm)to know more 

Regards
RD
 
Rafael.Martins.SantosRafael.Martins.Santos
RD,

Do you know if is possibel to update the metadata with apex?
RD@SFRD@SF
Iago FelicioIago Felicio

Hi Rafael,

I did some tests here what I found out was that maybe the field isn't accessible through its API name.

I first created the trigger: (Partially in Brazillian Portuguese)

trigger PopulateCampaignIntoOpps on Opportunity (after insert, after update) {

    //Listar Campanhas numa lista
	List<Opportunity> oportunidades = Trigger.new;
    
    //Lista de Campanhas ativas
    List<Campaign> campanhas = [SELECT Id, Name, IsActive FROM Campaign WHERE IsActive = true];
    
    //Set de ids das campanhas
    Set<Id> idsCampanhas = new Set<Id>();
    for(Campaign camp : campanhas){
        idsCampanhas.add(camp.id);
    }
    
    //Atualizar a Opportunity 
    for(Opportunity oport : oportunidades){
        for (Id idcamp : idsCampanhas){
        	oport.CampaignId = idcamp;
        }
    }
}
I even created a Test to check my trigger: (Partially in Brazillian Portuguese)
@isTest(seeAllData=TRUE)
private class PopulateCampaignIntoOppsTest {
	
    private static testMethod void test() {	
        Opportunity opp = new Opportunity();
        opp.Name = 'Teste Opp';
        opp.CloseDate = Date.newInstance(2018, 2, 17);
        opp.StageName = 'Prospecting';
        insert opp;
    }
}

When I run the test I get the following output:

User-added image

So, I believe that the solution is to find a way to get acces to that variable or even chnge the security level of that field. I'm looking for  solution but I didn't find yet.

Hope it helps you :D

Regards,
Iago

Rafael.Martins.SantosRafael.Martins.Santos
Hi Iago,

​Thanks, for your help, but I'm stuck in the same place than you.
I can list all the campaign, but not insert into the field.

I will see the link that the RD sent me, and test if will work.

RD, thanks again for your help, I will see this link now.
Regards
Rafael