Find your content:

Search form

You are here

Multi Picklist Validation via Apex?

 
Share

I have a question for you

Aim – Create a validation rule, where only 5 picklist values can be choosen

I have the following fields.

  • Institutional_Products_A_G__c
  • Institutional_Products_G_Z__c
  • Intermediary_Products_A_F__c
  • Intermediary_Products_G_L__c
  • Intermediary_Products_M_Z__c

Each field is a multi picklist. Each field contains over 100 values I have created the following validation rule (see below) which works, however I am unable to put all the values in due to the size.

Any one got any ideas, how I can get around this ?

IF(INCLUDES( Institutional_Products_A_G__c , "Agricultural Fund"),1,0) + 
IF(INCLUDES( Institutional_Products_A_G__c , "All Maturities Corporate Bond"),1,0) + 
IF(INCLUDES( Institutional_Products_A_G__c , "All Maturities Index Linked Bond"),1,0) + 
IF(INCLUDES( Institutional_Products_A_G__c , "Asia Diversified Growth"),1,0) + 

IF(INCLUDES(Institutional_Products_G_Z__c , "Global Property Securities"),1,0) + 
IF(INCLUDES(Institutional_Products_G_Z__c , "Global Resource Equity"),1,0) + 
IF(INCLUDES( Institutional_Products_G_Z__c , "Global Resources Equity Fund"),1,0) > 4


Attribution to: user7605

Possible Suggestion/Solution #1

You could use an apex trigger for this. Would look somewhat like this (not tested):

trigger ObjectNameCheckProducts on Campaign (before insert, before update) {
    for(Campaign o : Trigger.New){
        Integer counter = 0;

        if(o.Institutional_Products_A_G__c != null) 
           counter += o.Institutional_Products_A_G__c.split(';').size();
        if(o.Institutional_Products_G_Z__c != null)
           counter += o.Institutional_Products_G_Z__c.split(';').size();
        if(o.Intermediary_Products_A_F__c != null)
           counter += o.Intermediary_Products_A_F__c.split(';').size();
        if(o.Intermediary_Products_G_L__c != null)
           counter += o.Intermediary_Products_G_L__c.split(';').size();
        if(o.Intermediary_Products_M_Z__c != null)
           counter += o.Intermediary_Products_M_Z__c.split(';').size();

        if(counter > 5) o.addError('Max 5 values can be selected from Product lists');
    }    
   } 

Attribution to: Guy Clairbois
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31157

My Block Status

My Block Content