• Zach Alexander
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hello Everyone,

We have a trigger that works... but I'm concerned it is only by accident and may break in the future. Basically, the trigger utilizes the old value of a formula field called Status Code on an object called Vehicle Inventory; this trigger records the old value of Status Code and the new value of Status Code in a child object called Journal Entries.

My problem is that... well... it seems the order of execution should leave no "old" value of the formula field.

The reason for suspecting this might be a problem is because the Status Code formula is linked cross object to a junction object called Lease Tool... but the trigger itself is on Vehicle Inventory.

User-added image

The data flow starts on an unrleated object called "Lease", which contains a picklist field that tracks the Status of a vehicle (Leased, Repair, etc.). Any changes to this Status picklist field are transported down to a junction object called "Lease Tool" via trigger. At this point, the junction object has several filtered roll-up summaries linked up with Vehicle Inventory. Finally, the formula field Status Code (on Vehicle Inventory) reads the rollup-summary results to produce a value.

Lease --> (Trigger) --> Lease Tool --> (Rollup Summary) --> Vehicle Inventory --> (Trigger in Question) 

The final step is that our trigger on Vehicle Inventory records the "old" and "new" values of the formula field by creating a child object.

I'm amazed that this trigger works! So, for example, if the Picklist status on Lease previously read "Available", and later read "Leased", then the end result is a Journal Entry reading the following:

Previous Status: Availalbe
New Status: Leased
Time of Change: System Time

Does this seem correct? My understanding is that formulas are created at the time of query (when a record is accessed). How is it maintaining an "old" value all the way through this chain of events?

Is it a fluke? Or is this actual Salesforce functionality that we can trust to work in the future? There is an unrelated trigger on the origin object (Lease) that causes an Edit/Save on vehicle inventory at the exact same time the changes to the original Status picklist have been made. So that explains the DML operation on Vehicle Inventory. But... it just doesn't seem that the old value of the formula should survive.

Am I missing something obvious?
Hello Everyone,

Primary Question: Can someone pinpoint what we are doing wrong in our Trigger below?

Background: The purpose of this trigger is to "break up" large single payments (Fleet Lease Pay) made by business clients and create parallel records showing the payments per vehicle (Fleet Truck Pay). Each "Fleet Truck Pay" record needs to be linked to to both the original "Fleet Lease Pay" and another custom object called "Vehicle Inventory". This is important because we track payments made per vehicle on the object called "Vehicle Inventory".

Problem: Our trigger works for manual entries of new "Fleet Lease Pay" records. However, when using the dataoloader (with batch size greater than 1) the trigger explodes and creates thousands upon thousands of records matching "Fleet Trucks" that aren't part of the parent "Fleet Lease". We have not been able to figure out a rhyme or reason to this explosion. We figure the data is getting blended together like the ingredients in a body-builder's smoothie. But we're not sure how.

Does anybody know why the information is getting blended up?

Fleet Lease Pay   =   Fleet_Lease_Payment__c
Fleet Lease   =   Fleet_Lease_ID__c or Fleet_Lease__c
Fleet Truck   =   Fleet_Truck__c
Fleet Truck Pay   =   Fleet_Vehicle_Payment__c
Vehicle Inventory   =   VIN_Vehicle_Inventory__c or Vehicle_Inventory__c

Relationship Diagram

Trigger Code:

trigger FleetLeasePaymentDivider on Fleet_Lease_Payment__c (after insert) {
    Set<Id> ids = Trigger.newMap.keySet();
    
    //Create all the lists needed for this process.
    List<Fleet_Truck__c> fleetVehicles = new List<Fleet_Truck__c>();
    List<String> leaseId = new List<String>();
    List<FLeet_Lease_Payment__c> fleetLeasePayments = new List<Fleet_Lease_Payment__c>();
    List<Fleet_Vehicle_Payment__c> fleetVehiclePayments = new List<Fleet_Vehicle_Payment__c>();
    List<Fleet_Truck__c> vehicles = new List<Fleet_Truck__c>();
    
    //Get Data from the "Fleet Lease Pay" Object.
    //Obtain the "Fleet Lease" Parent ID
    for (Fleet_Lease_Payment__c pmt : [SELECT Id, Fleet_Lease_ID__c, Lease_Amount__c, Date_Paid__c FROM Fleet_Lease_Payment__c WHERE Id IN :Trigger.new])
    {
        //Populate List of Fleet Lease Payments in Upload
        fleetLeasePayments.add(pmt);

        //Populate List of Parent IDs
        leaseId.add(pmt.Fleet_Lease_ID__c);
    }
    
    //Get List of all the Fleet Trucks attached to Parent
    for (Fleet_Truck__c fleetUnit : [SELECT Id, Fleet_Lease__c, VIN_Lookup__c FROM Fleet_Truck__c WHERE Fleet_Lease__c IN :leaseId])
    {
         //Populate List of Fleet Trucks attached to parent
         fleetVehicles.add(fleetUnit);
    }
    
    //Start final phase of trigger by cycling through each Fleet Lease Payment
    for (FLeet_Lease_Payment__c FLPmt : fleetLeasePayments)
    {
        //Make Parent ID easy to reference
        String fleetLeaseId = FLPmt.Fleet_Lease_ID__c;
        
        //Summon back up the list of all Fleeet Trucks attached to Parent
        for (Fleet_Truck__c fleetTruck : fleetVehicles)
        {
            //Check to see if the the Fleet Lease Pay and Fleet Truck records belong to the same parent
            if (fleetTruck.FLeet_Lease__c == fleetLeaseId)
            {
                //If so, Populate a second list of trucks, that is specific to each Fleet Lease Pay record
                vehicles.add(fleetTruck);
            }
        }
        //Get the second list of trucks we just made
        for (Fleet_Truck__c truck : vehicles)
        {
            //Create Fleet Truck Payment Records from that data
            Decimal truckPmt = FLPmt.Lease_Amount__c / vehicles.size();
            Fleet_Vehicle_Payment__c fleetPmt = new Fleet_Vehicle_Payment__c(Date_Paid__c = FLPmt.Date_Paid__c,
                                                                             Fleet_Lease_Payment__c = FLPmt.Id,
                                                                             VIN_Vehicle_Inventory__c = truck.VIN_Lookup__c,
                                                                             Fleet_Vehicle__c = truck.Id,
                                                                             Lease_Amount__c = truckPmt);
            //Populate the Fleet Truck Payments we want to Upload
            fleetVehiclePayments.add(fleetPmt);
        }
    }
    
    //Check to see if there is anything in the list.
    if (!fleetVehiclePayments.isEmpty())
    {
        //If so, insert the list.
        insert fleetVehiclePayments;
    }
}
Hello Everyone,

Primary Question: Can someone pinpoint what we are doing wrong in our Trigger below?

Background: The purpose of this trigger is to "break up" large single payments (Fleet Lease Pay) made by business clients and create parallel records showing the payments per vehicle (Fleet Truck Pay). Each "Fleet Truck Pay" record needs to be linked to to both the original "Fleet Lease Pay" and another custom object called "Vehicle Inventory". This is important because we track payments made per vehicle on the object called "Vehicle Inventory".

Problem: Our trigger works for manual entries of new "Fleet Lease Pay" records. However, when using the dataoloader (with batch size greater than 1) the trigger explodes and creates thousands upon thousands of records matching "Fleet Trucks" that aren't part of the parent "Fleet Lease". We have not been able to figure out a rhyme or reason to this explosion. We figure the data is getting blended together like the ingredients in a body-builder's smoothie. But we're not sure how.

Does anybody know why the information is getting blended up?

Fleet Lease Pay   =   Fleet_Lease_Payment__c
Fleet Lease   =   Fleet_Lease_ID__c or Fleet_Lease__c
Fleet Truck   =   Fleet_Truck__c
Fleet Truck Pay   =   Fleet_Vehicle_Payment__c
Vehicle Inventory   =   VIN_Vehicle_Inventory__c or Vehicle_Inventory__c

Relationship Diagram

Trigger Code:

trigger FleetLeasePaymentDivider on Fleet_Lease_Payment__c (after insert) {
    Set<Id> ids = Trigger.newMap.keySet();
    
    //Create all the lists needed for this process.
    List<Fleet_Truck__c> fleetVehicles = new List<Fleet_Truck__c>();
    List<String> leaseId = new List<String>();
    List<FLeet_Lease_Payment__c> fleetLeasePayments = new List<Fleet_Lease_Payment__c>();
    List<Fleet_Vehicle_Payment__c> fleetVehiclePayments = new List<Fleet_Vehicle_Payment__c>();
    List<Fleet_Truck__c> vehicles = new List<Fleet_Truck__c>();
    
    //Get Data from the "Fleet Lease Pay" Object.
    //Obtain the "Fleet Lease" Parent ID
    for (Fleet_Lease_Payment__c pmt : [SELECT Id, Fleet_Lease_ID__c, Lease_Amount__c, Date_Paid__c FROM Fleet_Lease_Payment__c WHERE Id IN :Trigger.new])
    {
        //Populate List of Fleet Lease Payments in Upload
        fleetLeasePayments.add(pmt);

        //Populate List of Parent IDs
        leaseId.add(pmt.Fleet_Lease_ID__c);
    }
    
    //Get List of all the Fleet Trucks attached to Parent
    for (Fleet_Truck__c fleetUnit : [SELECT Id, Fleet_Lease__c, VIN_Lookup__c FROM Fleet_Truck__c WHERE Fleet_Lease__c IN :leaseId])
    {
         //Populate List of Fleet Trucks attached to parent
         fleetVehicles.add(fleetUnit);
    }
    
    //Start final phase of trigger by cycling through each Fleet Lease Payment
    for (FLeet_Lease_Payment__c FLPmt : fleetLeasePayments)
    {
        //Make Parent ID easy to reference
        String fleetLeaseId = FLPmt.Fleet_Lease_ID__c;
        
        //Summon back up the list of all Fleeet Trucks attached to Parent
        for (Fleet_Truck__c fleetTruck : fleetVehicles)
        {
            //Check to see if the the Fleet Lease Pay and Fleet Truck records belong to the same parent
            if (fleetTruck.FLeet_Lease__c == fleetLeaseId)
            {
                //If so, Populate a second list of trucks, that is specific to each Fleet Lease Pay record
                vehicles.add(fleetTruck);
            }
        }
        //Get the second list of trucks we just made
        for (Fleet_Truck__c truck : vehicles)
        {
            //Create Fleet Truck Payment Records from that data
            Decimal truckPmt = FLPmt.Lease_Amount__c / vehicles.size();
            Fleet_Vehicle_Payment__c fleetPmt = new Fleet_Vehicle_Payment__c(Date_Paid__c = FLPmt.Date_Paid__c,
                                                                             Fleet_Lease_Payment__c = FLPmt.Id,
                                                                             VIN_Vehicle_Inventory__c = truck.VIN_Lookup__c,
                                                                             Fleet_Vehicle__c = truck.Id,
                                                                             Lease_Amount__c = truckPmt);
            //Populate the Fleet Truck Payments we want to Upload
            fleetVehiclePayments.add(fleetPmt);
        }
    }
    
    //Check to see if there is anything in the list.
    if (!fleetVehiclePayments.isEmpty())
    {
        //If so, insert the list.
        insert fleetVehiclePayments;
    }
}