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
Tatiana Cooke 9Tatiana Cooke 9 

Error: " System.ListException: List index out of bounds: 0

Team, 

I have the following trigger on my person accounts and am trying to run the below test class with the following error. 
Error: " System.ListException: List index out of bounds: 0 "  Saying its in line 3570 of my test class (which doesn't exsit)

Appreciate any guidance.

Trigger:
 
trigger LogPersonAccountChange on Account (before delete, after insert, after undelete)
{
  List<pi__ObjectChangeLog__c> logs = new List<pi__ObjectChangeLog__c>(); 

  if (Trigger.new != null) {
    for (Account account : Trigger.new) {

      if (Account.PersonEmail != null && Account.PersonEmail != '') {
        pi__ObjectChangeLog__c log = new pi__ObjectChangeLog__c();
        log.pi__ObjectFid__c = Account.PersonContactId;
        log.pi__ObjectType__c = 1;
        log.pi__ObjectEmail__c = Account.PersonEmail;

        if  (System.Trigger.isInsert) {
          log.pi__ObjectState__c = 1;
        } else if  (System.Trigger.isDelete) {
          log.pi__ObjectState__c = 2;
        } else if  (System.Trigger.isUnDelete) {
          log.pi__ObjectState__c = 3;

        }
        logs.add(log);
      }
    }
  } else if (Trigger.old != null) {
    for (Account account : Trigger.old) {
      if (Account.PersonEmail != null && Account.PersonEmail != '') {
        pi__ObjectChangeLog__c log = new pi__ObjectChangeLog__c();

        log.pi__ObjectFid__c = Account.PersonContactId

        ;
        log.pi__ObjectType__c = 1;
        log.pi__ObjectEmail__c = Account.PersonEmail;
        if  (System.Trigger.isInsert) {
          log.pi__ObjectState__c = 1;
        } else if  (System.Trigger.isDelete) {
          log.pi__ObjectState__c = 2;
        } else if  (System.Trigger.isUnDelete) {
          log.pi__ObjectState__c = 3;
        }
        logs.add(log);
      }
    }
  }

  if (logs.size() > 0) {

    insert logs;
  }
}

Test Class: 
 
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class TestPersonAccountChangeLog {

    static testMethod void LogPersonAccountChangeTest() {
    	
String RecTypeId= [select Id from RecordType where (Name='Ward Residential Buyers') and (SobjectType='Account')].Id;
    	   
    WE_Process__c obj=new WE_Process__c();
    obj.Name='test';
    insert obj;
     
    Account account = new Account();    
    account.FirstName='Test Acc';
    account.LastName='Last Name';
    Account.PersonEmail = 'abc@123.com';
    account.RecordTypeID=RecTypeId;
    insert Account;

    pi__ObjectChangeLog__c  p1 = new pi__ObjectChangeLog__c (); 

    p1.name = 'fgasfsa'; 
    p1.pi__ObjectState__c = 1; 
    p1.pi__ObjectEmail__c = Account.PersonEmail; 
    p1.pi__ObjectFid__c = 'Sample Fid'; 
    p1.pi__ObjectType__c = 1; 
    Insert p1; 

    System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 1]);
    delete Account;
    System.assertEquals(0, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 2]);
    undelete Account;
    System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c =:Account.PersonEmail AND pi__ObjectState__c = 1]);
    }
}



 
Deepak GulianDeepak Gulian
At line 49 Replace by this
System.assertEquals(2, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 1]);
Tatiana Cooke 9Tatiana Cooke 9
When I update to the above I get a null arguement error??? = (
Deepak GulianDeepak Gulian
/** * This class contains unit tests for validating the behavior of Apex classes * and triggers. * * Unit tests are class methods that verify whether a particular piece * of code is working properly. Unit test methods take no arguments, * commit no data to the database, and are flagged with the testMethod * keyword in the method definition. * * All test methods in an organization are executed whenever Apex code is deployed * to a production organization to confirm correctness, ensure code * coverage, and prevent regressions. All Apex classes are * required to have at least 75% code coverage in order to be deployed * to a production organization. In addition, all triggers must have some code coverage. * * The @isTest class annotation indicates this class only contains test * methods. Classes defined with the @isTest annotation do not count against * the organization size limit for all Apex scripts. * * See the Apex Language Reference for more information about Testing and Code Coverage. */

@isTest private class TestPersonAccountChangeLog {

static testMethod void LogPersonAccountChangeTest() {

String RecTypeId= [select Id from RecordType where (Name='Ward Residential Buyers') and (SobjectType='Account')].Id;
WE_Process__c obj=new WE_Process__c();
obj.Name='test'; insert obj;

Account account = new Account();
account.FirstName='Test Acc';
account.LastName='Last Name';
Account.PersonEmail = 'abc@123.com';
account.RecordTypeID=RecTypeId;

insert Account;
System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 1]);

delete Account;
System.assertEquals(0, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 2]);

undelete Account;
System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 3]);
}

}
Let me know if it still not works!!
Deepak GulianDeepak Gulian

If it still not work then, add @isTest(SeeAllData=true) Add this at the top of your test Class.

Tatiana Cooke 9Tatiana Cooke 9
Deepak,

Almost there, tried it with your above @istest(seealldate=true).

Still have one final error I need help with.

Error:
System.DmlException: Insert failed. First exception on row 0 with id 001i000001nxtBTAAY; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] TestPersonAccountChangeLog.cls on insert account;
 
@isTest(SeeAllData=true)
private class TestPersonAccountChangeLog {

    static testMethod void LogPersonAccountChangeTest() {
    	
String RecTypeId= [select Id from RecordType where (Name='Ward Residential Buyers') and (SobjectType='Account')].Id;
    	   
    WE_Process__c obj=new WE_Process__c();
    obj.Name='test';
    insert obj;
     
    Account account = new Account();    
    account.FirstName='Test Acc';
    account.LastName='Last Name';
    Account.PersonEmail = 'abc@123.com';
    account.RecordTypeID=RecTypeId;
    insert Account;

    pi__ObjectChangeLog__c  p1 = new pi__ObjectChangeLog__c (); 

    p1.name = 'fgasfsa'; 
    p1.pi__ObjectState__c = 1; 
    p1.pi__ObjectEmail__c = Account.PersonEmail; 
    p1.pi__ObjectFid__c = 'Sample Fid'; 
    p1.pi__ObjectType__c = 1; 
    Insert p1; 

	insert Account;
	System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 1]);

	delete Account;
	System.assertEquals(0, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 2]);

	undelete Account;
	System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 3]);
}
}


 
Deepak GulianDeepak Gulian

At Line 28:
At Line 17:

You are inserting Account twice!

Deepak GulianDeepak Gulian
Just remove one Insert Account at line 28, and you are good to go!