Spring ’15 – New Feature Added – Access Address and Geolocation Compound Fields Using Apex
Another feature added in Sprint ’15 is the ability to access Address and Geolocation Compound Field using Apex. I will explain the new feature with example below -First I will create a new field of type Geolocation in the User object as shown below -Now I will write the below code to access Location and Address details from User as below -public class UserLocation { public List<Double> getUserGeoLocation(String userName){ User anUser = [Select Id, Location__c from User where Username = :userName LIMIT 1]; Location location = anUser.Location__c; List<Double> geoLocation = new List<Double>(); geoLocation.add(location.latitude); geoLocation.add(location.longitude); return geoLocation; } public Address getUserAddress(String userName){ User anUser = [Select Id, Address from User where Username = :userName LIMIT 1]; return anUser.Address; }}I will write the test class also to test the functionality as shown below -@isTestpublic class UserLocationTest { @testSetup static void setup() { User userToCreate = new User(); //Add required information userToCreate.FirstName = ‘Sudipta’; userToCreate.LastName = ‘Deb’; userToCreate.Alias = ‘sdeb’; userToCreate.Email = ‘sudipta.deb@gmail.com’; userToCreate.Username = ‘sudipta-deb@gmail.com’; userToCreate.CommunityNickname = ‘Sudipta.Deb’; userToCreate.ProfileId = ’00eB0000000F8dB’; userToCreate.TimeZoneSidKey = ‘America/Denver’; userToCreate.LocaleSidKey = ‘en_US’; userToCreate.EmailEncodingKey = ‘UTF-8’; userToCreate.LanguageLocaleKey = ‘en_US’; //Adding the information Geolocation userToCreate.Location__Latitude__s = 22.5667; userToCreate.Location__Longitude__s = 88.3667; //Adding the Address information userToCreate.State = ‘West Bengal’; userToCreate.City = ‘Kolkata’; userToCreate.PostalCode = ‘12345’; userToCreate.Country = ‘India’; insert userToCreate; } static testMethod void testGetUserGeoLocation(){ UserLocation userLocation = new UserLocation(); Test.startTest(); List<Double> userGeoLocation = userLocation.getUserGeoLocation(‘sudipta-deb@gmail.com’); Test.stopTest(); System.assertEquals(22.5667, userGeoLocation.get(0)); System.assertEquals(88.3667,...
Read More