function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Luke Higgins - RFLuke Higgins - RF 

Deployment change set error on apex email trigger Class Name:MyProfilePageControllerTest

Deployment change set error on apex email trigger Class Name:MyProfilePageControllerTest Error Message:FIELD_CUSTOM_VALIDATION_EXCEPTION "Please enter First Name"
I've got an email trigger in a sandbox I'm trying to deploy into production.

Here's the apex:
trigger EmailMessage on EmailMessage (after insert) {
List<Case> casesToAssign = new List<Case>();
AssignmentRule ar = [SELECT Id FROM AssignmentRule WHERE SObjectType = 'Case' AND Active = true LIMIT 1];
Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.AssignmentRuleHeader.AssignmentRuleId = ar.Id;
for(EmailMessage e : Trigger.new) {
if(e.Incoming) {
Case c = new Case(Id = e.ParentId);
c.setOptions(dmlOpts);
casesToAssign.add(c);
}
}
update casesToAssign;
}
Created by frontendloader

Obviously I've just copied this from this person's post.  I don't know anything about apex.

But here's the error I'm receiving while validating the inbound change set for this trigger.

Class Name:  MyProfilePageControllerTest
Method Name:  testSave
Error Message:  System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter First Name: [] 
Stack Trace: Class.MyProfilePageControllerTest.testSave: line 34, column 1

Can anyone help with this error?
Best Answer chosen by Luke Higgins - RF
Stephanie DodsonStephanie Dodson
Ok, yes, you have no test coverage on the code.  Did you write a test for it? If not you will need to do this.

All Answers

Stephanie DodsonStephanie Dodson
My guess would be that its running all tests and this particular test (MyProfilePageControllerTest.testSave)  is trying to insert some object as part of the test that has a field (First Name) that is required by a validation rule?
Luke Higgins - RFLuke Higgins - RF
Not sure if this would be recommended but I've deactivated that validation rule to see if the deployment validation would pass.  It did not.   New error: Code Coverage Error
Stephanie DodsonStephanie Dodson
Meaning not a high enough percentage to pass?
Luke Higgins - RFLuke Higgins - RF
here's the error message after turning off the validation rule.
User-added image

here was the original message.
User-added image
Stephanie DodsonStephanie Dodson
Ok, yes, you have no test coverage on the code.  Did you write a test for it? If not you will need to do this.
This was selected as the best answer
Luke Higgins - RFLuke Higgins - RF
No, I'm not familiar with that.  I'll look for a doc on writing a test for the code.
Stephanie DodsonStephanie Dodson
It will be a separate class.

Something kind of like this:

@isTest
private class TestEmailMessageT {
    static testMethod void TestEmailMessageT(){
        Case a = new Case();
        insert a;

        EmailMessage b = new EmailMessage();
        b.ParentId = a.id;
        b.incoming = true;
        try{
        insert b;
        }
        catch (DMLException e)
        {
            system.debug(e);
        }
    }

}

Basically you have to have something that triggers the trigger if that makes sense.

Once you have it ready, there is a run test button in the upper right corner of the developer console that will allow you to run it in the sandbox.  After it runs, go back to your trigger and check the code coverage.
Luke Higgins - RFLuke Higgins - RF
Bingo!  100% Code Coverage.

User-added image

I created a new outbound change set and validated it in production but got the same error. 
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
Stephanie DodsonStephanie Dodson
Awesome : )
Stephanie DodsonStephanie Dodson
Did the outbound change set have both the trigger and the test class in it?
Luke Higgins - RFLuke Higgins - RF
no...

But they do now.

User-added image

Thank you so much for walking me through this step by step and sticking with me for a couple hours today.  How do I tag all your answers as the best? :)
Stephanie DodsonStephanie Dodson
You are very welcome!