Skip to content

Instantly share code, notes, and snippets.

@olgaloza
Created April 12, 2018 19:19
Show Gist options
  • Save olgaloza/b08ee5e061ca620e744a186ae7b65e1f to your computer and use it in GitHub Desktop.
Save olgaloza/b08ee5e061ca620e744a186ae7b65e1f to your computer and use it in GitHub Desktop.
Test Apex Triggers Challenge
@isTest
private class TestRestrictContactByName {
@isTest static void testInvalidName() {
//try inserting a Contact with INVALIDNAME
Contact myConact = new Contact(LastName='INVALIDNAME');
insert myConact;
// Perform test
Test.startTest();
Database.SaveResult result = Database.insert(myConact, false);
Test.stopTest();
// Verify
// In this case the creation should have been stopped by the trigger,
// so verify that we got back an error.
System.assert(!result.isSuccess());
System.assert(result.getErrors().size() > 0);
System.assertEquals('Cannot create contact with invalid last name.',
result.getErrors()[0].getMessage());
}
}
@tienle488
Copy link

Thank you so much for this, @olgaloza. The Trailhead did not make "writing apex tests proficiently" a pre-requisite and so I really don't have a clue as how to write this test. I will know it soon though. Thanks again for your help!!

@rbeachHYP
Copy link

I know this is old but I am trying to learn this content and I'm confused---I passed the challenge but when I run the tests in the Developer Console the tests say that they fail:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Last Name "INVALIDNAME" is not allowed for DML: []

Obviously I was expecting the issue with the "INVALIDNAME" but I would expect that the test would succeed because I was testing for the failure. I am worried that if I were to try to deploy this code that I wouldn't be able to because the Apex tests would fail.

Thoughts? Thanks so much.

@phaniambati6
Copy link

I know this is old but I am trying to learn this content and I'm confused---I passed the challenge but when I run the tests in the Developer Console the tests say that they fail:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Last Name "INVALIDNAME" is not allowed for DML: []

Obviously I was expecting the issue with the "INVALIDNAME" but I would expect that the test would succeed because I was testing for the failure. I am worried that if I were to try to deploy this code that I wouldn't be able to because the Apex tests would fail.

Thoughts? Thanks so much.

Hey rbeachHYP

Ofcourse the test result leads to DML Exception because thats where the Trigger is firing. But yeah you get to see 100% code coverage for its respective Apex Class. And when its about Deployment , give a try with change sets. As the Apex test coverage is 100 percent it shouldnt be an issue.

@limkavitch
Copy link

I know this is old but I am trying to learn this content and I'm confused---I passed the challenge but when I run the tests in the Developer Console the tests say that they fail:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Last Name "INVALIDNAME" is not allowed for DML: []

Obviously I was expecting the issue with the "INVALIDNAME" but I would expect that the test would succeed because I was testing for the failure. I am worried that if I were to try to deploy this code that I wouldn't be able to because the Apex tests would fail.

Thoughts? Thanks so much.

Have you put INVALIDNAME inside single quotes for validation?

if you do this:

Contact contact= new Contact(LastName='INVALIDNAME');

the code will compile
but if you do this instead:

Contact contact= new Contact(LastName=INVALIDNAME);

Salesforce will try to use the value of a custom field named INVALIDNAME in the object Contact, and won't compile.
This will be shown on the "Problems" tab on Developer Console

@eldevitto
Copy link

I know this is old but I am trying to learn this content and I'm confused---I passed the challenge but when I run the tests in the Developer Console the tests say that they fail:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Last Name "INVALIDNAME" is not allowed for DML: []

Obviously I was expecting the issue with the "INVALIDNAME" but I would expect that the test would succeed because I was testing for the failure. I am worried that if I were to try to deploy this code that I wouldn't be able to because the Apex tests would fail.

Thoughts? Thanks so much.

Remove line 7, "insert myConact;". That is causing the issue you are seeing with the test failing. The db insert between test.start/test.stop, will save the result and then the assertions will pass, and the test will run successfully.

@vaugood
Copy link

vaugood commented Jul 14, 2024

Just reporting a silly mistake I made. Wanted to post this here in case anybody else makes the same mistake I did.
I was getting the error:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Last Name "INVALIDNAME" is not allowed for DML

and found that I was missing the false argument in the Database.insert() statement (line 11).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment