0

I have written this handler class which is called from the trigger. This code is not updating any on the below field when the trigger is executed. Can anyone please help me debug why my field update is not working?

// Set the "SCMC__Condition_Code__c" field to null

 inspection.SCMC__Condition_Code__c = null;
           

// Set the "SCMC__Quantity_to_Accept__c" field to the "SCMC__Received_Quantity__c" field

inspection.SCMC__Quantity_to_Accept__c = inspection.SCMC__Received_Quantity__c;
            

    public class diab_SetQuantityAndCallAPI {

public static List<SCMC__Receiving_Inspection__c> handleAfterInsert(Map<Id,SCMC__Receiving_Inspection__c> newInspectionsmap) {
    

        // Create a list of inspections to pass to the API
        List<SCMC__Receiving_Inspection__c> inspections = new List<SCMC__Receiving_Inspection__c>();
    List<SCMC__Receiving_Inspection__c> newinspections = [SELECT Id, Name, SCMC__Inspection_Type__c,SCMC__Condition_Code__c,
                                                          SCMC__Quantity_to_Accept__c,SCMC__Total_Quantity_Accepted__c,SCMC__Received_Quantity__c
                                                         FROM SCMC__Receiving_Inspection__c where ID IN:newinspectionsmap.keySet() ];
        
        // Iterate over the new records
        for (SCMC__Receiving_Inspection__c inspection : newInspections) {
            // Only perform the update and API call if the inspection type is "Customer_Return"
            if (inspection.SCMC__Inspection_Type__c == 'Customer_Return') {
                // Set the "SCMC__Condition_Code__c" field to null
                inspection.SCMC__Condition_Code__c = null;
                system.debug('Inside For Loop');
                // Set the "SCMC__Quantity_to_Accept__c" field to the "SCMC__Received_Quantity__c" field
                inspection.SCMC__Quantity_to_Accept__c = inspection.SCMC__Received_Quantity__c;
                //inspection.SCMC__Total_Quantity_Accepted__c = inspection.SCMC__Received_Quantity__c;
                
                // Add the inspection to the list to pass to the API
                inspections.add(inspection);
            }
        }
    
    
    system.debug('Inspection values--->' + inspections);
        
        // Call the API with the list of inspections and the parameter "123"
     /*   if (!inspections.isEmpty()) {
            
            SCMC.InventoryAPI api = new SCMC.InventoryAPI();
            
            api.inspect(inspections, '123');
        } */
    return inspections;
    }

}

Below is the trigger

trigger diab_SetQuantityAndCallInventoryAPI on SCMC__Receiving_Inspection__c (before update,after update) {
    
     List<SCMC__Receiving_Inspection__c> ReceivedInspections =  diab_SetQuantityAndCallAPI.handleAfterInsert(Trigger.newmap);
    if (!ReceivedInspections.isEmpty()) {
            
            SCMC.InventoryAPI api = new SCMC.InventoryAPI();
            
           // api.inspect(ReceivedInspections, '123');
    }
    
}

1 Answer 1

1

First of all, you have several problems.

In your trigger, you have both before update and after update trigger events.

You should have one of them or perform a check for which trigger event you want to execute the logic.

Second, method handleAfterInsert is called two times.

The first time for before update event. In this case nothing happens since you are expected to update records from trigger.new or trigger.newMap but instead you query records from the database which are not trigger variables thus no update should happen.

The second tim for after update event. In this case it may make sense to query and update, but in such case you need DML operation update to save updated records to the database.

Since you are just trying to update the same records coming from trigger, you shouldn't update them in after update trigger event since such DML operation will recursively invoke the trigger again and your transaction will fail because of recursive trigger invocation limit.

Also it is a best practice to follow Trigger Handler pattern which you are not using right now.

To make the smallest changes to make it work you can do the following.

  1. Remove the after update trigger event from trigger.

    trigger diab_SetQuantityAndCallInventoryAPI on SCMC__Receiving_Inspection__c (before update) {
    
         List<SCMC__Receiving_Inspection__c> ReceivedInspections =  diab_SetQuantityAndCallAPI.handleAfterInsert(Trigger.newmap);
        if (!ReceivedInspections.isEmpty()) {
    
                SCMC.InventoryAPI api = new SCMC.InventoryAPI();
    
               // api.inspect(ReceivedInspections, '123');
        }
    
    }
    
  2. Rename handleAfterInsert to something meaningful, like populateFields. Also, remove unused and commented code

     public class diab_SetQuantityAndCallAPI {
    
         public static void populateFields(Map<Id,SCMC__Receiving_Inspection__c> newInspectionsmap) {
             // Iterate over the new records
             for (SCMC__Receiving_Inspection__c inspection : newinspectionsmap.values()) {
                 // Only perform the update and API call if the inspection type is "Customer_Return"
                 if (inspection.SCMC__Inspection_Type__c == 'Customer_Return') {
                     // Set the "SCMC__Condition_Code__c" field to null
                     inspection.SCMC__Condition_Code__c = null;
                     system.debug('Inside For Loop');
                     // Set the "SCMC__Quantity_to_Accept__c" field to the "SCMC__Received_Quantity__c" field
                     inspection.SCMC__Quantity_to_Accept__c = inspection.SCMC__Received_Quantity__c;
                     //inspection.SCMC__Total_Quantity_Accepted__c = inspection.SCMC__Received_Quantity__c;
    
    
                 }
             }
     }
    
  3. Again, update the trigger to correspond with service class changes:

    trigger diab_SetQuantityAndCallInventoryAPI on SCMC__Receiving_Inspection__c (before update) {

      diab_SetQuantityAndCallAPI.populateFields(Trigger.newmap);
    

    }

Hope this helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .