salesforceapexsoql

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


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);  

Solution

  • 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);**
        --------------------------------------
        }