You need to sign in to do that
Don't have an account?
Arjun KV
Add value to multiselect picklist through apex code.
Hi All,
How to add value to the multiselect Picklist through apex code.
I have a requirement to get the value from one Object's text field and add that values to Multiselect pick list field in another object.
How to add value to the multiselect Picklist through apex code.
I have a requirement to get the value from one Object's text field and add that values to Multiselect pick list field in another object.
Or do we have two fields with same/similar options and you just want to copy from one record and update the other?
In the Student object, I have a multiselect Pick List Interest__c and I have a text field named Other_Interest__c. If I add anything in the text field (Other_Interest__c), the same value will be added to the multiselect pick list.I hope this will help you to understand -
Trigger StudentTrigger - And now the helper class StudentTriggerHelper -
Below is few screenshots for your reference -
Please let me know if you need any feedback. Thanks.
I need to add those value as MultiselectPickList Options
Thanks for your response.
Let me explain you my senario. I have two custom object Test__c and Testing__c. In Test__c i have a text field called Partner_level__c and in Testing__c i have a MultiSelectPicklistField called Pick_List__c.
When i enter value at Partner_Level__c at Test__c object that value must add to MultiPicklist in Testing__c .
By looking at your code i tried following thing but I am getting error while assigning new value to MultiSelectPicklistField.
Please Look at the code below and provide your input.
Error:Compile Error: Expression cannot be assigned.
Trigger:
trigger PickListTrigger on Test__c (after delete, after insert, after undelete,after update, before delete, before insert, before update) {
if(Trigger.isBefore)
{
if(Trigger.isInsert || Trigger.isUpdate)
{
PickListTriggerHelper.UpdatePartnerLevel(trigger.new);
}
}
}
HelperClass:
public class PickListTriggerHelper {
public static void UpdatePartnerLevel(List<Test__c> PartnerLevel)
{
Schema.DescribeFieldResult Pickvalue= Testing__c.Pick_List__c.getDescribe();
List<Schema.PicklistEntry> PickListValue = Pickvalue.getPicklistValues();
list<string> str= new list<string>();
for(schema.picklistEntry p:PickListValue){
str.add(string.valueof(p));
}
for(Test__c eachPartnerLevel:PartnerLevel)
{
if((eachPartnerLevel.Partner_Level__c!=null)&&isNewPickList(str,eachPartnerLevel.Partner_Level__c))
{
Testing__c.Pick_List__c=Testing__c.Pick_List__c + ';' + eachPartnerLevel.Partner_Level__c;
}
}
}
private static Boolean isNewPickList(list<String> existingPartnerLevels,String newPartnerLevel)
{
for(String eachLevel:existingPartnerLevels)
{
if(eachLevel.equalsIgnoreCase(newPartnerLevel)){
return false;
}
}
return true;
}
}
Please check the below code.
Assumptions:
Below you will find the code -
Trigger: Helper Class:
The screenshots are given below:
Initially TESTING 3 looks like -
Now I have added a record TEST 3 and I put "VALUE 10" in the Partner Level field.
Now the same value i.e. "VALUE 10" is added to the multiselect picklist in TESTING 3.
If this code helps, requesting you to please choose this as Best Answer and mark the question as Solved.
I want to add that new picklist value in Available values not in Chosen Value. For this any solution.
Hari Krishna,
SFDC.
I'm new with APEX, and I need know how populate a Select Field in Opportunity with a list of campaign Name.
I don't understand the code above, but I created the follow code and by this way I can list all campaign name, but I don't know how insert into a select field.
public class Opportunity{
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;
}
}
Opportunity p = new Opportunity();
System.debug(p.getCampanhas());
Can someone help me?
Best Regards
Rafael