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
nununinununi 

Creating a list from a text field

Hello developers,

I have a text field test__c on an object TestObject__c. This filed has values seperated by commas (a,b,c,d). In my apex class, I would like to pass these values into a list TestList. Can someone please provide me with the correct syntax?

Thanks!
Best Answer chosen by nununi
Boris BachovskiBoris Bachovski
@Bhanu how is that different from my answer?

Also
if(testObj.test__c != null && !''.equals(testObj.test__c))
can be shortened to:
 
if(!String.isBlank(testObj.test__c))


 

All Answers

Boris BachovskiBoris Bachovski
List <String> TestList = TestObject__c.test__c.split(',');
Bhanu MaheshBhanu Mahesh
Hi nununi,
You can use split method
TestObject__c testObj{get;set;}
List<String> TestList = new List<String>();
if(testObj.test__c != null && !''.equals(testObj.test__c)){
     TestList = testObj.test__c.split(',');
}


Regards,
Bhanu Mahesh 
Boris BachovskiBoris Bachovski
@Bhanu how is that different from my answer?

Also
if(testObj.test__c != null && !''.equals(testObj.test__c))
can be shortened to:
 
if(!String.isBlank(testObj.test__c))


 
This was selected as the best answer
Sai Ram ASai Ram A
Hi nununi
 
string xyz = 'a,b,c,d,e,f,g,h,i,j,k'; //Your text field
List<String> parts = xyz.split(','); //use String method Split
for(string s: parts){
    system.debug('String: '+s);
}

Hope this helps!!

Thank you
BLearn
nununinununi
Thanks everyone for your response. However, i am getting the following error:
Error: Compile Error: Method does not exist or incorrect signature: [Schema.SObjectField].split(String)