0

Please find my below code that is getting list index out of bound exception.

Apex Class:

global class Incidentimp_2 implements Database.Batchable < sObject > {

    public string query = 'SELECT DM_Lookup_id__c FROM Incident__c where createddate<>today';

    global database.querylocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List < Incident__c > scope) {
        //Map<Inventory_Wishlist__c, Product2> temp = new Map<Inventory_Wishlist__c, Product2>();
        Set < ID > IdSet = new Set < ID > {};
        // Map<string,string> xyz=new Map<string,string>();
        // EmailTemplate emailTemplate = [select Id, Body  from EmailTemplate where DeveloperName = 'POC_DM'];
        //List<Incident__c> soorders = (List<Incident__c>)scope;
        //Map<ID, String> salesOrderMap = new Map<ID,String>();
        for (Incident__c so: scope) {
            IdSet.add(so.DM_Lookup_id__c);
        }

        List < User > usr = [select Email, id, Name from User where id in : IdSet];
        for (User u: usr) {
            list < string > txtbody = new list < string > ();

            txtbody[0] = 'Dear ' + u.Name + ',';
            txtbody[1] = 'New non-compliant cases have been assigned to you in Aspire. \n';
            txtbody[2] = 'Please review and resolve all issues within two weeks. \n';
            txtbody[3] = 'or Late Code Subcategory is changed \n';

            String plaintxtbody = String.join(txtbody, '/n');

            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(new String[] {
                '[email protected]'
            });
            email.setToAddresses(new String[] {
                '[email protected]'
            });
            email.setSaveAsActivity(false);
            email.setTargetObjectId(u.Id);
            //email.setTemplateId(emailTemplate.Id);
            email.setPlainTextBody(plaintxtbody);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
                email
            });
        }


    }
    global void finish(Database.BatchableContext BC) {

    }

}
1
  • 2
    SFDC_Geek, could you at least also state what you've tried to debug this yourself and at what line the error is occurring ? Please understand that you need to do some effort if you expect others to answer. Helping us help you by providing all relevant details to an issue is usually a good start. Nov 10, 2014 at 12:29

2 Answers 2

0

To add to Samuel's answer, I suggest that the cleaner syntax to create a list (or array - they are interchangeable in Apex) where you know the content is this:

String[] txtbody = new String[] {
        'Dear ' + u.Name + ',',
        'New non-compliant cases have been assigned to you in Aspire. \n',
        'Please review and resolve all issues within two weeks. \n',
        'or Late Code Subcategory is changed \n'
        };

as described in Arrays and Lists.

Not sure why you are putting '/n' between the values soon afterwards though.

5

I'm assuming the following lines are the ones giving your error:

list < string > txtbody = new list < string > ();

            txtbody[0] = 'Dear ' + u.Name + ',';
            txtbody[1] = 'New non-compliant cases have been assigned to you in Aspire. \n';
            txtbody[2] = 'Please review and resolve all issues within two weeks. \n';

While lists can be used with this syntax (arrays) , you can only do so for existing items in your list. If you want to populate your indexes, you need to declare the list as an array:

List<String> txtbody  = new String[3];

But, you could also use the object method add() to add your strings to the list:

  txtbody.add( 'Dear ' + u.Name + ',');
  txtbody.add(  'New non-compliant cases have been assigned to you in Aspire. \n');
  txtbody.add(  'Please review and resolve all issues within two weeks. \n');

Using the latter syntax is more readable to most, and will avoid any hard coded numeric references which make your code less maintainable.

All of this is well documented in the List Methods Documentation

You must log in to answer this question.

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