+ Start a Discussion
Francisco Riccomagno 1Francisco Riccomagno 1 

Change the picklist type depending on a record type

Hello, I'm totaly new to the salesforce development world, so maybe this is not the correct question to make. 
I'm doing a quick app to try to understand how to develop on salesforce and I came to a moment where I don't know what to do. I need 2 different user to be able to choose a value from a picklist. The thing is that one user should be able to choose more than one value, an the other only one.
I tried to solve this by creating 2 different profiles, and try to create a record type, but the problem is that with the record type I can limit the values shown in the picklist, but can't change from a "Only one" selection to a Muptiple selection.
Maybe my way of thinking the solution is wrong, but is there a way of doing this? If not, how should be my way of thinking the solution to this?
Thank you in advance.
Best Answer chosen by Francisco Riccomagno 1
Scott McClungScott McClung
Hi Francisco
I really can't think of a good way to do what you're looking for without creating a custom validation in apex.  
If you're up for that, the values in a multi-select picklist are stored as a semi-colon separated string so it's a simple matter of splitting the value of the field on the semi-colons and counting the elements in the resulting array.  If the rules are specific to the user rather than the records, it's probably better to base your validation on the user's profile or role rather than the record types.
Here's an example of a custom validation you could use for this.  Call this method from a before insert or before update trigger to enforce your business rules.

public with sharing class MyCustomValidation 
{
  public void ValidateMyCustomField(List<Account> lstAccounts)
  {
    //Get the profile name for the current user
    String strProfileId = UserInfo.getProfileId();
    Profile objProfile = [SELECT Name FROM Profile WHERE Id = :strProfileId][0];

    for(Account objAccount : lstAccounts)
    {
      String[] strArray = objAccount.MyCustomField__c.split(';'); //Split the multi-select picklist into an array
      if(objProfile.Name == 'Sales Rep' && strArray.size() > 1) //Test the array size along with the profile name
      {
        objAccount.addError('Silly sales rep, only one option is allowed.'); //Generate an error on the record
      }
    } 
  }
}

Hope that helps. :)

All Answers

kevin lamkevin lam
Have you tried assigning two different default record types to those profile, one of them with a single value?
Francisco Riccomagno 1Francisco Riccomagno 1
kevin, I don't understand what you mean by two different default record types. Maybe my explanation was not the best. Think of a form where a sales person needs to choose only one state. Now, that same form, if the user is a support person, should be able to choose several states. That's why I was thinking on record types and profiles, but can't fin the way of solving this.
Scott McClungScott McClung
Hi Francisco
I really can't think of a good way to do what you're looking for without creating a custom validation in apex.  
If you're up for that, the values in a multi-select picklist are stored as a semi-colon separated string so it's a simple matter of splitting the value of the field on the semi-colons and counting the elements in the resulting array.  If the rules are specific to the user rather than the records, it's probably better to base your validation on the user's profile or role rather than the record types.
Here's an example of a custom validation you could use for this.  Call this method from a before insert or before update trigger to enforce your business rules.

public with sharing class MyCustomValidation 
{
  public void ValidateMyCustomField(List<Account> lstAccounts)
  {
    //Get the profile name for the current user
    String strProfileId = UserInfo.getProfileId();
    Profile objProfile = [SELECT Name FROM Profile WHERE Id = :strProfileId][0];

    for(Account objAccount : lstAccounts)
    {
      String[] strArray = objAccount.MyCustomField__c.split(';'); //Split the multi-select picklist into an array
      if(objProfile.Name == 'Sales Rep' && strArray.size() > 1) //Test the array size along with the profile name
      {
        objAccount.addError('Silly sales rep, only one option is allowed.'); //Generate an error on the record
      }
    } 
  }
}

Hope that helps. :)

This was selected as the best answer
Saurabh DhobleSaurabh Dhoble
Are you familiar with Visualforce ? If so, you can look at this post to do exactly what you want -

http://writeforce.blogspot.com/2012/11/the-trouble-with-picklists-visualforce.html