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
Ben Wild 8Ben Wild 8 

Trigger not running on Sheduled Batch apex record update

I have a trigger that updates a field on my line items to say what quarter they are in relative to the current quarter (so that I can use roll up summaries to show totals in fields on the opportunity page). The trigger consists of a very long if statement in order to fit in all the criteria to allow for going round end of calendar years etc. The trigger runs before update and works very well if a record is edited through the UI or updated using the data loader.

The issue I am having is I want it to run across all records at the start of each quarter as the value becomes incorrect as we go in to a new quarter so the trigger has to run again to update. Currently I just run an update with the data loader but I would like to to be automated. I wrote a Batch apex class that I scheduled to run every day and if the date is the start of a quarter, run an update on all of the opportunity line items in an attempt to have the trigger run. The Batch class runs as it should but I cant seem to get the trigger to run, I'm not getting any errors so I'm a bit confused as to what is going wrong.

My code below (apolgies for any glaring errors or bad practice, batch scheduled apex is new to me):
 
//BATCH APEX CLASS
global class UpdateOpportunityLineItems implements 
    Database.Batchable<sObject>, Database.Stateful {
    
    // instance member to retain state across transactions
    global Integer recordsProcessed = 0;

    global Database.QueryLocator start(Database.BatchableContext bc) {
        //Get the opportunities from the database
        return Database.getQueryLocator(
            'SELECT ID, Trigger_Update__c, Date_Today__c FROM OpportunityLineItem ');
    }

    global void execute(Database.BatchableContext bc, List<OpportunityLineItem> opp){
        // Process each batch of records
        List<OpportunityLineItem> oppLineItemsToUpdate = new List<OpportunityLineItem>();
        for (OpportunityLineItem oppLineItem : opp) {
            if (oppLineItem.Date_Today__c == Date.newInstance(Date.Today().year(), 11, 15)){
                oppLineItem.Trigger_Update__c = TRUE;
                oppLineItemsToUpdate.add(oppLineItem);
            }
            else if (oppLineItem.Date_Today__c == Date.newInstance(Date.Today().year(), 8, 1)){
                oppLineItem.Trigger_Update__c = TRUE;
                oppLineItemsToUpdate.add(oppLineItem);
            }
            else if (oppLineItem.Date_Today__c == Date.newInstance(Date.Today().year(), 11, 1)){
                oppLineItem.Trigger_Update__c = TRUE;
                oppLineItemsToUpdate.add(oppLineItem);
            }
            else if (oppLineItem.Date_Today__c == Date.newInstance(Date.Today().year(), 2, 1)){
                oppLineItem.Trigger_Update__c = TRUE;
                oppLineItemsToUpdate.add(oppLineItem);
            }
        }
        update oppLineItemsToUpdate;
    }
        
    global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed.');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
        // call some utility to send email
        //EmailUtils.sendMessage(a, recordsProcessed);
    }    
}
//SCHEDULABLE CLASS
global class ScheduleOpportunityLineItemUpdate implements Schedulable {

    global void execute(SchedulableContext sc){
        UpdateOpportunityLineItems uol = new UpdateOpportunityLineItems();
        database.executebatch(uol,1);
    }
}
//TRIGGER
trigger SetProductQuarter on OpportunityLineItem (before insert, before update) 
{
    
    for(OpportunityLineItem opitem : Trigger.new)
    {
        if(opitem.Product_Quarter_Number__c == 1)
        {
            if(opitem.Date_Today__c.Month() < 5)
            {
                if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year())
                {
                    opitem.Product_Quarter__c = 'Q1';
                }
                else if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year() - 1)
                {
                    opitem.Product_Quarter__c = 'P1';
                }
                else
                {
                    opitem.Product_Quarter__c = '=';   
                }
            }
            else
            {
                if(opitem.Date_Today__c.Month() > 4)
                {
                    if(opitem.Date_Today__c.Month() > 7)
                    {
                        if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year() + 1)
                        {
                            opitem.Product_Quarter__c = 'Q1';
                        }
                        else
                        if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year())
                        {
                            opitem.Product_Quarter__c = 'P1';
                        }
                        else
                        {
                            opitem.Product_Quarter__c = '=';
                        }
                    }
                    else
                    if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year())
                    {
                        opitem.Product_Quarter__c = 'Q1';   
                    }
                    else
                    {
                        opitem.Product_Quarter__c = '=';
                    }
                }
                else
                {
                    opitem.Product_Quarter__c = '=';
                }
            }
        }
        else if(opitem.Product_Quarter_Number__c == 2)
        {
            if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year())
            {
                if(opitem.Date_Today__c.Month() < 11)
                {
                    opitem.Product_Quarter__c = 'Q2';
                }
                else
                {
                    opitem.Product_Quarter__c = 'P2';
                }
            }
            else if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year() + 1)
            {
                if(opitem.Date_Today__c.Month() > 10)
                {
                    opitem.Product_Quarter__c = 'Q2';
                }
                else
                {
                    opitem.Product_Quarter__c = '=';
                }
            }
            else if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year() - 1)
            {
                if(opitem.Date_Today__c.Month() < 11)
                {
                    opitem.Product_Quarter__c = 'P2';
                }
                else
                {
                    opitem.Product_Quarter__c = '=';
                }
            }
            else
            {
                opitem.Product_Quarter__c = '=';
            }
        }
        else if(opitem.Product_Quarter_Number__c == 3)
        {
            if(opitem.Current_Quarter__c > 3)
            {
                if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year())
                {
                    if(opitem.ServiceDate.Month() >= 11)
                    {
                        opitem.Product_Quarter__c = 'Q3';
                    }
                    else if(opitem.ServiceDate.Month() < 5)
                    {
                        opitem.Product_Quarter__c = 'P3';
                    }
                    else
                    {
                        opitem.Product_Quarter__c = '=';   
                    }
                }
                else if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year() - 1)
                {
                    if(opitem.ServiceDate.Month() >=11)
                    {
                        opitem.Product_Quarter__c = 'P3';
                    }
                    else
                    {
                        opitem.Product_Quarter__c = '=';
                    }
                }
                else
                {
                    opitem.Product_Quarter__c = '=';   
                }
            }
            else if(opitem.Current_Quarter__c < 3)
            {
                if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year())
                {
                    if((opitem.ServiceDate.Month() < 2 && opitem.Date_Today__c.Month() < 2))
                    {
                        opitem.Product_Quarter__c = 'Q3';
                    }
                    else if(opitem.ServiceDate.Month() < 2 && opitem.Date_Today__c.Month() > 1)
                    {
                        opitem.Product_Quarter__c = 'P3';
                    }
                    else
                    {
                        opitem.Product_Quarter__c = '=';
                    }
                }
                else if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year()+1)
                {
                    if(opitem.ServiceDate.Month() < 2)
                    {
                        opitem.Product_Quarter__c = 'Q3';
                    }
                    else
                    {
                        opitem.Product_Quarter__c = '=';
                    } 
                }
                else if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year()-1)
                {
                    if(opitem.ServiceDate.Month() > 10)
                    {
                        opitem.Product_Quarter__c = 'P3';
                    }
                    else
                    {
                        opitem.Product_Quarter__c = '=';
                    }
                }
                else
                {
                    opitem.Product_Quarter__c = '='; 
                }  
            }
            else if(opitem.Current_Quarter__c == 3)
            {
                if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year())
                {
                    if(opitem.ServiceDate.Month() > 10)
                    {
                        if(opitem.Date_Today__c.Month() > 10)
                        {
                            opitem.Product_Quarter__c = 'Q3';
                        }
                        else
                        {
                            opitem.Product_Quarter__c = '=';
                        }
                    }
                    else if(opitem.ServiceDate.Month() < 2)
                    {
                        if(opitem.Date_Today__c.Month() < 2)
                        {
                            opitem.Product_Quarter__c = 'Q3';
                        }
                        else
                        {
                            opitem.Product_Quarter__c = 'P3';
                        }
                    }
                    else
                    {
                        opitem.Product_Quarter__c = '=';
                    } 
                }
                else if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year() + 1)
                {
                    if(opitem.ServiceDate.Month() < 2)
                    {
                        opitem.Product_Quarter__c = 'Q3';
                    }
                    else
                    {
                        opitem.Product_Quarter__c = '=';
                    }
                }
                else if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year() - 1)
                {
                    if(opitem.ServiceDate.Month() > 10)
                    {
                        if(opitem.Date_Today__c.Month() < 2)
                        {
                            opitem.Product_Quarter__c = 'Q3';
                        }
                        else
                        {
                            opitem.Product_Quarter__c = 'P3';
                        }
                    }
                    else if(opitem.ServiceDate.Month() < 2)
                    {
                        if(opitem.Date_Today__c.Month() < 2)
                        {
                            opitem.Product_Quarter__c = 'P3';
                        }
                        else
                        {
                            opitem.Product_Quarter__c = '=';
                        }  
                    }
                    else
                    {
                        opitem.Product_Quarter__c = '=';
                    }
                }
                else
                {
                    opitem.Product_Quarter__c = '=';
                }
            }
        }
        else if(opitem.Product_Quarter_Number__c == 4)
        {
            if(opitem.ServiceDate.Year()== opitem.Date_Today__c.Year())
            {
                if(opitem.ServiceDate.Month() < 5)
                    {
                        if(opitem.Date_Today__c.Month() < 5)
                        {
                            opitem.Product_Quarter__c = 'Q4';
                        }
                        else
                        {
                            opitem.Product_Quarter__c = '=';
                        }
                    }
                    else
                    {
                        opitem.Product_Quarter__c = '=';
                    }
            }
            else if(opitem.ServiceDate.Year() == opitem.Date_Today__c.Year() + 1)
            {
                if(opitem.Date_Today__c.Month() < 5)
                {
                    opitem.Product_Quarter__c = '=';
                }
                else
                {
                    opitem.Product_Quarter__c = 'Q4';
                }    
            }
            else if(opitem.ServiceDate.Year() - opitem.Date_Today__c.Year() > 1)
            {
                opitem.Product_Quarter__c = '=';
            } 
            else
            {
                opitem.Product_Quarter__c = '=';  
            }
                    
        }
    }  
}

I have tried chnaging the batch size (hence why it is currently 1) in case I was hitting any limits but that didnt seem to help. Any ideas?

Thanks,

Ben