I'm having problems building a test class for a custom apex trigger. No matter what I try I always get compile errors. Here is a copy of the trigger.

This is designed to automatically add the Quote contact as the Primary Opportunity contact (if blank).

trigger OpportunityContactRoleTrigger on Quote (after insert, after update) {
    List<OpportunityContactRole> roles = new List<OpportunityContactRole>();
    for (Quote q : [SELECT Id, Contact.Id, OpportunityId FROM Quote WHERE Id =: Trigger.new AND OpportunityId != null]) {
        OpportunityContactRole role = new OpportunityContactRole();
        role.ContactId = q.Contact.Id;
        role.OpportunityId = q.OpportunityId;
        role.Role = 'Other';
    insert roles;

I do have some required fields on the quote, which I think is causing the problem.

EDIT: Error message when trying to veryify deployment in production:

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityContactRoleTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Co...

  • 3
    Please share the errors you are getting. – Chris Duncombe Feb 27 '14 at 10:48
  • are the errors you are having compiling coming from the trigger, or the test class? please post either the error message, or the test class too! – Simon Lawrence Feb 27 '14 at 10:48
  • You need to provide us with more information on your test class for anyone to be able to troubleshoot it. Show us what you are trying (by posting your test class code) and tell us exactly what error messages you are getting. – Jesse Altman Feb 27 '14 at 12:59
  • 4
    While you posted the code, you haven't posted the error messages you are receiving. – Daniel Hoechst Feb 27 '14 at 14:30
  • You took the test out to put the error in, and then left the critical part of the error out - the name of the missing field. Look at the error message, then at your code, and see if that doesn't straighten things out. – Thomas Taylor Feb 28 '14 at 21:30

This line:

q.Contact.Id = '*REMOVED*'; 

should be

q.ContactId = '*REMOVED*';

in order to populate the lookup from Quote to Contact. I'd also recommend creating the contact as test data rather than using an existing one, for consistency's sake.

| improve this answer | |
  • Actually, q.Contact.Id should still work. Using q.ContactId is definitely the preferred method since if you are updating that field, it is the required way to do it. If you are just getting the value though, it won't complain. – dphil Jan 15 '15 at 18:49

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.