I have an apex code like that :
List<Campaign> campaigns = DataFactory.createCampaign();
Contact contact = new Contact(/*some fields here*/);
if(!campaigns.isEmpty()){
DataFactory.createCampaignMember(campaigns[0], contact);
}
If I put a system.debug(campaings)
I retrieve 3 campaings, so my list campaigns
is not empty.
But when I run the code (it is in an apex test class) I got this error :
System.ListException: List index out of bounds: 0
at the line DataFactory.createCampaignMember(campaigns[0], contact);
How can I resolve this error ?
EDIT :
Here is the method of the DataFactory :
public static CampaignMember createCampaignMember(Campaign campaign, Contact contact){
List<Contact> idContact = [SELECT Id FROM Contact WHERE FirstName=:contact.FirstName LIMIT 1];
List<Campaign> idCampaign = [SELECT Id FROM Campaign WHERE Name=:campaign.Name LIMIT 1];
CampaignMember campaignMember = new CampaignMember(ContactId=idContact.get(0).Id, CampaignId=idCampaign.get(0).Id);
return campaignMember;
}
The error is indicated on the line CampaignMember(ContactId=idContact.get(0).Id, CampaignId=idCampaign.get(0).Id);
DataFactory.createCampaignMember
method