+ Start a Discussion
Jason Kuzmak 12Jason Kuzmak 12 

Apex lookup reference fields still showing null even after being queried

I am working with fields in Salesforce CPQ, though I figure all of the custom objects contained in it should respond to Apex as any others would. For some reason though, every time I try to reference a field on a product referenced by one of the custom objects, all fields related to that product show as null. I'm probably making some really obvious mistake. Please take a look:

public without sharing class QuoteLineHandler {
	public static void HandleBeforeDelete(){
		// Removes the Product type of all "Mosca Model" quote line items from the "Product Names" field on quote level

List<SBQQ__QuoteLine__c> quoteLineList = new List<SBQQ__QuoteLine__c>();
		List<Id> productIds = new List<Id>();
		// Add all relevant lines to list for further processing
		for(SBQQ__QuoteLine__c delQuoteLine : (List<SBQQ__QuoteLine__c>)Trigger.old){
			quoteLineList.add(delQuoteLine);
			productIds.add(delQuoteLine.SBQQ__Product__c);
		}

		List<Product2> productList = [Select Id,Name,Product_Type__c
										From Product2
									   Where Id In :productIds];		
		System.debug('productList query size is ' +productList.size()+ ' and product list = ' +productList);				   			   

		for(SBQQ__QuoteLine__c ql : quoteLineList){
			System.debug('checking if quoteline product name is null. Cover thy bases lest ye should receive a null-pointer exception.');
			System.debug('ql.SBQQ__Product__c = '+ql.SBQQ__Product__c+ ' AKA ' + ql.SBQQ__Product__r.Name);
			System.debug('ql.Id = ' +ql.Id);
			if(ql.SBQQ__Product__r.Name != null){
                System.debug('This code is never reached. Product name is always null.");
            }
        }
    }
}
As the last debug says, my null check never passes. My product's name is always null in spite of the fact that it shows in my query as having a name. Help would be greatly appreciated. 
Best Answer chosen by Jason Kuzmak 12
bob_buzzardbob_buzzard
The issue here is that you are expecting the trigger to have populated the entire object graph - i.e. not just the SBQQ__QuoteLine__c but all records that is looks up to etc. This isn't the case. The trigger will pass you fully built out SBQQ__QuoteLine__c records, but only the id of relationships will be populated, not the related object itself. If you need this, you have to query it back from the database yourself.

All Answers

bob_buzzardbob_buzzard
The issue here is that you are expecting the trigger to have populated the entire object graph - i.e. not just the SBQQ__QuoteLine__c but all records that is looks up to etc. This isn't the case. The trigger will pass you fully built out SBQQ__QuoteLine__c records, but only the id of relationships will be populated, not the related object itself. If you need this, you have to query it back from the database yourself.
This was selected as the best answer
Jason Kuzmak 12Jason Kuzmak 12
@bob_buzzard

Wouldn't that be what the Product2 SOQL Query does for me? I neglected to mention that the SBQQ__Product__c is a lookup to this object, but I reckon that querying the product object as shown would get me the fields I'm trying to reference. I'm not sure why it doesn't.
Jason Kuzmak 12Jason Kuzmak 12

I figured out what you meant, though I can't delete the question now so I'll just go over it. Even when queried, the system still can't recognize how the queried information relates to the objects I'm trying to reference, so it's up to me to either map them together, or to only work with the data that I have just queried.