You need to sign in to do that
Don't have an account?
Rafael.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
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
Are you using this on a Visual force page or standard page
Regards
RD
I'm using standard page.
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
Do you know if is possibel to update the metadata with apex?
Please follow the link (http://salesforce.stackexchange.com/questions/49057/adding-values-to-a-picklist-using-apex)
Regards
Rd
Hi Rafael,
I even created a Test to check my trigger: (Partially in Brazillian Portuguese)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)
When I run the test I get the following output:
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
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