You need to sign in to do that
Don't have an account?
Jaguar
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.');
}
}
}
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.');
}
}
}
@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);
}
}
WHERE Report_Date__c < :aar.Report_Date__c
AND Report_Status__c = 'OPEN' and Property__c = :aar.Property__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.
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. :(