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
Marie KagayMarie Kagay 

Method to set proper case in text field

I have a text field on Opportunity Product that is updated via apex with the values of a multi-select picklist fields. A number of values from the multi-select picklist are all uppercase and at this time need to remain so. 
However, we'd like the all uppercase values to be updated to proper case when the apex code brings them over to the text field. (I'm defining proper case and first letter of a word is upper case, the rest are lower case). Since the values are all seperated by semi-colons when updated in the text field, I would like to have this update run on every value separated by a semi-colon.
For example, if the multiselect picklist has values "JAPAN;CANADA;PERU", my current apex code updates my text field with "JAPAN;CANADA;PERU". Is there a method I could use that would update the text field to "Japan;Canada;Peru"?

Thanks!
Tejpal KumawatTejpal Kumawat
Hello Marie,

Place below method in your controller & pass your picklist value in this method..
 
public String setProperCase(String str){
    String upDatedstr = '';
    if(str != null)
        for(String s : str.split(';') ){
            if(upDatedstr == '')
                upDatedstr = s.substring(0,1).toUpperCase()+''+s.substring(1, s.length()).toLowerCase();
            else	
                upDatedstr += ';'+ s.substring(0,1).toUpperCase()+''+s.substring(1, s.length()).toLowerCase();
        }
        System.debug('****'+upDatedstr);
	}
    return upDatedstr;
}


If this answers your question mark Best Answer it as solution and then hit Like!