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
chriseustacechriseustace 

Apex Trigger - trigger too deep problem

Hello all,

I think the reason for this issue is because I am looking to do it on both insert and update.  Basically, what I need to do is have an AccountID populated into specific fields based on the Contact that is selected.  Take a look at the sample below.  It does work upon insert, but I get errors afterwards - I think the trigger gets caught in an infinite loop. Any help is much appreciated!

 

trigger AutoFill_ContactPicklists on Contact (after insert, after update) {
            List<Contact> con_list = new List<Contact>();
    for(Contact con : trigger.new)
    {
    
        Contact c = new Contact();
        /* SOURCE */
        if(con.Business_Source__c != null)
        {
            for(Contact c_update : [select Id,AccountId from Contact where Id =:Trigger.new[0].Id]  )
            {
                con_list.add(new Contact(Id=Trigger.new[0].Id,Source_Account__c=c_update.AccountId));
            }

        }

 

            if(con_list.size() > 0)
            {
            update con_list;
            }
}

BritishBoyinDCBritishBoyinDC

That seems over complex as  a trigger, and referencing the [0] means it won't work in bulk...

 

There may be more to your logic, but this seems to work in my instance...

 

 

trigger AutoFill_ContactPicklists on Contact (before insert, before update) {
          
    for(Contact con : trigger.new) 
    {
        if(con.Business_Source__c != null) 
        {
        con.Source_Account__c = con.AccountId;
        }

        }
}

 

 

chriseustacechriseustace

awesome, thanks!  I will try it later today.  Thanks again.

chriseustacechriseustace

Actually that won't work.  I need to pull the Account ID of the Contact I am populating in a different field.  It's not the main account ID of the Contact.  Any other ideas?

BritishBoyinDCBritishBoyinDC
Not sure I follow... Which field on the contact is being updated with which other value? Not seeing how my code differs from the code you published?