I have a Multi-select picklist in account object and I want to display the selected picklist values in contact object using a formula field. I came across many using multiple If conditions. In my scenario, I have 20+ values in Multi-select picklist, how am I supposed to do that
[SalesForce] formula field to display multi select picklist (selected values)
Related Solutions
Safe Harbor: this is a really long answer...read at your own risk. Also, none of this sample code has been tested, this is just me stubbing it out in Notepad++.
This is not currently possible with standard Salesforce configuration, as multi-select picklists cannot be selected as the Controlling Field in a Field Dependency. So, you're definitely looking at a VisualForce solution; your approach here really depends on your requirements. Does it need to be easily configurable, or is it feasible to hardcode the dependencies in your controller?
If it's acceptable to hardcode the dependencies in your controller, then you'll probably want to set up maps of the dependencies to simplify your code (I use the word "simplify" loosely here)...
public class someController {
public String[] parentPicklistVal {public get; public set;}
public String[] childMultiPicklistVal {public get; public set;}
public String childSinglePicklistVal {public get; public set;}
// maps to hold your dependencies between picklists
private Map<String, List<String>> parentDepMap;
private Map<String, List<String>> childDepMap;
private String[] parentOpts = new String[] { 'parent option 1', 'parent option 2' };
private String[] childMultiOpts = new String[] { 'child multi 1', 'child multi 2', 'child multi 3' };
private String[] childSingleOpts = new String[] { 'child single 1', 'child single 2', 'child single 3' };
public someController() {
// init dependency maps
parentDepMap = new Map<String, List<String>>();
childDepMap = new Map<String, List<String>>();
// pick which child options to display for which parent value
parentDepMap.put(parentOpts[0], (new String[]{childMultiOpts[0], childMultiOpts[1]}));
parentDepMap.put(parentOpts[1], (new String[]{childMultiOpts[1], childMultiOpts[2]}));
// pick which single-select options to display for which child value
childDepMap.put(childMultiOpts[0], (new String[]{childSingleOpts[0], childSingleOpts[2]}));
childDepMap.put(childMultiOpts[1], (new String[]{childSingleOpts[1], childSingleOpts[2]}));
childDepMap.put(childMultiOpts[2], childSingleOpts); // or if you want to show them all?
}
public List<SelectOption> getParentPicklistOptions() {
List<SelectOption> selectOpts = new List<SelectOption>();
for ( String s : parentOpts )
selectOpts.add(new SelectOption(s, s));
return selectOpts;
}
public List<SelectOption> getChildMultiPicklistOptions() {
List<SelectOption> selectOpts = new List<SelectOption>();
if ( parentPicklistVal != null && parentPicklistVal.size() > 0 ) {
// build a set of values to avoid dupes, since there may be overlapping dependencies
Set<String> possibleOpts = new Set<String>();
for ( String val : parentPicklistVal )
possibleOpts.addAll(parentDepMap.get(val));
for ( String s : possibleOpts )
selectOpts.add(new SelectOption(s, s));
}
return selectOpts;
}
public List<SelectOption> getChildSinglePicklistOptions() {
List<SelectOption> selectOpts = new List<SelectOption>();
if ( childMultiPicklistVal != null && childMultiPicklistVal.size() > 0 ) {
// build a set of values to avoid dupes, since there may be overlapping dependencies
Set<String> possibleOpts = new Set<String>();
for ( String val : childMultiPicklistVal )
possibleOpts.addAll(childDepMap.get(val));
for ( String s : possibleOpts )
selectOpts.add(new SelectOption(s, s));
}
return selectOpts;
}
public PageReference actionUpdatePicklistVals() {
// this doesn't really need to do anything, since the picklists should be updated when their getters call after returning
return null;
}
}
And then your VF page...
<apex:page controller="someController">
<apex:form>
<apex:outputPanel id="panel1">
<apex:selectList value="{!parentPicklistVal}" multiselect="true" size="3">
<apex:selectOptions value="{!parentPicklistOptions}" />
<apex:actionSupport event="onchange" action="{!actionUpdatePicklistVals}" rerender="panel1" />
</apex:selectList>
<apex:selectList value="{!childMultiPicklistVal}" multiselect="true" size="3">
<apex:selectOptions value="{!childMultiPicklistOptions}" />
<apex:actionSupport event="onchange" action="{!actionUpdatePicklistVals}" rerender="panel1" />
</apex:selectList>
<apex:selectList value="{!childSinglePicklistVal}" multiselect="false" size="1">
<apex:selectOptions value="{!childSinglePicklistOptions}" />
</apex:selectList>
</apex:outputPanel>
</apex:form>
</apex:page>
However, if this needs to be easily configurable, then you're looking at a custom object with a lookup relationship to itself. You can then query the dependencies and build the maps from them instead of hardcoding them. Just throwing some ideas out there, let's say this custom object uses a Name field for its value, a FieldDep__c field as a lookup to it's controlling value, and a WhichPicklist__c field to control which picklist it belongs to (for this I'll just say it's an integer with possible values of 1, 2, and 3).
public class someController {
public String[] parentPicklistVal {public get; public set;}
public String[] childMultiPicklistVal {public get; public set;}
public String childSinglePicklistVal {public get; public set;}
private Map<String, List<String>> parentDepMap;
private Map<String, List<String>> childDepMap;
public someController() {
FieldDep__c[] deps = [select id, name, fielddep__c, fielddep__r.name, whichpicklist__c from FieldDep__c order name asc];
// init the same maps that we used in the example above
parentDepMap = new Map<String, List<String>>();
childDepMap = new Map<String, List<String>>();
for ( FieldDep__c dep : deps ) {
if ( dep.whichpicklist__c == 1 ) {
// shouldn't really need to do anything here...
} else if ( dep.whichpicklist__c == 2 ) {
// add to the dependency map between the parent and child multi-select picklist
String parentVal = dep.fielddep__r.name;
if ( parentDepMap.containsKey(parentVal) )
parentDepMap.get(parentVal).add(dep.Name);
else
parentDepMap.put(parentVal, new String[]{dep.Name});
} else if (dep.whichpicklist__c == 3 ) {
// add to the dependency map between the child multi-select and child single-select picklist
String parentVal = dep.fielddep__r.name;
if ( childDepMap.containsKey(parentVal) )
childDepMap.get(parentVal).add(dep.Name);
else
childDepMap.put(parentVal, new String[]{dep.Name});
}
}
}
public List<SelectOption> getParentPicklistOptions() {
List<SelectOption> selectOpts = new List<SelectOption>();
String[] parentOpts = parentDepMap.keySet();
for ( String s : parentOpts )
selectOpts.add(new SelectOption(s, s));
return selectOpts;
}
public List<SelectOption> getChildMultiPicklistOptions() {
List<SelectOption> selectOpts = new List<SelectOption>();
if ( parentPicklistVal != null && parentPicklistVal.size() > 0 ) {
// build a set of values to avoid dupes, since there may be overlapping dependencies
Set<String> possibleOpts = new Set<String>();
for ( String val : parentPicklistVal )
possibleOpts.addAll(parentDepMap.get(val));
for ( String s : possibleOpts )
selectOpts.add(new SelectOption(s, s));
}
return selectOpts;
}
public List<SelectOption> getChildSinglePicklistOptions() {
List<SelectOption> selectOpts = new List<SelectOption>();
if ( childMultiPicklistVal != null && childMultiPicklistVal.size() > 0 ) {
// build a set of values to avoid dupes, since there may be overlapping dependencies
Set<String> possibleOpts = new Set<String>();
for ( String val : childMultiPicklistVal )
possibleOpts.addAll(childDepMap.get(val));
for ( String s : possibleOpts )
selectOpts.add(new SelectOption(s, s));
}
return selectOpts;
}
public PageReference actionUpdatePicklistVals() {
// this doesn't really need to do anything, since the picklists should be updated when their getters call after returning
return null;
}
}
You would then want to populate FieldDep__c records from the top down so that you can add a lookup to the controlling value for each record.
I hope I didn't go too overboard with this! If I get a chance sometime today I'll put this together in an org to see how valid my code works, but it sounded like you were just looking for a push in the right direction (although this answer may have been slightly more than that...)
What about this?
if(ISBLANK(Video_Monetizing_tool__c),NULL,IF(INCLUDES(Video_Monetizing_tool__c,'No'),0,10))
Related Topic
- [SalesForce] get selected values of Picklist (Multi-Select) as list of string
- [SalesForce] Populate a field with values that were selected in a multi-select picklist
- [SalesForce] Changing Semicolon from Multi-Select Picklist values to a Comma
- [SalesForce] Multi-Select Picklist in Apex
- [SalesForce] Trigger to update Multi-Select Picklist
Best Answer
To update a contact object formula field based on the values from the account object multi-select picklist . In contact formula field write down as
and goes on. When my formula field has more characters this is the error I faced
The formula expression is invalid: Formula is too long (6,647 characters). Maximum length is 3,900 characters . In that case, try to use trigger its a good choice.