+ Start a Discussion
jtresjtres 

Error during trigger execution. Error while trigger is processing new data

Ok so here is the scenario.  We have an application object which contains information about several entities: 3 contacts and 3 custom objects we have called involvements, one associated with each person.  Inside of the involvement object we have a drop down list with several options and depending on the option you have selected different fields are displayed and different validation rules are applied.  I am not sure if that is what is causing me this issue, but anytime I attempt to insert a new application object I get this error being thrown:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger parseStudentApplication caused an unexpected exception, contact your administrator: parseStudentApplication: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Contact]: [Contact]: Class.StudentApplicationParser.Parse: line 182, column 9

 

The following is my trigger:

trigger parseStudentApplication on Application_For_Admission__c(after insert) {
    List<Application_For_Admission__c> studentApps = Trigger.new;
    {
        StudentApplicationParser sap = new StudentApplicationParser();
        sap.Parse(studentApps);
    } 
}

 

Here is the class that process the trigger:

public with sharing class StudentApplicationParser {
    public void Parse(List<Application_For_Admission__c> sas)
    {
        List<Contact> contacts = new List<Contact>();
        List<Account> accounts = new List<Account>();
        List<npe4__Relationship__c> relationships = new List<npe4__Relationship__c>();
        List<Involvement__c> involvements = new List<Involvement__c>();
        List<Semester__c> semesters = new List<Semester__c>();
        List<School__c> schools = new List<School__c>();
        for(Application_For_Admission__c sa : sas)
        {
            Contact student = new Contact();
            Account studentAcct = new Account();
            Contact mother = new Contact();
            Account motherAcct = new Account();
            Contact father = new Contact();
            Account fatherAcct = new Account();
            npe4__Relationship__c relToMother = new npe4__Relationship__c();
            npe4__Relationship__c relToFather = new npe4__Relationship__c();
            Involvement__c studentInvolvement = new Involvement__c();
            Involvement__c motherInvolvement = new Involvement__c();
            Involvement__c fatherInvolvement = new Involvement__c();
            student.FirstName = sa.First_Name__c;
            student.LastName = sa.Last_Name__c;
            student.Birthdate = sa.Birthdate__c;
            student.MailingStreet = sa.Student_Address__c;
            student.MailingCity = sa.City__c;
            student.MailingState = sa.State__c;
            student.MailingPostalCode = sa.Zip__c;
            student.OtherPhone = sa.Phone__c;
            student.npe01__PreferredPhone__c = 'Other';
            student.npe01__HomeEmail__c = sa.Student_Email_Address__c;
            student.npe01__Preferred_Email__c = 'Home';
            System.debug('Set all student fields');
            mother.FirstName = sa.Mother_First_Name__c;
            mother.LastName = sa.Mother_Last_Name__c;
            mother.npe01__HomeEmail__c = sa.Mother_Email_Address__c;
            mother.npe01__Preferred_Email__c = 'Home';
            mother.npe01__WorkPhone__c = sa.Mothers_Work_Phone__c;
            mother.OtherPhone = sa.Mothers_Home_Phone__c;
            mother.npe01__PreferredPhone__c = sa.Mother_Preferred_Phone__c;
            mother.npe01__WorkPhone__c = sa.Mothers_Work_Phone__c;
            mother.MobilePhone = sa.Mothers_Cell_Phone__c;
            if(sa.Mothers_Address__c == null)
            {   
                mother.MailingStreet = sa.Student_Address__c;
                mother.MailingCity = sa.City__c;
                mother.MailingState = sa.State__c;
                mother.MailingPostalCode = sa.Zip__c;
                System.debug('Set all mother fields with same address as child');
            }
            else
            {
                mother.MailingStreet = sa.Mothers_Address__c;
                mother.MailingCity = sa.Mothers_City__c;
                mother.MailingState = sa.Mothers_State__c;
                mother.MailingPostalCode = sa.Mothers_Zip__c;
                System.debug('Set all mother fields with own address');
            }
            father.FirstName = sa.Father_First_Name__c;
            father.LastName = sa.Father_Last_Name__c;
            father.npe01__HomeEmail__c = sa.Father_Email_Address__c;
            father.npe01__Preferred_Email__c = 'Home';
            father.npe01__WorkPhone__c = sa.Fathers_Work_Phone__c;
            father.OtherPhone = sa.Fathers_Home_Phone__c;
            father.npe01__PreferredPhone__c = sa.Father_Preferred_Phone__c;
            father.npe01__WorkPhone__c = sa.Fathers_Work_Phone__c;
            father.MobilePhone = sa.Fathers_Cell_Phone__c;
            if(sa.Fathers_Address__c == null)
            {
                father.MailingStreet = sa.Student_Address__c;
                father.MailingCity = sa.City__c;
                father.MailingState = sa.State__c;
                father.MailingPostalCode = sa.Zip__c;
                System.debug('Set all father fields with same address as child');
            }
            else
            {
                father.MailingStreet = sa.Fathers_Address__c;
                father.MailingCity = sa.Fathers_City__c;
                father.MailingState = sa.Fathers_State__c;
                father.MailingPostalCode = sa.Fathers_Zip__c;    
                System.debug('Set all father fields with own address');         
            }
            relToMother.npe4__Contact__c = student.Id;
            relToMother.npe4__RelatedContact__c = mother.Id;
            relToMother.npe4__Description__c = 'Parent/Child';      
            relToMother.npe4__Status__c = 'Active'; 
            relToMother.npe4__Type__c = 'Family';
            System.debug('Set relationship to mother');
            relToFather.npe4__Contact__c = student.Id;
            relToFather.npe4__RelatedContact__c = father.Id;
            relToFather.npe4__Description__c = 'Parent/Child';
            relToFather.npe4__Status__c = 'Active';
            relToFather.npe4__Type__c = 'Family';
            System.debug('Set relationship to father');
            //studentInvolvement.Contact__c = student.Id;
            //studentInvolvement.Involvement__r = student.Id;
            studentInvolvement.Involvement_Level__c = 'Student';
            //studentInvolvement.Academic_Semester__c = current.Id;
            studentInvolvement.Grade_Level__c = sa.Grade__c;
            studentInvolvement.Gender__c = sa.Gender__c;
            studentInvolvement.Race__c = sa.Race__c;
            studentInvolvement.Level_Enrolled__c = sa.Enrollment_Level__c;
            studentInvolvement.Date_of_Birth__c = sa.Birthdate__c;
            studentInvolvement.Place_of_Worship__c = sa.Worship_Place__c;
            Semester__c current;
            Integer semCount = [Select Count() From Semester__c a where name = :sa.Academic_Semester__c];
            if(semCount == 0)
            {
               current = new Semester__c(name = sa.Academic_Semester__c);
               semesters.add(current);
            }
            else
            {
                current = [Select Id From Semester__c where name = :sa.Academic_Semester__c];
            }

Best Answer chosen by Admin (Salesforce Developers) 
ehartyeehartye

In these lines...

 

relToMother.npe4__RelatedContact__c =  mother.Id;

relToFather.npe4__RelatedContact__c =  father.Id;

 

...mother.id and father.id do not yet exist. You are assigning null values to the contact id field of your relationship object. You'll need to wait to assign these values until the contact insert has taken place.

All Answers

jtresjtres

  studentInvolvement.Academic_Semester__c = current.Id;
            School__c schl;
            Integer schlCount = [Select Count() From School__c where name = :sa.Attending_School__c];
            if(schlCount == 0)
            {
                 schl = new School__c(name = sa.Attending_School__c);
                 schools.add(schl);
            }
            else
            {
                schl = [Select Id From School__c where name = :sa.Attending_School__c];
            }
            student.Involvement__r.add(studentInvolvement);
            System.debug('Set student involvement object');
            studentAcct.Name = student.FirstName + ' ' + student.LastName;
            studentAcct.npe01__One2OneContact__c = student.Id;
            motherAcct.Name = mother.FirstName + ' ' + mother.LastName;
            motherAcct.npe01__One2OneContact__c = mother.Id;
            fatherAcct.Name = father.FirstName + ' ' + father.LastName;
            fatherAcct.npe01__One2OneContact__c = father.Id;
            //motherInvolvement.Contact__c = mother.Id;
            //motherInvolvement.Involvement__r = mother.Id;
            motherInvolvement.Involvement_Level__c = 'Parent';
            motherInvolvement.Academic_Semester__c = current.Id;
            System.debug('Set mother involvement object');
            mother.Involvement__r.add(motherInvolvement);
            //fatherInvolvement.Contact__c = father.Id;
            //fatherInvolvement.Involvement__r = father.Id;
            fatherInvolvement.Involvement_Level__c = 'Parent';
            fatherInvolvement.Academic_Semester__c = current.Id;
            System.debug('Set father involvement object');
            father.Involvement__r.add(fatherInvolvement);
            contacts.add(student);
            contacts.add(mother);
            contacts.add(father);
            relationships.add(relToMother);
            relationships.add(relToFather);
            involvements.add(studentInvolvement);
            involvements.add(motherInvolvement);
            involvements.add(fatherInvolvement);
            accounts.add(fatherAcct);
            accounts.add(motherAcct);
            accounts.add(studentAcct);
        }
        System.debug('Inserting new contacts');
        insert contacts;
        System.debug('Inserted new contacts');
        System.debug('Inserting new involvements');
        insert involvements; //This is where the error happens
        System.debug('Inserted new involvements');
        System.debug('Inserting new relationships');
        insert relationships;
        System.debug('Inserted new relationships'); 
        insert schools;
        System.debug('Inserted new schools');
        insert semesters;    
        System.debug('Inserted new semesters');
    }   
}

ehartyeehartye

Do you need to un-comment the lines where you're defining the Contact for your involvement records?

 

//fatherInvolvement.Contact__c = fathe r.Id;

jtresjtres

I had attempted to use the code with that line uncommented previously, yet it still had the same error displayed.  That is why the other commented lines are there, I was attempting to take different approaches which seemed to create the relationship but they all ended with the same error message being displayed at the same line. 

ehartyeehartye

In these lines...

 

relToMother.npe4__RelatedContact__c =  mother.Id;

relToFather.npe4__RelatedContact__c =  father.Id;

 

...mother.id and father.id do not yet exist. You are assigning null values to the contact id field of your relationship object. You'll need to wait to assign these values until the contact insert has taken place.

This was selected as the best answer