You need to sign in to do that
Don't have an account?

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; } |
Hi ,
try this:
Hope it helps!
Regards
Sam
All Answers
Hi ,
try this:
Hope it helps!
Regards
Sam
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
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!
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.
Hi
Hope it helps!
-Sam
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!
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
Hi?
Just add the @isTest annotation in the 1st line. This should solve the issue. Something like this:
@isTest
public class YourTriggerTestClass{
.......
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.