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
Vladimir BessonovVladimir Bessonov 

trigger after update recursion

Hi. I have the following code. 
It works when I update the status of part - the code below updates the dates when the status was changed. 
It has a Recursion constant that prevents Apex code run forever. It works every time the status of part is updated. It works just fine. 

After the dates of each part are updated and component status is updated, I call the same static method from the class below (own trigger for Truck__c)  but Recursion is still True and the code is not executed. 

Can someone explain why is so? 

I
public with sharing class UpdateDateOnStatusChange {
    public UpdateDateOnStatusChange() {

    }

    public static void updateDate(ID PartID, String NewStatus) {
        System.debug('UpdateDateOnStatusChange.updateDate is called: ID, Status' + PartID + ' ' + NewStatus);
        System.debug(Recursion.quoteRecursion);
        if(Recursion.quoteRecursion)
        return;
      Recursion.quoteRecursion = true;
            
        Schema.SObjectType PartType = PartID.getSObjectType();
       // String ListPartType = 'List<' + PartType + '>';
        System.debug(PartType); // Propulsion_Part__c or Truck_Part__c
        // date to update 
        String DateField; 
        Date UpdateDate = Date.today();

        
        SObject Part = (SObject)Type.forName(String.valueOf(PartType)).newInstance();
        String qryStringPart = 'SELECT Id, Sent_Date__c, Arrival_Date__c, Acceptance_Date__c, End_of_Warranty__c FROM ' + String.valueOf(PartType) + ' WHERE ID ' + '=' + '\'' + PartID+ '\'';  
        system.debug('***********************Build Query == ' + qryStringPart);                  
        // SObject resultObject = Database.query(qryString);
        Part = Database.query(qryStringPart); // shell return 1 record only
        system.debug('Part :' + Part);
        
        switch on NewStatus {
            when 'SENT' {
                Part.put('Sent_Date__c',UpdateDate);
                Part.put('Arrival_Date__c', Null);
                Part.put('Acceptance_Date__c',Null);
                Part.put('End_of_Warranty__c',Null);
            }
            when 'ARRIVED' {
                Part.put('Arrival_Date__c', UpdateDate);
                Part.put('Acceptance_Date__c',Null);
                Part.put('End_of_Warranty__c',Null);
            }
            when 'ACCEPTED/WARRANTY' {
                Part.put('Acceptance_Date__c', UpdateDate);
                Part.put('End_of_Warranty__c',UpdateDate.addMonths(36));
            }
            when 'END OF WARRANTY' {
                Part.put('End_of_Warranty__c', UpdateDate);
            }
            
        }
        System.debug('PartID, Status, UpdateDate, Part' + PartID + ' ' + NewStatus + ' ' + UpdateDate + ' ' + Part);
        update Part;
    }
}
The recursion class is below:
 
public class Recursion {
    public static boolean quoteRecursion;
    static {
      quoteRecursion = false;
    }
  }




 
Vladimir BessonovVladimir Bessonov
I found my fault. I just forgot to delete the call to this static method in my trigger to update the status. 
It works fine.