Can someone tell me why this error would show up in this canned code I am using from SF's dev site? Enterprise Release 12 of SF....
Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 50 column 42
Lead dup1 = new Lead(LastName='Test1Dup', Company='Test1Dup Inc.', Email='test1@duptest.com'); try { insert dup1; System.assert(false); } catch (DmlException e) { System.assert(e.getNumDml() == 1); System.assert(e.getDmlIndex(0) == 0); System.assert(e.getDmlFields(0).size() == 1); System.assert(e.getDmlFields(0)[0] == 'Email'); System.assert(e.getDmlMessage(0).indexOf( 'A lead with this email address already exists.') > -1); }
I am not even clear on the use of System.assert at all, and I've proven to myself that I have next to no clue on what to do to rectify this. :-(
The error is referencing the "System.assert(false)" line.
Attribution to: AMM
Possible Suggestion/Solution #1
The line provoking the error is actually
System.assert(e.getDmlFields(0)[0] == 'Email');
The error is complaining that you're comparing a Schema.SObjectField
value with a String
, which isn't allowed. It looks like getDmlFields()
has changed over the releases. I'll raise a doc bug to get that example code fixed. Changing that line to
System.assert(e.getDmlFields(0)[0].getDescribe().getName() == 'Email');
should fix the error.
System.assert(condition)
is used in test code to check that a given condition is true. If the condition is false, the test as a whole fails, and the location of the failure is given in the test output log.
Working through the asserts in that example:
System.assert(false);
says that you should never get to that line - the insert is expected to throw an exception.System.assert(e.getNumDml() == 1);
says that the failure concerns a single recordSystem.assert(e.getDmlIndex(0) == 0);
says that the failure was on the very first inserted recordSystem.assert(e.getDmlFields(0).size() == 1);
says that the failure was caused by a single field in the offending recordSystem.assert(e.getDmlFields(0)[0].getDescribe().getName() == 'Email');
says that offending field is called 'Email'System.assert(e.getDmlMessage(0).indexOf( 'A lead with this email address already exists.') > -1);
says that the error message should match the expected value.
Basically, the test method is checking that the trigger executed EXACTLY as intended!
The exception methods, such as getNumDml()
, are listed here.
Attribution to: metadaddy
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1215