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
Michael Snow 5Michael Snow 5 

Trigger not Associating Records through junction object (Or triggering?)

Hi There,

So this is my first time trying to use APEX code to automate a process for our teams - I'm not exactly sure why it is not working (or maybe not even triggering)

I guess my first question would be - is there a way to see if my APEX Trigger is even starting? and if so Is it possible to see a log of the flow of the Trigger is that done through code/comments that I have to write into the code? 

Otherwise, I copied a portion of the APEX below. The goal of this portion of the APEX is as to see if there are any custom objects - `Executive_Business_Review__c` - that are currently associated with the Account that the Opportunity is associated with (when created). These EBR objects must be within 1 year of the Opp close Date to be considered. If there are any EBR's within this one year period I want to associate them with the Opportunity. This association is done through a junction object - `EBR_Opportunity_Association__c`. 

Also, I am only posting this portion of the code for now in the hopes that if I can figure it out for this portion of the trigger then I can figure it out/ fix it for the remaining trigger types. I also plan on moving these loops into APEX Classes of their own but for now am planning to test and develop in this trigger to start 
 
trigger Auto_Associate_Opps_with_EBRs_1yr on Opportunity (after insert,after update, after undelete) {
    // This Trigger Auto Associates EBRs with an Opp when an Opp is created. The EBRs have to be within
    // one year of the close date of the Opp It assumes that there are only 4 within a year so we do 
    // not have a limit to the association of the 4 latest 
    
    //Lets Map out the logic first in commentes
    if (Trigger.isInsert) {
        //After Insert: 
        for(Opportunity Opp : Trigger.New) {
            //First Get the Account ID of the Account on the Opp
            ID OppID = Opp.Id;
            ID AcctID = Opp.Account.Id;
            Date OppCloseDate = Opp.CloseDate;
            Date OppCloseDateLimit;
            OppCloseDateLimit = OppCloseDate.addDays(-365);
            //Are there any EBRs with the Account from within the last year? - YES/NO
            List<Executive_Business_Review__c> EBRs_at_account = 
                [SELECT EBR_Account__c,Id,Name,EBR_Date__c 
                 FROM Executive_Business_Review__c 
                 WHERE EBR_Account__c =:AcctID AND EBR_Date__c >=:OppCloseDateLimit AND EBR_Date__c <=:OppCloseDate];
            
            if (EBRs_at_account.size()>0) {
                //If Yes auto associate those EBR's with the Opp      
                for(Executive_Business_Review__c EBR : EBRs_at_account) {
                    //Here I have to Create an EBR-Opportuity Association: 
                    EBR_Opportunity_Association__c EBR_Opp = new EBR_Opportunity_Association__c();
                    EBR_Opp.Executive_Business_Review__c=EBR.Id;
                    EBR_Opp.Opportunity__c=OppID;
                    insert EBR_Opp;
                    
                }
            }
        }
    }
}

Thanks for all the help and suggestions!