Category: Spring 15

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

Spring ’15 – New Feature Added – @testSetup

One of the very interesting feature added in Spring ’15 is @testSetup. In this post I will try to explain with example more about the feature. Below is what you will get from release note about @testSetup.Now let’s check with example -Below is the class/methods for which we need to write test methods – public class AccountHelper { public List<Account> getAllAccounts(){ List<Account> allAccounts = [Select Id from Account]; return allAccounts; } public Integer getEmployeeCountFrom(String accountNumber){ Account fetchedAccount = [Select Id, NumberOfEmployees from Account where AccountNumber = :accountNumber LIMIT 1]; return fetchedAccount.NumberOfEmployees; } public List<Case> getAllCases(Id accountId){ return [select ID from Case where AccountId = :accountId]; } public void updateEmployeeCountFor(String accountNumber, Integer newEmployeeCount){ Account fetchedAccount = [Select Id, NumberOfEmployees from Account where AccountNumber = :accountNumber LIMIT 1]; fetchedAccount.NumberOfEmployees = newEmployeeCount; update fetchedAccount; }}Previously we used to write test classes like below -@isTestpublic class AccountHelperTest { static testMethod void testGetAllAccounts(){ List<Account> accounts = new List<Account>(); for(Integer i=0;i < 100;i++){ accounts.add(new Account(Name = ‘Universal Container’)); } insert accounts; Test.startTest(); AccountHelper accountHelper = new AccountHelper(); List<Account> allAccounts = accountHelper.getAllAccounts(); Test.stopTest(); System.assertEquals(100, allAccounts.size()); } static testMethod void testGetEmployeeCountFrom(){ Account anAccount = new Account(); anAccount.Name = ‘Universal Container’; anAccount.AccountNumber = ‘TEST’; anAccount.NumberOfEmployees = 100; insert anAccount; Test.startTest(); AccountHelper accountHelper = new AccountHelper(); Integer numberOfEmployees = accountHelper.getEmployeeCountFrom(‘TEST’); Test.stopTest(); System.assertEquals(100, numberOfEmployees); } static testMethod void testGetAllCases(){ //Create the account first Account anAccount = new Account(); anAccount.Name = ‘Universal Container’; insert anAccount;...

Read More
Loading

Earlier Posts

  • 2024 (27)
  • 2023 (47)
  • 2022 (25)
  • 2021 (35)
  • 2020 (41)
  • 2019 (13)
  • 2018 (16)
  • 2017 (14)
  • 2016 (18)
  • 2015 (74)
  • 2014 (20)