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
BritishBoyinDCBritishBoyinDC 

String Split Fail..

Not sure what I am missing here - this seems simple enough...

 

I have a MVP Picklist field called Languages__c. I want to split the field into a String List using ';' as the split. But when Languages has more than ten entries, ther debug shows me that the List contains the first ten, and then the characters  ', ...)'

 

What I am missing here? What can I do to make sure all the values end up in the array?

 

 

public class cSearch{
        Public String [] languagesvalue{get;set;}    
         
        
        //This is the contructor method to build the search filters
        public cSearch(Contact con,Contact formatch){ //set search fields = values on contact being searched           
                   
            languagesvalue = new String [] {} ;
            if (con.Languages__c != null) {
            String [] sissues = con.Languages__c.split(';');
            for (String s: sissues) {
            languagesvalue.add('\'' + s + '\'');
            }           
            }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

Looks like you are doing a query string type of DML.  You might need to build the full query out with the value, referencing the array in that fashion may still be limiting the results returned.  Maybe instead of building out your list as an array, build it out as the full text string, and then append it to the query string.

 

 

//create the array for validation of the number of values
String includestring='';
  for (String s: sissues) {
            languagesvalue.add('\'' + s + '\'');
            includestring+=','+s;
            }           
includestring=includestring.substring(1);//used to remove the leading comma
String q = [Select Id from Contact where isDeleted = FALSE ';
if(languagevalue.size()<0){
     q += ' AND Languages__c INCLUDES (' + includestring+')';

 

 

All Answers

cl0s3rcl0s3r

Is it a multi select or just a picklist?  If multi select the seperator is ";" not a ",".

SteveBowerSteveBower

Seems obvious... what does system.debug(con.languages__c) yield?

BritishBoyinDCBritishBoyinDC

 

if you debug the contact field, it looks like you'd expect English, French,xxx etc...and includes all the values in the selected MVP.

 

But the converted string [] looks like this (English, French, xxx, ...)

 

So when it reaches the eleventh element, it decides not to add anymore, and instead, just adds ,...) to show there are more records to see but they weren't added....

 

Is there a parameter I am missing? I tried to add -1 etc but nothing seems to change the behavior?

 

 

JimRaeJimRae

I believe that is just the Debug function representation of the list.  You could do a different type of test where you display the number of elements in the list using the size method to see if they are all present or not.

BritishBoyinDCBritishBoyinDC

Ah, you're right - it does seem to have the right number of elements...so then the error is happening somewhere else...

 

The actual error is appearing here when I include that fields in a string for a SOQL query:

 

String q = [Select Id from Contact where isDeleted = FALSE ';
q += ' AND Languages__c INCLUDES ' + cSearches.languagesvalue;

 When cSearches.Languages has 10 elements, it works fine.

 

If I add an eleventh, I get this error: 

System.QueryException: unexpected token: '.'

And the debug on that string q shows the SOQL as being AND Languages__c INCLUDES ('English', 'French' 'xxx', ...) AND etc

 

That's why I thought it was the original split that was failing, but your suggestion seems to disprove that, so is the string to SOQL Query doing something odd?

 

 

JimRaeJimRae

Looks like you are doing a query string type of DML.  You might need to build the full query out with the value, referencing the array in that fashion may still be limiting the results returned.  Maybe instead of building out your list as an array, build it out as the full text string, and then append it to the query string.

 

 

//create the array for validation of the number of values
String includestring='';
  for (String s: sissues) {
            languagesvalue.add('\'' + s + '\'');
            includestring+=','+s;
            }           
includestring=includestring.substring(1);//used to remove the leading comma
String q = [Select Id from Contact where isDeleted = FALSE ';
if(languagevalue.size()<0){
     q += ' AND Languages__c INCLUDES (' + includestring+')';

 

 

This was selected as the best answer
BritishBoyinDCBritishBoyinDC

Thanks Jim - that worked.