1

I have a Trigger and Trigger Helper.

  • Opportunity is a parent for CompanyOrder__c.
  • One Opportunity can have many CompanyOrder__c records with uncrossed Start- and End Dates.
  • CompanyOrder__c is a parent for Supply__c.
  • Each CompanyOrder__c can haveonly 1 Supply__c.
  • If we have 1 Opportunity with CompanyOrder1 and CompanyOrder2 where CompanyOrder1.EndDate__c == CompanyOrder2.StartDate__c, the Supply__c record of CompanyOrder1 should get a Tools__c value from CompanyOrder2.OrdersTools__c.

Everything works, but not always. When I get many records, Supplies can be with wrong Tools__c values.

Trigger:

if(Trigger.isAfter && Trigger.isUpdate) {
    CleaningsTriggerHelper.updateSupplies(Trigger.new);  
}

Helper:

public static void updateSupplies(List<CompanyOrder__c> createdCompanyOrders){
    Map<Id, Supply__c> mapSuppliesToConnect = new Map<Id, Supply__c>();
    Map<Id, Supply__c> mapSuppliesToUpdate = new Map<Id, Supply__c>();
    List<Id> oppIds = new List<Id>();
    List<Date> compOStartDate = new List<Date>();

    for(CompanyOrder__c compOToCheck : createdCompanyOrders){
        oppIds.add(compOToCheck.Opportunity__c);
        if(compOToCheck.StartDate__c != null){ 
            compOStartDate.add(Date.newinstance(compOToCheck.StartDate__c.year(), compOToCheck.StartDate__c.month(), compOToCheck.StartDate__c.day()));
        }
    }

    List<Supply__c> actualSupplies = [SELECT EndDate__c, CompanyOrder__r.Opportunity__c, Tools__c FROM Supply__c
    WHERE CompanyOrder__r.Opportunity__c IN : oppIds AND DAY_ONLY(EndDate__c) IN : compOStartDate
    AND DAY_ONLY(EndDate__c) >= : Date.TODAY()];

    if(actualSupplies.size() > 0){
        for(Supply__c sup : actualSupplies){
            mapSuppliesToConnect.put(sup.CompanyOrder__r.Opportunity__c, sup);
        }
    }

    if(!mapSuppliesToConnect.isEmpty()){
        for(CompanyOrder__c compO : createdCompanyOrders){
            Id opportunityId = compO.Opportunity__c;
            Supply__c supplyForUpd = mapSuppliesToConnect.get(opportunityId);

            if(supplyForUpd != null){
                if(compO.OrdersTools__c > 0){
                    mapSuppliesToConnect.get(opportunityId).Tools__c = String.valueOf(compO.OrdersTools__c);
                } else{
                    mapSuppliesToConnect.get(opportunityId).Tools__c = '';
                }
                mapSuppliesToUpdate.put(supplyForUpd.Id, supplyForUpd);
            }
        }
    }

    if(mapSuppliesToUpdate.size() > 0){
        update mapSuppliesToUpdate.values();
    }
}

Updated: I found the problem. I'm getting wrong values because of the updating only the last one element in the map. How can I fix it?

  • Try to debug the code. – Sarvesh Sep 19 '19 at 12:57
  • Updated: I found the problem. I'm getting wrong values because of the updating only the last one element in the map. – MichaelLev19 Sep 24 '19 at 9:23
2

I don't see anyplace in your code where you're validating that the Dates of all related CompanyOrder__c have StartDates and EndDates that do not overlap with one another. That would require a query for all CompanyOrder__c's related to the parent Opportunity, then iterating through the StartDates and EndDates since not all instances of them will necessarily be included in this instance of the trigger execution. This could easily account for the discrepancies you're seeing.

| improve this answer | |
  • Thank you for the comment. These dates never overlap. It's a rule. – MichaelLev19 Sep 23 '19 at 13:34
  • Updated: I found the problem. I'm getting wrong values because of the updating only the last one element in the map. – MichaelLev19 Sep 24 '19 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.