+ Start a Discussion
Swapnil PatneSwapnil Patne 

Apex Classes error- System.ListException: List index out of bounds: 0

Hi All,

 

I am trying to deploy changes to Apex Classes in production org via sandbox using change sets.

But upon hitting validate button or test run, I am getting the following error-

System.ListException: List index out of bounds: 0, Stack Trace-Class.DeleteRWClient.DelRecs: line 79, column 1

 

I haven't got  clue about Apex Classes, the below codes were developed by someone else which unfortunately has errors and I am trying to fix it with your assistance. Can anyone guide me with this please, it would be a massive help.. 

 

public class DeleteRWClient
{
private string mstrSource;
public string mstrClientID;
private string mstrURL ;
public string mstrNoDel = '';
public string mstrClientName;
List<Opportunity> objListOppor = null;
List<RW_Client_Contact__c> objCM = null;

public DeleteRWClient(ApexPages.StandardController controller)
{
mstrClientID = ApexPages.currentPage().getParameters().get('ClientID');
List<RWClientMaster__c> objRWM = [SELECT NAME FROM RWClientMaster__c WHERE ID = : mstrClientID];
if(objRWM.size() !=0)
{
mstrClientName = objRWM[0].Name;
}
if(ApexPages.currentPage().getParameters().get('source') == null)
{
SourcePage='/apex/RWClientMaster?id=' + mstrClientID + '&sfdc.override=1&emessage=You cannot delete the Client, as it has associated Accounts and Contacts to it';
}
else
{
if(ApexPages.currentPage().getParameters().get('ContactID') != null)
{
objListOppor = [SELECT o.Contact__c From Opportunity o
WHERE o.Contact__c =: ApexPages.currentPage().getParameters().get('ContactID') ];
if(objListOppor .size() !=0)
{
mstrNoDel += 'You cannot delete the Contact, as it has associated Appointments set with this Contact Already';
}
else
{

mstrNoDel += 'Contact is no more associated with RWClient' + ' ' + mstrClientName;
}
}
else
{
mstrNoDel += 'You cannot delete the Client, as it has associated Accounts and Contacts to it';
}

SourcePage= ApexPages.currentPage().getParameters().get('source') + '&emessage=' + mstrNoDel;
}


}

public String SourcePage
{
get{return mstrSource;}
set{mstrSource = value;}
}


public DeleteRWClient()
{

}
public PageReference DeleteRec()
{
//return null;

objCM = [Select ID, r.ContactID__c From RW_Client_Contact__c r
WHERE r.ContactID__c =: ApexPages.currentPage().getParameters().get('ContactID')
AND r.RW_Client_Master__r.ID =:mstrClientID];
delete objCM ;
return new PageReference(SourcePage);
}
static testMethod void DelRecs()
{
List<RWClientMaster__c> objLstCl = [SELECT ID FROM RWClientMaster__c ORDER BY CREATEDDATE DESC LIMIT 1];
ApexPages.currentPage().getParameters().put('ClientID',objLstCl[0].ID );
ApexPages.currentPage().getParameters().put('source','');
List<RW_Client_Contact__c> objClCont = [Select r.ContactID__c, r.RW_Client_Master__c from RW_Client_Contact__c r
WHERE RW_Client_Master__c =:objLstCl[0].ID LIMIT 1];
ApexPages.currentPage().getParameters().put('ContactID',objClCont[0].ContactID__c);
ApexPages.StandardController controller;
DeleteRWClient objDw = new DeleteRWClient(controller);
DeleteRWClient objDw1 = new DeleteRWClient();

objDW.SourcePage = ApexPages.currentPage().getParameters().get('source');
objDW.mstrClientID = ApexPages.currentPage().getParameters().get('ClientID');

string strPage = objDW.DeleteRec().getUrl();

ApexPages.currentPage().getParameters().put('ClientID',objLstCl[0].ID );
ApexPages.currentPage().getParameters().put('source','rc');
ApexPages.currentPage().getParameters().put('ContactID',objClCont[0].ContactID__c);

objDw = new DeleteRWClient(controller);
objDw1 = new DeleteRWClient();

objDW.SourcePage = ApexPages.currentPage().getParameters().get('source');
objDW.mstrClientID = ApexPages.currentPage().getParameters().get('ClientID');

strPage = objDW.DeleteRec().getUrl();


}

}

 

Regards,

Swapnil

 

Aar3538Aar3538

Hello!
 
When you deploy your change sets to production, your unit tests ran and failed, which is halting the deploy.
 
This looks to be because the tests assume there will be data returned from the queries, but production doesn't have that data.  Most unit tests will generate test data within the test method scope to work with but the test attached did not do that.

 

Your test method from what I can see is missing two records:

1) RWClientMaster__c record
2) RW_Client_Contact__c record where RW_Client_Master__c field is populated to the RWClientMaster__c record above and a filled out ContactID__c field.

 

You could create that data manually in the system and that might solve your problem but that is ill advised since that data could be removed and you never know what test data the unit tests would be using.  You could also just remove this entire test but that will lower code coverage and if its below 75%, your deploy would be halted.

 

Listed below is my take on what would potentially unblock your deployment.  Please note that I am unable to know the exact specifics of your custom objects and I am working with only what will unblock this current error.


Please add the following green code to the unit test and also make an edit to the code in orange:

 

static testMethod void DelRecs()
{
//Insert some test records to properly validate the DelRec test
RWClientMaster__c rwcClientExample = new RWClientMaster__c();
insert rwcClientExample;
RW_Client_Contact__c rwcClientContactExample = new RW_Client_Contact__c(RW_Client_Master__c = rwcClientExample.Id);
insert rwcClientContactExample;

List<RWClientMaster__c> objLstCl = [SELECT ID FROM RWClientMaster__c WHERE Id =: rwcClientExample.Id];
//Keep rest of the unit test code...

Swapnil PatneSwapnil Patne

Hi,

 

Thank you for your reply.

 

I tried out what you said but it throws the following error now.?

 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [AnnualRevenue__c]: [AnnualRevenue__c]  Class.DeleteRWClient.DelRecs: line 76, column 1

 

Your help is appreciated.

 

Regards,

Swapnil

 

Rajesh SriramuluRajesh Sriramulu

Hi

 

As the given error that AnnualRevenue__c field  is mandatory so assign any value to it and insert

for eg:

when we going to insert any account means in that account name mandatory like this here also AnnualRevenue__c is mandatory.

 

 

Regards

SRS