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
Kon DeleKon Dele 

How to copy multi-select picklist value strings into Long Text Area field

I am trying to copy multi-select picklist values into a Long Text Area field. The issue I am running into is I'm able to copy over a single value, but when multiple values are selected it throws an error.
User-added image
How can I split the String value so that the selection is copied into the Long Text Area with line breaks as follows:
Germany
Kentucky

Here's my code:
public class updateMultiSelectPicklistField {
    public static void updateMSelectPickField (ContentVersion[] ContentVersion1){
        for(ContentVersion c: ContentVersion1){        
            c.Main_Offices__c = c.Office__c;  
        }
    }
}
and trigger:
trigger updatefields on ContentVersion (before insert, before update) {
    ContentVersion[] ContentVersion1 = Trigger.new;
    updateMultiSelectPicklistField.updateMSelectPickField(ContentVersion1);
}


 
Best Answer chosen by Kon Dele
Bhanu MaheshBhanu Mahesh
Hi Kon,

Try below code 
public class updateMultiSelectPicklistField {
    public static void updateMSelectPickField (ContentVersion[] ContentVersion1){
        for(ContentVersion c: ContentVersion1){        
            String pickValuesStr;
			if(!String.isBlank(c.Office__c)){
				List<String> pickValues = c.Office__c.split(';');
				for(String str : pickValues){
					if(String.isBlank(pickValuesStr)){
						pickValuesStr = str;
					}
					else{
						pickValuesStr = pickValuesStr + '\n' + str;
					}
				}
			}
			c.Main_Offices__c = pickValuesStr;  
        }
    }
}

Mark it as "SOLVED" if your query is Answered

Regards,
Bhanu Mahesh

All Answers

Bhanu MaheshBhanu Mahesh
Hi Kon,

Try below code 
public class updateMultiSelectPicklistField {
    public static void updateMSelectPickField (ContentVersion[] ContentVersion1){
        for(ContentVersion c: ContentVersion1){        
            String pickValuesStr;
			if(!String.isBlank(c.Office__c)){
				List<String> pickValues = c.Office__c.split(';');
				for(String str : pickValues){
					if(String.isBlank(pickValuesStr)){
						pickValuesStr = str;
					}
					else{
						pickValuesStr = pickValuesStr + '\n' + str;
					}
				}
			}
			c.Main_Offices__c = pickValuesStr;  
        }
    }
}

Mark it as "SOLVED" if your query is Answered

Regards,
Bhanu Mahesh
This was selected as the best answer
Kon DeleKon Dele
Thank you Bhanu! That worked perfectly!
Kon DeleKon Dele
Using the code above, is there a way I can dynamically select all values in the multiselect based on if a checkbox is true or false?