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
ckellieckellie 

Displaying comma-separated strings in a table format

The below code combines two lists as one list and sorts the list.
public with sharing class ProductCostSalesSheet {


    public ProductCostSalesSheet() {
        o = [select id from Opportunity where id =: System.currentpageReference().getParameters().get('oppid')];
        system.debug('*********************000000000:'+ o); 
        oppi = o.id;  
        system.debug('*********************000000001:'+ oppi); 
        
    }

    
    id oppi;
    Public final Opportunity o;
    List<String> prodOption = new List<String>();
    Map<String, String> joinMap = new Map<String, String>();

    public ProductCostSalesSheet(ApexPages.StandardController controller) {
        
        }

      public  List<string> getOppProductOptions(){
      
      prodOption = new List<String>();
      

       for(OpportunityLineItem oli: [select id, Product2.name, unitprice, discount, totalprice, quantity from OpportunityLineItem where OpportunityId =: oppi]){
           joinMap.put(oli.id, oli.id+','+oli.product2.name+','+oli.quantity+','+oli.discount+','+oli.unitprice);
       }
       
       for(Proposal_Option__c p : [select id, Opportunity_Product__c, Description__c,sequence__c,sales_price__c,discount__c,unit_price__c,quantity__c from Proposal_Option__c where Opportunity__c =: oppi]){
           joinMap.put(p.Opportunity_Product__c+p.id, p.Opportunity_Product__c+p.sequence__c+','+p.Description__c+','+p.quantity__c+','+p.discount__c+','+p.unit_price__c);
           system.debug('*********************000000003:'+ joinMap); 
       }
       prodOption = joinMap.values();
      
       prodOption.sort();
    
       return prodOption;
    }

}
The code does the following:
1. Joins mulitple fields into one comma-separated string.
2. Combines two lists into a map, then a list.
3. Sorts the list.
4. returns the list.

How do I now convert the comma-separated string to a list?
 
ManojjenaManojjena
HI Ckellie,

You can convert comma separated string into a list by using split method .

Check blow code in console it will give you list .
 
String str='A,B,C,D,E,F,G,H';
List<String> strList=str.split(',');

System.debug('***********************'+strList);
System.debug('***********************'+strList.size());
Let me know if it helps .

Thanks 
Manoj
 
ckellieckellie
Manoj,

Thank you for the string suggestion. In my code I have a the following values in my list

string record 1 = value 1a, value1b, value 1c
string record 2 = value 2a, value2b, value 2c

Both strings are right now in a list. How do I separate each string, use the string.split command on each string, and keep the lists separate?

Thank you,
ckellie
ManojjenaManojjena
Hi Ckellie,

You can try the code snnipet below and try to implement .
 
List<String> firsttLst = new List<String>();
List<String> secondLst = new List<String>();
string str = 'value 1a, value1b, value 1c,value 2a, value2b, value 2c';
List<String> commonLsit=str.split(',');
for(Integer i=0;i<3;i++){
 firsttLst.add(commonLsit.get(i));   
}
for(Integer i=0;i<3;i++){
   commonLsit.remove(i);   
}
secondLst.addAll(commonLsit);
System.debug('*************'+firsttLst);
System.debug('^^^^^^^^^^^^'+secondLst);

Let me know if it helps .



 
ckellieckellie
I tried to add the following code: List<string> commonList = prodOption.split(',');

I recieved the following error: productcostsalessheet ProductCostSalesSheet

[Error] Error: ProductCostSalesSheet Compile Error: Method does not exist or incorrect signature: [List<String>].split(String) at line 40 column 34


Although I understand the list cannot the be separated, I don't know how to change the list to a string and keep all records that are in the list. The code you provided works for one or two strings, but not for a list of strings.
ManojjenaManojjena
Hi Ckellie,

Your eeor is saying that you are spliting to a list not a string . Split method is there in string class not in list class .
if it is already a list what you wnat to split to create a list ?
 
ckellieckellie

Hi Manoj,

I havea list of strings, but I need to divide each string into a substring to represent the fields in each record.  The end state is to display a list of records in a table 7-columns wide. Each string in the list is comma-separated for each field/column.

Some background to this need:
I am pulling data from two objects, opportunitylineitem and a custom object. The custom object is a child object of the opportunitylineitem, but salesforce does not allow the opportunitylineitem object to be a master object in a master-chile relationship. Through apex code I have inserted the opportunitylineitem id in a text field on the proposal option object.  In my initial post, As the field labels are not exactly the same, I have combined all 7 fields into one string to represent a record or line item. I have combined the string values into one list.

Now that I have combined the strings into one list, I need to find some way to create sublists for each string in the list.

Does this explanation give you a better handle as to what I am trying to do?

Thank you,
ckellie

shardul singh 23shardul singh 23
anyone help me with thw trigger and handler class to display fields with comma seperated