SOQL gives (null <object__r>) output for the lookup type field value in salesforce?

142 Views Asked by At

I have two objects, Sdf__c and SdfBudget__c. Sdf__c has a lookup field related to the SdfBuget object. I want to fetch values from sdf__c along with budget fields from sdfBudget that match SdfBudget vbu names.vbu is a formula field (text).

But i return (null <object__r>) for the field. How to get the value instead of null.

The field has values but its not returning.

suppose the table has the following columns: Sdf__c

vbu__c Name
Abc_1 John
Abc_2 Abrahm
Abc_3 Jake
Abc_3 Ozler

SdfBudget__c

vbu__c Budget
Abc_1 1000
Abc_2 2000
Abc_3 3000

and i need to get the output as follows

Name vbu__c Budget
John Abc_1 1000
Abrahm Abc_2 2000
Jake Abc_3 3000
Ozler Abc_3 3000

Query which i have trid

    List<SdfBudget__c> sdfVbus=new List<SdfBudget__c>();
    sdfVbus=[SELECT budget__c,VBU__c FROM SdfBudget__c WHERE VBU__C != null];
    List<Sdf__c> sdfItems=new List<Sdf__c>();
    for(SdfBudget__c ob:sdfVbus)
    {
        vbuValues.add(ob.VBU__c);
    }
   sdfItems=[SELECT Name,VBU__c,Sdf_Budget__r.Budget__c FROM sdf__c WHERE VBU__c IN :vbuValues];

Another thing is that while trying to debug it in anonymous window. the field is not being logged.

system.debug('sdfItems>>>'+sdfItems);  
2

There are 2 best solutions below

0
Jilnesh Ajay Jîllu On BEST ANSWER

This is how i got the result,

    Map<string, Decimal> sdfBudgetMap = new Map<string, Decimal>();

    sdfVbus=[SELECT budget__c,VBU__c FROM SdfBudget__c WHERE VBU__C != null];
    for(SdfBudget__c ob:sdfVbus)
    {
        sdfBudgetMap.put(ob.VBU__c,ob.budget__c);
        vbuValues.add(ob.VBU__c);
    }
 sdfItems=[SELECT Id,Name,VBU__c FROM sdf__c WHERE VBU__c IN :vbuValues ];
    for(Sdf__c item :sdfItems)
    {
    ---------------------
    -----------------------   
    model.Budget=**sdfBudgetMap.get(item.VBU__c);**
    --------------------------------------
    }
0
eyescream On

VBU__c is same on both records? and what is it, a text? a picklist? Sounds like you have a match by text field but actual lookup Sdf__c.SdfBudget__c is not populated so you're getting null.

You can get around that in apex but really this will make reporting harder... If they should be linked - make them. Maybe mark SdfBudget__c.VBU__c as unique external id and then when upserting Sdfs you can tell salesforce to find matching budget by that instead of by id.

A quick and dirty way to make it work is this but you should really consider populating the lookup

Map<String, SdfBudget__c> budgetsByVBU = new Map<String, SdfBudget__c>();
for(SdfBudget__c ob:[SELECT budget__c,VBU__c FROM SdfBudget__c WHERE VBU__C != null]){
    budgetsByVBU.put(ob.VBU__c, ob);
}

List<Sdf__c> sdfItems = [SELECT Name,VBU__c,Sdf_Budget__r.Budget__c 
FROM sdf__c 
WHERE VBU__c IN :budgetsByVBU.keyset()];

for(Sdf__c item : sdfItems){
    System.debug(item);
    if(item.Sdf_Budget__r != null){
        System.debug('got value from proper lookup: ' + item.Sdf_Budget__r.Budget__c);
    } else if(budgetsByVBU.containsKey(item.VBU__c)){
        System.debug('no lookup but match by exact vbu ' + budgetsByVBU.get(item.VBU__c));
    } else {
       System.debug('no match by vbu. Check if there\'s really a budget with that value. Maps are case sensitive so maybe somebody wrote wrong one to this sdf?');
    }
}