You need to sign in to do that
Don't have an account?
HAZLITT VASUDEVAN
why am I getting this error System.ListException: List index out of bounds ?
public class checktest
{
public static void force()
{
Work_Order__c work = new Work_Order__c();
String s, s1, s2, s3='';
String a = 'Hello. \n Welcome to salesforce, \n Name \n Asian Paints \n WO Number \n 12345678 \n Account Id \n 1547586597';
List<String> lst = a.split('\n');
for(integer i=0;i<lst.size();i++)
{
s=lst.get(i);
if(s.contains('Hello. \n Welcome to salesforce, \n Name \n Asian Paints \n WO Number \n 12345678 \n Account Id \n 1547586597'))
{
work.Email_Body__c=lst.get(i);
}
if(s.contains('Asian Paints'))
{
i = i+3;
s1=lst.get(i);
work.Name = s1;
}
if(s.contains('12345678'))
{
i = i+5;
s2=lst.get(i);
work.WO_Number__c =double.valueOf(s2) ;
}
if(s.contains('1547586597'))
{
i = i+7;
s3= lst.get(i);
work.Account_Id__c =double.valueOf(s3);
}
}
insert work;
}
}
{
public static void force()
{
Work_Order__c work = new Work_Order__c();
String s, s1, s2, s3='';
String a = 'Hello. \n Welcome to salesforce, \n Name \n Asian Paints \n WO Number \n 12345678 \n Account Id \n 1547586597';
List<String> lst = a.split('\n');
for(integer i=0;i<lst.size();i++)
{
s=lst.get(i);
if(s.contains('Hello. \n Welcome to salesforce, \n Name \n Asian Paints \n WO Number \n 12345678 \n Account Id \n 1547586597'))
{
work.Email_Body__c=lst.get(i);
}
if(s.contains('Asian Paints'))
{
i = i+3;
s1=lst.get(i);
work.Name = s1;
}
if(s.contains('12345678'))
{
i = i+5;
s2=lst.get(i);
work.WO_Number__c =double.valueOf(s2) ;
}
if(s.contains('1547586597'))
{
i = i+7;
s3= lst.get(i);
work.Account_Id__c =double.valueOf(s3);
}
}
insert work;
}
}
On a general note, if we attempt to access an element at row 0, the code will throw an error if no data exist at row 0.
To fix this, check whether there are records in the list you have defined in your code before accessing it.
Sample code
Reference: https://help.salesforce.com/articleView?id=000329067&type=1&mode=1
Similar scenario: https://salesforce.stackexchange.com/questions/9611/how-to-solve-the-list-index-out-of-bounds-0-issue
If this information helps, please mark the answer as best. Thank you
Try Below Code Changes Are In Bold =lst[i]; Please Mark It As Best Answer If It Helps
Thank You!