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
larrmill59larrmill59 

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

Jeremy_nJeremy_n

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:

 

 

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),
    RecordTypeID = '012S00000000PV0'
);

 

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:

 

ID rtid = [select id from Recordtype where Name = 'My Recordtype' and SObjectType = 'AE_Sponsorship__c' limit 1];

for (AE_Sponsorship__c a: trigger.new){
        IF(a.RecordTypeId == rtid)
        {
            //process...
        }
    }

 This will work in any org that has a Recordtype by that name. 

 

Good luck!

 

Jeremy

 

larrmill59larrmill59

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)

 

trigger AccUpdate on AE_Sponsorship__c (after insert, after update) {
//Query for the Account record types
     List<RecordType> rtypes = [Select Name, Id From RecordType 
                  where sObjectType='AE_Sponsorship__c' limit 1];
     
     //Create a map between the Record Type Name and Id for easy retrieval
     Map<String,String> accountRecordTypes = new Map<String,String>{};
     for(RecordType rt: rtypes)
        accountRecordTypes.put(rt.Name,rt.Id);

    for (AE_Sponsorship__c a: trigger.new){
            if(a.RecordTypeId==accountRecordTypes.get('DO Nomination'))
            {
            Account myAcc = [select Id from Account where ID = : a.Account__c];
        myAcc.Test_Update_from_AE_sponsorship__c = a.lastmodifiedDate;
        update myAcc;
        }
    }
}

 

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.

 

@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),
    RecordTypeID = 'DO Nomination'
);
insert sponsor1;
}
}

Thanks again,