0

I have written at shipment insertion to update the shipment's expedited checkbox to be true, and then in the after trigger, it should update all the packages that have the newly created shipment to update the packer. However it is only setting the expedited checkbox and not updating the packer in the packages at all. When I debug it is not returning my query at all:

 UtilitiesDTS.getPackagesForExpShipments=
 [SELECT ID, Packer__c FROM Package__c WHERE shipment__c IN : shipIds];

Here are my methods:

public static void checkShipmentExpeditedBox(List<Shipment__c> newShips, Boolean isAfterTrigger){
    Map<Id, Shipment__c> expShipments = new Map<Id, Shipment__c>();
    for(Shipment__c ship : newShips){
        if(ship.Due_Date__c != null && ship.Due_Date__c > Date.today() - 3 ){
            if(!isAfterTrigger){
                ship.Expedited__c = True;
            }
            expShipments.put(ship.id, ship);
        }
    }
    if(expShipments.size() > 0 && isAfterTrigger){
        Packages.updatePackerExpShips(expShipments);
    }
}

}

public static void updatePackerExpShips(Map<Id, shipment__C> shipMap){
    List<package__c> packsToUpdate = new List<package__c>();
    packsToUpdate = UtilitiesDTS.getPackagesForExpShipments(shipMap.keySet());
    for(Package__c pack : packsToUpdate){
        pack.packer__c = shipsMap.get(pack.Shipment__c).source_Id__c;
    }
    if(packsToUpdate.size()>0){
        update packsToUpdate;
    }
}      

May I have some assistance as to why the query is returning null and therefore not allowing the package's packer to be update in my second method?

  • I'm confused. The first version of the query that you posted was incorrect. Are you saying that with the correct version shown here the query returns no results? What trigger event is this running in? Are you certain that there are matching packages? – David Reed Nov 22 '18 at 0:05
  • I mean I had written the query wrong and in my method its written with shipment__c then i updated it in the post. I believe there are matching packages because i see the lookup being populated – j.koh Nov 22 '18 at 0:08
  • Can you clarify: is this code running in a before insert or after insert trigger on your Shipment object? – David Reed Nov 26 '18 at 19:38
1

It's returning nothing because you're querying for Package__c records whose Id are in a collection of Shipment__c Ids.

packsToUpdate = UtilitiesDTS.getPackagesForExpShipments(shipMap.keySet());

shipMap.keySet() is a collection of Shipment__c Ids. When you run the query

[SELECT ID, Packer__c FROM Package__c WHERE Id IN : shipIds];

You should be querying not the Id field but the lookup field from Package__c to Shipment__c, which is also called Shipment__c:

[SELECT ID, Packer__c FROM Package__c WHERE Shipment__c IN : shipIds];
  • My apologies the original,query did have shipment__c not the ID – j.koh Nov 21 '18 at 23:59

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.