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
HAZLITT VASUDEVANHAZLITT 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;
        }
    }
SwethaSwetha (Salesforce Developers) 
HI Vasu,
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
List lstAccount = [Select Id, Name from Account Limit 10];
// Before processing the list check if its empty or not
// It will go inside the loop only if the List is having values in it.
if(lstAccount.size() > 0) {
 // Do something with lstAccount[0].Name
 }
// If you try to access lstAccount[0] without empty check then it will obviously throw that error!!

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​​​​​​​
CharuDuttCharuDutt
Hii Hazlitt
Try Below Code Changes Are In Bold =lst[i];

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[i];
            }
            if(s.contains('Asian Paints'))
            { 
                i = i+3;
                s1=lst[i];
                work.Name = s1;
            }
            if(s.contains('12345678'))
            { 
                i = i+5;
                s2=lst[i];
                work.WO_Number__c =double.valueOf(s2) ;
            }
            if(s.contains('1547586597'))
            { 
                i = i+7;
                s3=lst[i];
                work.Account_Id__c =double.valueOf(s3);
                                
            }
        }
               
         insert work;
        }
    }
Please Mark It As Best Answer If It Helps
Thank You!

 
Goku ZeusGoku Zeus
We take pleasure in our work and strive to surpass our clients' expectations at all times https://www.lgcdecorators.co.uk/