• Timothy Wade 18
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 5
    Replies
Hello, I am very new to writing Apex triggers and I am practicing. I wrote the following apex trigger:

Basically, what I'm trying to do is the following:

Write a trigger that creates two identical Contacts whenever an Account is created. Make sure both Contacts are associated with the Account. Use any values for the fields on the Contacts - just make sure to use variables when populating the fields of each Contact to make sure they are identical.

rigger CreateIdenticalContacts on Account (after insert) {
    for(Account acc: Trigger.new) {
        Contact con                 =  new Contact();
        con.FirstName          =  'Aki';  
        con.FirstName          = 'Akisey';
        con.LastName           = 'Johnson';
        con.LastName          = 'Johnson';
        con.AccountId           = acc.Id;
        
        insert acc;
    }        
}

Here's my test class:
@isTest
public class IdenticalContactsTest {

    @isTest static void createAccount()  {
        Account acc  = new Account();
        acc.Name                        = 'Test123';
      insert acc;
        
        }
}

Here's the error that I' receiving:

21:41:32:112 FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateIdenticalContacts: execution of AfterInsert

When I run test, I receive 100% code coverage. I even deleted other triggers in my sandbox org to see if the error would go away but it didn't. Any assistance would be greatly appreciated.

Thanks,
Tim
 
Hello, I am very new to writing Apex triggers and I am practicing. I wrote the following apex trigger:

Basically, what I'm trying to do is the following:

Write a trigger that creates two identical Contacts whenever an Account is created. Make sure both Contacts are associated with the Account. Use any values for the fields on the Contacts - just make sure to use variables when populating the fields of each Contact to make sure they are identical.

rigger CreateIdenticalContacts on Account (after insert) {
    for(Account acc: Trigger.new) {
        Contact con                 =  new Contact();
        con.FirstName          =  'Aki';  
        con.FirstName          = 'Akisey';
        con.LastName           = 'Johnson';
        con.LastName          = 'Johnson';
        con.AccountId           = acc.Id;
        
        insert acc;
    }        
}

Here's my test class:
@isTest
public class IdenticalContactsTest {

    @isTest static void createAccount()  {
        Account acc  = new Account();
        acc.Name                        = 'Test123';
      insert acc;
        
        }
}

Here's the error that I' receiving:

21:41:32:112 FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateIdenticalContacts: execution of AfterInsert

When I run test, I receive 100% code coverage. I even deleted other triggers in my sandbox org to see if the error would go away but it didn't. Any assistance would be greatly appreciated.

Thanks,
Tim