i have a requirement to provide distance between two objects with latitude and longitude.

But i am getting null as per debug logs where i missed it.

Apex Class:

for (Opportunity opp:[Select Id,Name,StageName,Account.name,Number_of_Units__c,account.SIC_Code__c,Type,
                         account.No_of_active_units__c,account.BillingLatitude,account.BillingLongitude from opportunity where account.SIC_Code__c 
                         LIKE :codes and StageName='Closed Won' ORDER BY account.No_of_active_units__c Desc NULLS LAST Limit 10])
                    if(opp.account.SIC_Code__c <> null)
                        Location loc1 = Location.newInstance(Latitudes,Longtitudes);
                        System.debug('Data in Leadlatitude'+Latitudes);
                        System.debug('Data in Leadlongitude'+Longtitudes);
                        System.debug('Data in:'+loc1);
                        Location loc2 = Location.newInstance(opp.account.BillingLatitude,opp.account.BillingLongitude);
                        System.debug('Data in opplat'+opp.account.BillingLatitude);
                        System.debug('data in opplong'+opp.account.BillingLongitude);
                        System.debug('Data in loc2:'+loc2);
                        Total = Location.getDistance(loc1, loc2, 'mi');
                        System.debug('Data in total:'+Total);
                        MyWrapper wrapped= new MyWrapper(opp,Total);


As per debug logs:

10:05:16.0 (42796530)|USER_DEBUG|[75]|DEBUG|Data in Leadlatitudenull
10:05:16.0 (42830078)|USER_DEBUG|[76]|DEBUG|Data in Leadlongitudenull
10:05:16.0 (42985462)|USER_DEBUG|[77]|DEBUG|Data in:System.Location[getLatitude=null;getLongitude=null;]
10:05:16.0 (43058781)|USER_DEBUG|[79]|DEBUG|Data in opplatnull
10:05:16.0 (43097005)|USER_DEBUG|[80]|DEBUG|data in opplongnull
10:05:16.0 (43142854)|USER_DEBUG|[81]|DEBUG|Data in loc2:System.Location[getLatitude=null;getLongitude=null;]
10:05:16.0 (43227583)|USER_DEBUG|[83]|DEBUG|Data in total:null
10:05:16.0 (43310090)|USER_DEBUG|[84]|DEBUG|Opportunity:Opportunity:{Id=006N000000B3WeUIAV, Name=Sample-, StageName=Closed Won, AccountId=001N000001GaODGIA3, Number_of_Units__c=23, Type=New Business, Billing_City__c=DOTHAN, Billing_State_Province__c=AL}
10:05:16.0 (43750677)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

But in above account record have all the details:

enter image description here


Automatically populating Longitude and Latitude data for the standard Billing/Shipping Address fields on Account was introduced in Summer '16, but it appears that it needs to be manually activated.

The most logical explanation for why your Location records (Salesforce seems to recommend using System.Location to avoid confusion with the standard SObject Location [which they suggest using Schema.Location for]) are null is that the longitude/latitude of the address field on your Accounts are null. Needing to activate the auto-population fits with that.

It took a bit of digging, but it appears that instead of using the quick find box in the setup menu to search for "clean rule", it's since been moved, and you need to quick find "Data Integeration Rules".

Setup -> Administration Setup -> Data Management -> Data Integration Rules.

In there, you should find 4 Geocode data integration rules "Geocodes for Account Billing Address", "Geocodes for Account Shipping Address", Geocodes for Contact Mailing Address", and "Geocodes for Lead Address".

Naturally, you'll want to drill into the Account Billing Address one. From there, it looks like the default status is 'Inactive'. You'll need to activate the rule (click the "Activate" button), and wait for a while for Salesforce to populate that data for you.

Afterwords, your snippet should start working as expected.

| improve this answer | |

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.