You need to sign in to do that
Don't have an account?

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
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
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.
:)
Thanks a lot Yash....
This soved my issue.