0

My Lookup's(MD) related lookup field is coming up as null in testclass. So I have 3 objects, lets Objects X, Y and Z
Z is the detail and Y is the Master.
Y has a lookup to X

In the test class I am inserting all the 3 records. I have a trigger on 'Z', which collects the object X id's to perform validations, I add them in a set:

Set<Id> productIdSet = new Set<Id>();
 for(ObjectZ z : zList from trigger){           
     productIdSet.add(z.Y__r.X__c);
     system.debug('>>>'+z.Y__r.X__c);
 }

This runs perfectly fine when run in actual context, but when executed via testclass 'z.Y__r.X__c' , always returns null. I have checked the field and object level permissions, they are fine. I do not want to add conditions for runningTest context, query and populate the set, just to bypass the issue. Kindly help. Thank you.

1 Answer 1

2

Without seeing your code it's difficult to say what exactly is wrong, but my best guess is that you don't fill in X__c field on your Y__c object. Make sure it's filled in.

Another possibility would be that you can't access related record fields directly(i.e. yourRecord.Lookup__r.some_Field__c) without querying these fields first, but you say that it works fine in an actual context, so I doubt the issue is there

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .