You need to sign in to do that
Don't have an account?
ANIL PAKKI
Hi, I know many of you might seen this error earlier: Failure Message: "System.ListException: List index out of bounds: 0", Failure Stack Trace, Stack Trace:Method7: line 133, column 1
public static testmethod void testuser() {
Account accountRecord1 = new Account(Name = 'Dell Products test',Core_Account_Type__c = 'Distributor',Core_Sales_Segment__c = 'Compute',Industry = 'Banking',BillingCity = 'Delhi',BillingCountry = 'USA',billingState = 'AK',billingStreet = 'Bratislava 1',Core_IPS_Active__c = true);
insert accountRecord1;
contact con = new contact(FirstName = 'Customer',LastName='john',Email='customer@john.com',AccountId=accountRecord1.id);
insert con;
ApexPages.StandardController sc = new ApexPages.standardController(con);
classcontroller obj = new classcontroller(sc);
List<User> userlst=[SELECT id,username,name,userroleid,contactid,profileid,Accountid,email,isactive from user where contactid=:con.id and profile.Name='Customer' ];
Id CustRoleId=userlst[0].id;
Test.starttest();
obj.method(CustRoleId);
Test.stoptest();
}
Account accountRecord1 = new Account(Name = 'Dell Products test',Core_Account_Type__c = 'Distributor',Core_Sales_Segment__c = 'Compute',Industry = 'Banking',BillingCity = 'Delhi',BillingCountry = 'USA',billingState = 'AK',billingStreet = 'Bratislava 1',Core_IPS_Active__c = true);
insert accountRecord1;
contact con = new contact(FirstName = 'Customer',LastName='john',Email='customer@john.com',AccountId=accountRecord1.id);
insert con;
ApexPages.StandardController sc = new ApexPages.standardController(con);
classcontroller obj = new classcontroller(sc);
List<User> userlst=[SELECT id,username,name,userroleid,contactid,profileid,Accountid,email,isactive from user where contactid=:con.id and profile.Name='Customer' ];
Id CustRoleId=userlst[0].id;
Test.starttest();
obj.method(CustRoleId);
Test.stoptest();
}
Id CustRoleId=userlst[0].id;
Here the problem is with above line.
userlst[] has no elements so trying to access the 0 index element will cause the list index out of bounds excpetion.
To protect against this you could change your code to first check to see if the returned List is not empty:
if (!userlst.isEmpty())
{
Id CustRoleId=userlst[0].id;
}