2

I have object customObjectA__c which has a lookup fields - lookupfield1__c

I have an after insert trigger which, at some point, checks if that field is populated. The whole issue is, it's reading that field as null, which is wrong, as i can see on the record that field is populated.

Here is the best part. Same code works in my dev environment. But when it is deployed to QA, that field is seen as null. So i am guessing there is some sort of FLS or permset issue, rather than code. Can someone give ideas under which circumstances can this happen (where a lookup field is read as null by trigger, even if it is populated)

A general structure of my code -

trigger DemoTrigger on customObjectA__c (after insert){
   //bunch of code
   helperClass.dummyMethod(trigger.new);
}

----- helper class ---

public class helperClass{
   public static void dummyMethod(List<customObjectA__c> allRecords){
       for(customObjectA__c record : allRecords){
           if(record.lookupField1__c != null){
               //do something
           }
       }
   }
}

helper class does not have "with sharing". Plus, trigger is run in context of the user that owns the record (so he has full access).

I am going bonkers why it would run in one environment, but not in another.

9
  • 1
    This is troubleshooting 101 that only you can do. Does it work when you change it to 'without sharing'? What about before trigger? any workflows or process builder running? just throw some debug everywhere and see what happens – Nick Cook Oct 15 '16 at 1:25
  • Did you check FLS for this lookup in QA? – Ashwani Oct 15 '16 at 5:40
  • I believe you've oversimplified your code. Lookup fields are indeed populated in a trigger, both before and after events. What often trips people up, however, are the fact that lookup relationships are null! If you're using __r anywhere, those values aren't populated until you query for them, usually by way of a map. – sfdcfox Oct 15 '16 at 8:35
  • @NickCook - Added debugs, thats how i know that code comes to that if statement in helper class, and evaluates that lookupField1__c = null (which is not true) – user2957592 Oct 15 '16 at 19:27
  • @Ashwani - Yes, i checked FLS, the profile (of the user that runs the trigger) has read/edit access to the field. – user2957592 Oct 15 '16 at 19:29
0

Both the codes should have to work. Please check all the permissions- OWD, FLS etc for the Object the Look up field of that object for the User who use going to use the record.

2
  • I agree, both codes should work, but neither one does. Here's the kicker. i just moved the same code to "Before insert", and it's reading those lookup fields just fine (so seems like no issue of permset/OWD, etc). But in "After Insert" , it doesn't. Can't blame code either, because same code works fine in dev org. I am totally lost as to what's going on. – user2957592 Oct 18 '16 at 0:02
  • Is there any workflow or other operations performing after Insert? If Yes, then it may be deleting the value of the Look Up field or doing any operation with the Look Up field, which can cause to NULL value of the Look Up field. – Amit Agarwalla Oct 19 '16 at 12:59
-2
public class helperClass{
    public static void dummyMethod(List<customObjectA__c> allRecords){
        Set<Id> idSet = new Set<Id>();
        for(customObjectA__c record : allRecords){
            idSet.add(record.Id);
        }
        List<customObjectA__c> customObjectAList = [];
        for(customObjectA__c custObj : [SELECT Id, lookupField1__c FROM customObjectA__c WHERE Id IN : idSet]){
            if(record.lookupField1__c != null){
                //do something
            }         
        }
    }
}
1
  • - that code doesn't work either. It is still reading those lookup fields as null. And i may point, we don't need that query. As i said, my code works fine in my dev environment. The exact same code doesnt work in QA environment. – user2957592 Oct 15 '16 at 19:32

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.