1

I'm updating a method on Product that creates a related object record and then updates the product record. The functionality only works on edit in my sandbox. In production where the original code is runs on insert and edit.

I think I've narrowed the issue down to the criteria that sends a map to run through the creation logic. For the life of me I can't figure out why it does not run on the record insertion. Comparing the two sets of code, can you help to see why the second does not fire for the record insert?

Production:

 for(Product2 objProduct2: newMap.values())
{
  Product2 objOldProduct2 = new Product2();
  if(Trigger.isUpdate)
    {
    objOldProduct2 = oldMap.get(objProduct2.Id);
    }
  if(objProduct2.Product_Identifier__c != null && 
    ((Trigger.isUpdate || objProduct2.isActive) ||
    (Trigger.IsUpdate  && (objProduct2.Family != objOldProduct2.Family || 
    objProduct2.Product_Group__c != objOldProduct2.Product_Group__c  || 
    objProduct2.Product_Offering__c != objOldProduct2.Product_Offering__c ||
    objProduct2.Product_Identifier__c != objOldProduct2.Product_Identifier__c || 
    objProduct2.ALR_Retailer__c != objOldProduct2.ALR_Retailer__c ||
    objProduct2.Product_LOB__c != objOldProduct2.Product_LOB__c ||
    objProduct2.isActive != objOldProduct2.isActive))
    )
  )
  {          

Sandbox:

for(Product2 objProduct2: newMap.values())
 {
  Product2 objOldProduct2 = new Product2();
  if(Trigger.isUpdate)
   {
    objOldProduct2 = oldMap.get(objProduct2.Id);
   }            
  if( 
   objProduct2.Product_Identifier__c != null &&
    (
     objProduct2.IsActive ||
     objProduct2.isActive != objOldProduct2.isActive
    )
  {
  // add to map;
  }
 // do stuff

This is an After Insert method:

public override void afterInsert(Map<Id, SObject> newMapGeneric){
    Map<Id, Product2> newMap = (Map<Id, Product2>) newMapGeneric;
    /*AFTER INSERT METHODS START*/
    if(!isRecursive)
    {
        isRecursive = true;
        manageProductBundle(newMap, new Map<Id, Product2>());
        updatePA(newMap);
    }
    /*AFTER INSERT METHODS END*/
  • Is this trigger before insert or after insert? It seems like you should get a null pointer from any reference to oldObjProduct2.Any_Field__c during insert triggers of any kind. – Adrian Larson Jan 13 '17 at 21:10
  • It's after insert, I'll update. – Dan Wooding Jan 13 '17 at 21:17
2

Here's what I think my be the source of your issue based on what you've shared:

In your production trigger that's working, you have the following condition that you test for:

if(objProduct2.Product_Identifier__c != null && ...
(Trigger.isUpdate || objProduct2.isActive) ... ||
objProduct2.isActive != objOldProduct2.isActive))

That line seems have gotten translated for your new code in sandbox to:

if(objProduct2.Product_Identifier__c != null &&
  (objProduct2.IsActive ||
   objProduct2.isActive != objOldProduct2.isActive)

These two are not equivalent to one another. It seems that on an afterInsert, you'd only need to know that objProduct2 isActive. The last || condition comparison is more like what I'd expect to see in an AfterUpdate conditional test.

  • Thanks, I figured it out, I had a before insert method to populate the Identifier field. it had the isRecursive check witch I think was not allowing the code to go to the next method. Once I removed that it worked. – Dan Wooding Jan 15 '17 at 17:18

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.