4

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.

13

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 record
  • System.assert(e.getDmlIndex(0) == 0); says that the failure was on the very first inserted record
  • System.assert(e.getDmlFields(0).size() == 1); says that the failure was caused by a single field in the offending record
  • System.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.

  • Thank you VERY much. I was pretty sure that this was related to this code being released (let's say) with r7, and we're on r12, so now it's broken. (sigh) – AMM Sep 6 '12 at 18:45
  • This takes me back to my "original" error though. The new code does allow me to keep the Email lines in (great!)... When testing Apex in Sandbox, I get: Error Message System.AssertException: Assertion Failed Stack Trace Class.leadDupePreventerTests_v2.testLeadDupPreventer: line 45, column 1 L45, C1 = System.assert(false); (from the above code) Any clue there..? :-/ Ugh. – AMM Sep 6 '12 at 18:51
  • (I need to get in the habit of formatting Added Comments more cleanly, once I figure it out) – AMM Sep 6 '12 at 18:52
  • Error Message System.AssertException: Assertion Failed – AMM Sep 6 '12 at 18:52
  • 1
    @AMM - btw - it's good practice here to hit the 'accept' tick box thingie if you feel I've answered the question as you stated it. – metadaddy Sep 7 '12 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.