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
Mike Parks 5Mike Parks 5 

Error: List has no rows for assignment to SObject in test class for a simple Trigger


I've written a simple trigger which works fine in Sandbox. However my test class won't work at all and so I am unable to move my trigger into Production. I keep getting the error: "List has no rows for assignment to SObject" on the line Insert prj; in the test class shown below. I'm worried the SObject prj is empty because it requires a RecordType and I'm not creating/setting the RecordType correctly. All of the other Insert's in the Test Class work fine but they don't require RecordType.  
trigger UpdateRelatedGoal on Project__c (before insert) {

for (Project__c p : Trigger.New) {

 if(p.Start_Date_Project__c==Null ) {
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter a Project Start Date'));
        }

    sObject s = [SELECT Id FROM Goals__c WHERE Region_Name__c =:p.Region__c  AND Year__c =:p.Start_Year_Project__c  ];
  
   for(Project__c a : Trigger.New) {
        a.Goal__c = s.Id;
    }
 
    }  
}

Here's my Test Class so far.....
 
@isTest
private class TestUpdateRelatedGoal {
    @isTest static void TestGoalUpdateWithStartDate() {
    
// Create Project Manager
        Project_Manager__c pm = new Project_Manager__c(Name='mwp');
        insert pm;
// Create Account
        Account acct = new Account(Name='Acme');
        insert acct;
// Create Contact
        Contact cont = new Contact(FirstName='joe',LastName='blow1',email='joe@mail.com', title='president');
        insert cont;
// Create Proposal
       Proposal__c prop = new Proposal__c(Name='Test Proposal',Date__c=System.today(),Proposal_Estimate__c=1000, Contact__c=cont.Id, Service_Type__c='Other');
       insert prop;

// Create Project
    RecordType rt = [SELECT Id FROM RecordType WHERE SObjectType='Project__c' AND Name='Confirmation of Service' LIMIT 1];

    Project__c prj = new Project__c(Proposal__c=prop.Id,Start_Date_Project__c=System.today(),End_Date_Project__c=System.today(),Account__c=acct.Id,Project_Contact__c=cont.Id);

    prj.Name = 'test';
    prj.Service_Type__c='Other';
    prj.Substance_Code__c='33 Workforce';
    prj.Project_Manager__c=pm.Id;
    prj.Account__c=acct.Id;

        
        // Perform test
        Test.startTest();
         insert prj;                // This row is where error occurs
        Test.stopTest();

    }

}


 
VamsiVamsi
Hi,

Please specify some value to Start_Year_Project__c   on Project__c  object in test class 

Please mark as best answer if the above helps ..!!!
Mike Parks 5Mike Parks 5
Vamsi I think I already did that in line 21.....

Project__c prj = newProject__c(Proposal__c=prop.Id,Start_Date_Project__c=System.today(),End_Date_Project__c=System.tod
VamsiVamsi
Not sure if Start_Year_Project__c  on Project__c is populated automatically based on Start_Date_Project__c or something , If so then its fine or else Its an issue because below query retrieves the results based on Start_Year_Project__c  not on Start_Date_Project__c.

So please specify some value to Start_Year_Project__c  on Project__c in test class.

sObject s = [SELECT Id FROM Goals__c WHERE Region_Name__c =:p.Region__c  AND Year__c=:p.Start_Year_Project__c  ];
Mike Parks 5Mike Parks 5
Oh I see...yes Start_Year_Project__c is a formula field that calculates from Start_Date_Project__c. 
VamsiVamsi
In that case make sure that Start_Year_Project__c  formula field is setup properly and populating correct value in order to retrieve the Goals__c  records 
Mike Parks 5Mike Parks 5
Vamsi, I know Start_Year_Project__c is setup correctly because the actual trigger works perfectly. It's just the test that is failing. Thanks for all of the suggestions!