0

Below is the method when triggered it is inserting multiselected records delimited by semicolon. When inserting, initially a semicolon is getting inserted like below pic. How can I remove the first semicolon?

public PageReference save()
   {
      Id rfpId;
      rfpId = ApexPages.currentPage().getParameters().get('rfpId');
      //upsert accounts;
       List<ChildRFP__c> rfpchild = new List<ChildRFP__c>();


       for(integer i=0;i< attendeeList1.size(); i++){
            if(attendeeList1[i].discountSchedule.size()!=0){
           for(integer j=0; j<attendeeList1[i].discountSchedule.size(); j++ ){
                s = s + '; ' + attendeeList1[i].discountSchedule[j];
           }
           rfpchild.add(new ChildRFP__c(Name = 'TestNow2', Custom_Package__c = s,Market__c =attendeeList1[i].discountSchedule1));
           s=' ';

           }
       }
            if (rfpchild.size() > 0 ) {
                insert rfpchild;
            }



       return new PageReference('/' + ApexPages.currentPage().getParameters().get('rfpId'));    
   }

enter image description here

0

3 Answers 3

7

You can modify this line as follows

    List<String> setStr = new List<String>();
    for(integer j=0; j<attendeeList1[i].discountSchedule.size(); j++ ){
            setStr.add(attendeeList1[i].discountSchedule[j]);
       }

    s = String.join(setStr,';');
2
  • Thnnx Himanshu....This worked.... Commented Nov 19, 2015 at 18:01
  • fantastic!!!!!!
    – Himanshu
    Commented Nov 19, 2015 at 18:04
1

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_removeStart

The removeStart method can do this. After your loop:

s = s.removeStart(';');
5
  • AFter which loop should i use the line.. Commented Nov 19, 2015 at 17:55
  • Right before rfpchild.add(new ChildRFP__c(Name = 'TestNow2', Custom_Package__c = s,Market__c =attendeeList1[i].discountSchedule1)); s=' ';
    – dphil
    Commented Nov 19, 2015 at 17:56
  • No..The semicolon is still there... Commented Nov 19, 2015 at 17:58
  • It shouldn't be unless you have something else prepended to it. I just ran the code in my org and it worked exactly as I expected.
    – dphil
    Commented Nov 19, 2015 at 18:01
  • Ok.. Let me chk once again... Commented Nov 19, 2015 at 18:02
0

You can use String.join as below

List<Integer> li = new    List<Integer> {10, 20, 30};
String s = String.join(li, '/');
System.assertEquals('10/20/30', s);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .