Thursday, February 5, 2015

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 -
@isTest
public 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, userGeoLocation.get(1));
    }
    
    static testMethod void testGetUserAddress(){
        UserLocation userLocation = new UserLocation();
        Test.startTest();
        Address userAddress = userLocation.getUserAddress('sudipta-deb@gmail.com');
        Test.stopTest();
        
        System.assertEquals('West Bengal', userAddress.State);
        System.assertEquals('Kolkata', userAddress.City);
        System.assertEquals('12345', userAddress.PostalCode);
        System.assertEquals('India', userAddress.Country);
    }
}

You will notice that the test method created above is using another new feature introduced in Spring '15 i.e. @testSetup. I have written a post on that. Please check Spring '15 - New Feature Added - @testSetup

This is really a great feature introduced. You as a developer now have huge power to do many things for example - calculating distance between two geolocations (I will write a post on this very soon).

You should read this great resource before start working with Compound fields. 

You can try this new feature in your own Spring '15 Pre-release Org! 

0 comments:

Post a Comment