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
BonzBonz 

Help writing a test class for my trigger

Hello!

I'm new to writing apex triggers but I finally got one to work in my sandbox.  My problem now is that I can't put it inot production without a test class, and I'm clueless on how to write it.  Here is what I'm trying to do:

 

When a Lead is created links via a lookup to a custom object called Site; When the field "First Payment Date" on the Site is updated the trigger updats a field named "First Payment Date" on the Lead.

 

How the heck do I write a test class for this??  Any help would be greatly appreciated!!

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
trigger updateFirstPaymentDate on Site__c(after update, after insert) {
    Integer i = 0;
    Set<ID> leadIds = new Set<ID>();
    Map<ID, Date> id2Date = new Map<ID, Date>();
    for (Site__c o : Trigger.new)
    {
        if ((Trigger.isInsert && o.First_Payment_Date__c != null) ||
            Trigger.old[i].First_Payment_Date__c != o.First_Payment_Date__c)
        {
            leadIds.add(o.lead__c);
            id2Date.put(o.lead__c, o.First_Payment_Date__c);
        }     
        i++;    
    }
    
    List<Lead> leads = [select id  from Lead where id in :leadIds];
    
    for (Lead c : leads)
    {
        c.First_Payment_Date__c = id2Date.get(c.Id);
    }
    
    update leads;
}

Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

Hi ,

 

try this:

 

@isTest 
private class updateFirstPaymentDateTest{
    static testMethod void firstPaymentTest() {
        lead l = new lead();
        l.name = 'test Leads';
        //add all the necessary / required fields.
        insert l;
        
        site__c s = new site__c();
        s.name = 'test';
        s.First_Payment_Date__c = system.today();
        s.lead__c = l.id;
        //add all the necessary / required fields.
        insert s;
    }
}

 Hope it helps!

 

Regards

Sam

 

 

All Answers

jungleeejungleee

Hi ,

 

try this:

 

@isTest 
private class updateFirstPaymentDateTest{
    static testMethod void firstPaymentTest() {
        lead l = new lead();
        l.name = 'test Leads';
        //add all the necessary / required fields.
        insert l;
        
        site__c s = new site__c();
        s.name = 'test';
        s.First_Payment_Date__c = system.today();
        s.lead__c = l.id;
        //add all the necessary / required fields.
        insert s;
    }
}

 Hope it helps!

 

Regards

Sam

 

 

This was selected as the best answer
BonzBonz

Hi Sam,

First - thanks so much for your help.  I tried out the class but am having 1 problem, I got this error when I tried save the class:

 

Error: Compile Error: Field is not writeable: Lead.Name at line 5 column 9

 

I'm not sure why the Lead.Name wouldn't be writable, but any suggestion on what else I can try?  

Marcus

BonzBonz

Sam - thanks again for your help.  With some tinkering I figured it out!  Here is how the test class look now:

 

@isTest 
private class updateFirstPaymentDateTest{
    static testMethod void firstPaymentTest() {
        lead l = new lead();
        l.lastname = 'test Leads';
        l.company = 'test company';
        //add all the necessary / required fields.
        insert l;
        
        site__c s = new site__c();
        s.First_Payment_Date__c = system.today();
        s.lead__c = l.id;
        //add all the necessary / required fields.
        insert s;
    }
}

 

 

Thanks again for the help!

kevin.chileskevin.chiles

Hello!  I am having the same issues.  I am ok at writing triggersbut I cannot get my test class to work correctly.  Any help would be appreciated!  Here is the trigger.

 

Trigger updateRelatedOpportunity on dsfs__DocuSign_Status__c(after update){

List<Id> oppIds = new List<Id>();
for(dsfs__DocuSign_Status__c dc:trigger.new){
if(dc.dsfs__Completed_Date_Time__c<>NULL){ 
oppIds.add(dc.dsfs__Opportunity__c); 

}
}

List<Opportunity> oppList = [select StageName from Opportunity where id in:oppIds];
List<Opportunity> oppListnew = new List<Opportunity>();

for(Opportunity op : oppList){
op.StageName='Closed Won';
oppListnew.add(op);
}
update oppListnew;
 
}

 

jungleeejungleee

Hi

 

Hope it helps!

 

private class YourTriggerTestClass{
	static testMethod void yourTestMethod(){
		//insert account
		account acc = new account();
		acc.name='test account';
		//fill all the required field and make sure your data passes the validation rules.
		insert acc;
		
		opportunity opp = new opportunity();
		opp.name = 'Test oppty';
		opp.account = acc.id;
		//fill all the required field and make sure your data passes the validation rules.
		insert opp;
		
		dsfs__DocuSign_Status__c  dsfs = new dsfs__DocuSign_Status__c ();
		dsfs.name='dsjfnjksd'; //do not add this line, if its autonumbered.
		dsfs.dsfs__Completed_Date_Time__c = system.today();
		dsfs.dsfs__Opportunity__c = opp.id;
		insert dsfs;
		
		dsfs.dsfs__Completed_Date_Time__c = system.today().addDays();
		update dsfs;
	}
}

 -Sam

kevin.chileskevin.chiles

Soooo close!  It looks like I am still getting an error of Illegal Assignment from id to SOBJECT:Account at line 11 column 9

 

public class YourTriggerTestClass{
static testMethod void yourTestMethod(){
//insert account
account acc = new account();
acc.name='test account';
//fill all the required field and make sure your data passes the validation rules.
insert acc;

opportunity opp = new opportunity();
opp.name = 'Test oppty';
opp.account = acc.id;
opp.Project_Type__c='Semedica';
opp.CloseDate=system.today();
//fill all the required field and make sure your data passes the validation rules.
insert opp;

dsfs__DocuSign_Status__c dsfs = new dsfs__DocuSign_Status__c ();

dsfs.dsfs__Completed_Date_Time__c = system.today();
dsfs.dsfs__Opportunity__c = opp.id;
insert dsfs;

dsfs.dsfs__Completed_Date_Time__c = system.today().addDays();
update dsfs;
}
}

 

 

 

Thank you so much for your help so far!  Do you why this error would occur?  Again thanks a ton for your help!

kevin.chileskevin.chiles

So I was able to get my class to save correctly!  WHOOOO HOOO!  However I am now running into 0% percent coverage for my class. and I need to get that bad boy up to 75%.  here is the finished code

 

public class YourTriggerTestClass{
    static testMethod void yourTestMethod(){
        //insert account
        account acc = new account();
        acc.name='test account';
        //fill all the required field and make sure your data passes the validation rules.
        insert acc;
        
        opportunity opp = new opportunity();
        opp.name = 'Test oppty';
        opp.Project_Type__c='Semedica';
        opp.StageName='Qualified';
        opp.CloseDate=system.today();
        //fill all the required field and make sure your data passes the validation rules.
        insert opp;
        
        dsfs__DocuSign_Status__c  dsfs = new dsfs__DocuSign_Status__c ();
        
        dsfs.dsfs__Completed_Date_Time__c = system.today();
        dsfs.dsfs__Opportunity__c = opp.id;
        insert dsfs;
        
        dsfs.dsfs__Completed_Date_Time__c = system.today();
        update dsfs;
    }
}
jungleeejungleee

Hi?

Just add the @isTest annotation in the 1st line. This should solve the issue. Something like this:

@isTest
public class YourTriggerTestClass{


.......

kevin.chileskevin.chiles

Yep, I quickly realized this after I posted lol,  I hope someone can use this it seems to be a very common need from my searches.