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
lizmijohnlizmijohn 

Apex test class code help

Hi,

 

I have 'Project' as parent record and 'Project Analysis' as child records.

I have written a test class as follows:

 

@isTest
private class projecttrigger {
static testMethod void ProjectAnalysistrigger() {

Project1__c prj = new Project1__c();
prj.Name='P-006';
prj.Project_Name__c='Test-Rigs';
prj.Country__c='a0GL0000001G4mu';
prj.End_Market_Segment__c='Rigs';
prj.EPC_Awarding_Date__c=Date.newInstance(2014,6,1);
prj.EndDate__c=Date.newInstance(2017,6,1);
prj.Analysis_dell__c='a0CL0000000cIeN';
prj.ProjectValue__c=2000000;
prj.Eq_Q1_month_percent_Eq1__c = 0.8;
prj.Eq_Q1_month_percent_FM1__c = 0.6;
prj.Eq_Q2_3_4_month_percent_Eq1__c = 0.2;
prj.Eq_Q2_3_4_month_percent_FM1__c = 0.4;

Integer i = 0;
date Date1= prj.EPC_Awarding_Date__c;
date myDate1=Date1;
Decimal x=0;
Decimal y=0;

prj = [SELECT id,Name,Country__c,Project_Name__c,End_Market_Segment__c, Start_Date__c, EPC_Awarding_Date__c, Analysis_dell__c, ProjectValue__c, No_of_months__c,Eq_Q1_month__c,Eq_Q1_month_FM__c,Eq_Q2_3_4_month__c,Eq_Q2_3_4_month_FM__c,Eq_Q1_month_percent_Eq1__c,Eq_Q1_month_percent_FM1__c,Eq_Q2_3_4_month_percent_Eq1__c,Eq_Q2_3_4_month_percent_FM1__c FROM Project1__c WHERE id = :prj.id];
Project_Analysis__c[] analysisToCreate = new Project_Analysis__c[]{};


for (i=0;i< prj.No_of_Months__c; i++)
{

if((myDate1>=prj.EPC_Awarding_Date__c ) && (myDate1 < prj.Q3__c))
{
if(myDate1<prj.Q1__c)
{
x=prj.Eq_Q1_month__c;
}
else
{
x=prj.Eq_Q2_3_4_month__c;
}
}
else
{
x=0;
}

if((myDate1>=prj.EPC_Awarding_Date__c) && (myDate1 < prj.Q3_FM__c))
{
if(myDate1<prj.Q1_FM__c)
{
y=prj.Eq_Q1_month_FM__c;
}
else if(myDate1<prj.Q2_FM__c)
{
y=0;
}
else if(myDate1>=prj.Q2_FM__c)
{
y=prj.Eq_Q2_3_4_month_FM__c;
}
}
else
{
y=0;
}
Project_Analysis__c pa=new Project_Analysis__c(Project1__c= prj.Id,Amount_per_month__c=x,FM__c=y,Date__c = myDate1);
MyDate1=MyDate1.addMonths(1);
analysisToCreate.add(pa);
}

Test.startTest();
insert analysisToCreate;
Test.stopTest();

}
}

 

On running the test, I am getting the following error: 

Error Message System.QueryException: List has no rows for assignment to SObject

 

Please help

 

Thanks,

Liz

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma
Hi Liz

your query where clause criteria is : WHERE id = :prj.id
while you have not inserted the prj yet.
You will have to insert prj first and then query this;
insert prj;

SOQL Query

All Answers

Bhawani SharmaBhawani Sharma
Hi Liz

your query where clause criteria is : WHERE id = :prj.id
while you have not inserted the prj yet.
You will have to insert prj first and then query this;
insert prj;

SOQL Query
This was selected as the best answer
lizmijohnlizmijohn

Thanks Bhawani. It worked:-)