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
JaguarJaguar 

Write a Test Class for a Simple Trigger

Can somebody pls help

 trigger trCheckReportStatus on After_Audit_Report__c (before insert) {
    for (After_Audit_Report__c aar : Trigger.new){
        List<After_Audit_Report__c> aar1 = [SELECT id, Property__c, Report_Date__c
                                 FROM After_Audit_Report__c
                                 WHERE Report_Date__c < :aar.Report_Date__c
            AND Report_Status__c = 'OPEN' and Property__c = :aar.Property__c];
           
            if (aar1.size() > 0){
                aar.addError('You have not closed Previous Day Report.  Please close the Previous Day before you can save this.');
            }
    }
}
JaguarJaguar
I did write a Test Class.  This did not work

 @isTest
public with sharing class AARTest{
   
    private static testMethod void insertAAR1(){
        Property__c p1 = new Property__c(Name = 'RRI792');
        insert p1;
       
        date myDate = date.newInstance(2014, 6, 01);
        Property__c p = [SELECT Id FROM Property__c WHERE Name = 'RRI792'];
       
        After_Audit_Report__c aar = new After_Audit_Report__c( Report_Date__c = myDate, Property__c = p.id, REPORT_STATUS__c = 'OPEN');
        insert aar;
       
        date myDate1 = date.newInstance(2014, 6, 02);
        After_Audit_Report__c aar1 = new After_Audit_Report__c( Report_Date__c = myDate, Property__c = p.id, REPORT_STATUS__c = 'OPEN');
        insert aar1;
       
        After_Audit_Report__c aar3 = [SELECT id, Report_Date__c FROM After_Audit_Report__c WHERE id = :aar1.id];
       
            System.assertEquals(null, aar3.id);
    }
}
Vinnie BVinnie B
In the lines below is there a reason that Report_Status__c doesn't have an ':aar' in front of it?

WHERE Report_Date__c < :aar.Report_Date__c
            AND Report_Status__c = 'OPEN' and Property__c = :aar.Property__c];
JaguarJaguar
You don't need it, it belongs to the FROM Object --  After_Aduit_Report__c.
The Trigger is working as expected.  No problems there, I cannot figure out how to write a test class to cover 75% to promote this to Production.
Vinnie BVinnie B
Sorry, I didn't read your post thoroughly enough.

I think I have come across a similar problem.  Oddly, I believe that after inserting an item in a test class you can't just reference its ID.  You need to tell SFDC that this new object is the one with that ID.  I did it for a new account (after "insert anAcc") like this:

  anAcc = [select ID from Account where ID = :anAcc.ID];

So, in your case I would try this line after the insert of p1.

  p1 = [select ID from Property__c where ID = :p1.ID];

NOW (and only now) will 'p1' be considered to be the record you just created, and thus you can reference it by ID.

Please let me know if this works.  I don't claim to be an expert and I could easily be wrong here.  :(