You need to sign in to do that
Don't have an account?
Trigger not firing from unit test
Hi All,
I've written a trigger on the lead object :
trigger foo on Lead (after update)
{
System.debug('Starting foo');
if (Trigger.old[0].IsConverted == false && Trigger.new[0].IsConverted == true)
{
// lead is being converted do sanity check to make sure all fields are filled out if not
// add a Trigger.new[0].addError('missing info') to fail the conversion.
}
}
running this code in the UI seems to work fine which is a good start. Now I'm trying to write my unit tests for this class however I believe that the trigger is not being fired (Test coverage of selected Apex Trigger is 0% at least 1% test coverage is required.) Also the 'Starting foo' debug message is not present in the log file. I have the log level set to finest possible.
Here is my first unit test method
static testMethod void foo_ValidateOnConvert_ValidValues()
{
Lead myTestLead = new Lead();
myTestLead.Email = 'kbradley@merx.com';
myTestLead.LastName = 'Bradley';
insert myTestLead;
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(myTestLead.id);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess() == true);
}
TIA for any ideas and suggestions,
KJB
No problem, glad I could help.
Not too sure on the Eclipse thing, mostly because I don't use it.
All Answers
Here's what I got when trying to run that test class:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Company]: [Company]
So yeah, you'll have 0% test code coverage when the test class fails.
Hi Cragmh,
Thanks for the reply!
The missing company assigment is a copy & paste error on my part. I've emmited the custom field assignments that I do as well in hopes that a shorter example would make the problem more obvious.
KJB
Ok, the company was the only required field that I was missing. Added it in, and the trigger works fine. Even got 100% code coverage. Do you have anything else firing for Leads?
Hmm...not that I can see. I'll reduce my full version to this more simplified one and see if that works. Sounds like I'm on the right track though (thanks for that).
KJB
Ok reduced the trigger code to the following and get 100% coverage:
trigger Lead_Enteprise_ValidateOnConvert on Lead (after update)
{
System.Debug('Processing converted lead');
}
However searching the debug logs does not turn up this string (Did the trigger actually fire?).
if I switch to the following then I'm back to my 0% coverage problem:
trigger Lead_Enteprise_ValidateOnConvert on Lead (after update)
{
if (Trigger.old[0].IsConverted == false && Trigger.new[0].IsConverted == true)
{
System.Debug('Processing converted lead');
}
}
The puzzle continues...
KJB
That's really odd, it should at least hit the conditional.
Are you testing from the SFDC IDE, or a third-party IDE?
Eclipse (Helos) with Force.com plugin. You?
SFDC IDE. I have a feeling that you have an exception occurring that isn't immediately apparent in the Eclipse IDE.
Confirmed! I get different results from the Eclipse IDE vs the SFDC Web UI. Good call!! Any Idea on how I report this back to the Eclipse Plugin dev team?
Thanks again for the assist!
KJB
No problem, glad I could help.
Not too sure on the Eclipse thing, mostly because I don't use it.
Morning Craigmh,
I was wondering why you don't use it? Any particular reason or does the SFDC Web UI suit your needs?
KJB
I find it just as clunky as the SFDC UI, and it doesn't seem to work quite right. I've seen a lot of little things like this pop up, and it seems that with every new version of SFDC, the plug-in has issues. So I shy away from it.