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
AmitSahuAmitSahu 

Strange Apex Code .......!! Please help

Hi All,

 

Thanks in advance for any help on this topic.

 

Here is my code : 

   

String emailsubject = 'test1,test2,test3';
String emailbody= '100\n200\n300';
String[] emailSubjectIteams = emailsubject.split(',');
String[]  emailBodyMulti = emailbody.split('\n');
 List<Expense__c> expelement= new List<Expense__c>();
   Expense__c expItems = new Expense__c();
        for(integer i=0; i < emailSubjectIteams.size();i++)
                           {
    expItems.ExpName__c = emailSubjectIteams[i];
  expItems.Price__c = integer.valueOf(emailBodyMulti[i].substring(0));     
             expelement.add(expItems);           
      System.debug('expItems object'+expItems);
   System.debug('Expelement list values : '+expelement );     
              }

   

I am running the above code in System log to check my issue.  The Last system.debug line to print expelement shows a confusing result to me.

 

Please let me know if any one has any clue about this issue.

 

Regards,

 

NewSFDC

Best Answer chosen by Admin (Salesforce Developers) 
yashagarwalyashagarwal

Try changing the code to this:

 

String emailsubject = 'test1,test2,test3';
String emailbody= '100\n200\n300';
String[] emailSubjectIteams = emailsubject.split(',');
String[]  emailBodyMulti = emailbody.split('\n');
List<Expense__c> expelement= new List<Expense__c>();

for(integer i=0; i < emailSubjectIteams.size();i++)
{
       Expense__c expItems = new Expense__c();

       expItems.ExpName__c = emailSubjectIteams[i];
       expItems.Price__c = integer.valueOf(emailBodyMulti[i].substring(0));    
       expelement.add(expItems);          
      System.debug('expItems object'+expItems);
       System.debug('Expelement list values : '+expelement );    
 }


  System.debug('Final List : '+expelement );     

 

The initialization needs to be inside the for loop.

 

:)

All Answers

yashagarwalyashagarwal

Try changing the code to this:

 

String emailsubject = 'test1,test2,test3';
String emailbody= '100\n200\n300';
String[] emailSubjectIteams = emailsubject.split(',');
String[]  emailBodyMulti = emailbody.split('\n');
List<Expense__c> expelement= new List<Expense__c>();

for(integer i=0; i < emailSubjectIteams.size();i++)
{
       Expense__c expItems = new Expense__c();

       expItems.ExpName__c = emailSubjectIteams[i];
       expItems.Price__c = integer.valueOf(emailBodyMulti[i].substring(0));    
       expelement.add(expItems);          
      System.debug('expItems object'+expItems);
       System.debug('Expelement list values : '+expelement );    
 }


  System.debug('Final List : '+expelement );     

 

The initialization needs to be inside the for loop.

 

:)

This was selected as the best answer
AmitSahuAmitSahu

Thanks a lot Yash....

 

This soved my issue.