You need to sign in to do that
Don't have an account?
Help writing a test class for a trigger
First of all, I know little to nothing about Apex. I can kind of follow existing code and make minor modifications, but that's about it. I enlisted someone's help in writing the trigger below, but their test class only gives 40% code coverage. I can see in the test class what is missing, but I don't know how to write it to pick up the rest. Here is the trigger:
trigger AccUpdate on AE_Sponsorship__c (after insert, after update) {
for (AE_Sponsorship__c a: trigger.new){
IF(a.RecordTypeId=='012S00000000PV0')
{
Account myAcc = [select Id from Account where ID = : a.Account__c];
myAcc.Test_Update_from_AE_sponsorship__c = a.lastmodifiedDate;
update myAcc;
}
}
}
Here is the test class:
@isTest
private class AccUpdateTest {
private static testmethod void SponsorshipInsert(){
AE_Sponsorship__c sponsor1 = new AE_Sponsorship__c(Overall_Monthly_Volume__c=100, Met_with__c='jim',
Expected_Flagstar_Volume__c=100, Approved_for_CFL__c=TRUE, Account__c = '0015000000Jo7v5', Date_of_Visit__c=Date.newInstance(2010, 07, 15));
insert sponsor1;
}
}
As you can see it only inserts the sponsorship but doesn't populate the date/time field on the account record. I know this should be a simple thing but with absolutely no experience in Apex or Java I'm stuck.
Thanks
Your Trigger specifies it will only work if the record is a particular Recordtype, but the testmethod doesn't create a record of that recordtype. Change to this:
Now, on another issue: I will tell you right now that using the hardcoded RecordTypeID may cause problems when you try to deploy to production. Instead, you can query for the appropriate RecordTypeID based on the name of it and store that in a variable. If the RecordType you want to use is called "My Recordtype", try this:
This will work in any org that has a Recordtype by that name.
Good luck!
Jeremy
Thank you very much for the assistance. However I couldn't get your Record Type selection code to work. I found another method using a map which seems to work. (see full code for my trigger here)
My issue now is that I don't know how to specify the record type in my Test Method without hard coding the RecordTypeID. The last line below causes an error when I try to run test. That was just my last attempt.
Thanks again,