function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex SolodyankinAlex Solodyankin 

How to convert multi-select picklist to a text field on Cases

Our organization has been in dire need of being able to convert a multi-select picklist value on our Case into text fields. We've discovered we can split the array by ';', though we're seeing multiple ways people have done it and are not able to create a working trigger. We are hoping to limit the trigger based on the record type, 'Applications', but could simply limit the trigger based on the multi-select picklist being blank, though not sure about a simple way to do that. We are trying to populate the text field: S1_High_School_Electives__c with the potentially multiple values of High_School_Electives_Desired__c. I tried manipulating the code from https://developer.salesforce.com/forums?communityId=09aF00000004HMGIA2#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F0000000B1n0IAC

I'm not sure if I also need to create the class like that post has, and how that would be embedded in the trigger. The error I get is that a DML statement cannot operate on a trigger.new. I'm thinking a SOQL query could be made instead as well.
trigger UpdateHSElectivesMSPV1 on Case (after update) {



for(Case c : Trigger.new) {
String pickValuesStr;
    if(!String.isBlank(c.High_School_Electives_Desired__c)){
        List<String> pickValues = c.High_School_Electives_Desired__c.split(';');
        for(String str : pickValues){
            if(String.isBlank(pickValuesStr)){
                pickValuesStr = str;
                }
                 else{
                     pickValuesStr = pickValuesStr + '\n' + str;
                    } 
                }
            
  c.S1_High_School_Electives__c = pickValuesStr;

}

  Update c;
  }
  }

 
Best Answer chosen by Alex Solodyankin
Raj VakatiRaj Vakati
try this
 
trigger UpdateHSElectivesMSPV1 on Case (before update) {
	for(Case c : Trigger.new) {
		if(!String.isBlank(c.High_School_Electives_Desired__c)){
			List<String> pickValues = c.High_School_Electives_Desired__c.split(';');
			String pickValuesStr ='' ;
			for(String str : pickValues){
						 pickValuesStr = pickValuesStr + '\n' + str;
			}
			  c.S1_High_School_Electives__c = pickValuesStr;

		}
}
}

 

All Answers

Raj VakatiRaj Vakati
try this
 
trigger UpdateHSElectivesMSPV1 on Case (before update) {
	for(Case c : Trigger.new) {
		if(!String.isBlank(c.High_School_Electives_Desired__c)){
			List<String> pickValues = c.High_School_Electives_Desired__c.split(';');
			String pickValuesStr ='' ;
			for(String str : pickValues){
						 pickValuesStr = pickValuesStr + '\n' + str;
			}
			  c.S1_High_School_Electives__c = pickValuesStr;

		}
}
}

 
This was selected as the best answer
AshishkAshishk
Use before trigger that way you dont need to update record again.

After trigger you are using will go in infinite loop, to solve it you need to add static boolen (better will be before trigger).

Thanks,
Ashish
Alex SolodyankinAlex Solodyankin
Thank you Raj and Ashishk. Can you explain the "String pickValuesStr ='' ;" Are we simply setting a blank default value prior to populating it with the following for statement? 
And yes Ashishk, the loop makes sense that it will keep updating after each update.
Ideally, we need to look at 4 different fields in an if then else statement. How would I potentially add an 'or' or 'if' statement to include splitting the S1_High_School_Electives__c field. If we are able to reference multiple if isblank references, then we can just dump the string values into c.Electives_Desired__c.