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
Craig GroveCraig Grove 

Error on Trigger deployment to production (worked in sandbox)

I'm new to triggers and apex, but was attempting a simple trigger to create a child object on Contact object. Goal is to establish a contact to account relationship when creating a contact using the contacts Name and Account to populate the relationship object. Here is the trigger I wrote, which worked in Sandbox.

trigger CreateContactAccountAssociation on Contact (after insert)
{    List<Contact_Account_Association__c> Associations = new List<Contact_Account_Association__c>();
 
    //For each Contact processed by the trigger, add a new
    //Association record for the specified user.
    //Note that Trigger.New is a list of all new Contacts
    //That are being created.
 
    for (Contact newContact: Trigger.New) {
        if (newContact.RecordTypeId !='012700000009iHg' ) {
            Associations.add(new Contact_Account_Association__c(
                Account__c = newContact.AccountId,
                Contact__c = newContact.Id));
        }
    }

    insert Associations;
}

Trigger worked in Sandbox......Created below apex class  which passed in Sandbox

@IsTest(SeeAllData=True)
public class TestTrigger {

    static testmethod void insertContact() {
  
        Contact u = new Contact();
      
            u.Salutation = 'Mr.';
            u.FirstName = 'Test';
            u.LastName = 'Trigger';
            u.AccountId = '00170000011BckQ';
      
        insert u;
    }
}

Received below errors when deploying to production

Deployment Validation Errors

Any assistance is appreciated!!
Best Answer chosen by Craig Grove
EnreecoEnreeco
Are you sure there is not another Trigger on Contact object that maybe blanks the AccountId field?

All Answers

EnreecoEnreeco
There is some required field (not shown in this image) that is not set or in the Contact object or in the Contact_Account_Association__c object (see the "..." on the error description)
Craig GroveCraig Grove
the missing field is account__c from the child record. the account id is supposed to carry over from the contact created in the test class to the child rcord (contact_account_association__c).
EnreecoEnreeco
Are you sure there is not another Trigger on Contact object that maybe blanks the AccountId field?
This was selected as the best answer
Craig GroveCraig Grove
I don't believe that is the case. I did review the errors displayed. The errors do reference other Apex code and the lines in reference are in regards to Contact inserts, however, I the missing field is in regards to the Account Name on the Contact-Account Association. I believe the issue is related to my test class not referencing a valid or retrievable account during test. I'm having a similar issue on another class I am attempting to create on a different object.  Any advise on how to create a test class to reference a lookup field on the parent record would be appreciated. Thanks!
Craig GroveCraig Grove
You are in part correct sir, and thank you! There were two classes which were already in my org which pass when running tests, but when attempting to run along side my new class would produce errors. This was due to those test classes not meeting newer validation rules. I have updated the existing apex to include location, and my new test class passed.