You need to sign in to do that
Don't have an account?

Controller to Calculate the driving Distance between 2 objects
I am trying to write a controller that will calculate the driving distance between the account and a lookup field on the account, called Facility__c. Facility__c is attached to the Object Facility__c that needs to be referenced. How do I fix my code?
public with sharing class CalculateDistanceMatrixGoogleMapsCtrl { public String accountId {get;set;} public Account account1 {get;set;} public String account1Address {get;set;} public String FacilityId {get;set;} public String facility2 {get;set;} public String facility2Address {get;set;} public CalculateDistanceMatrixGoogleMapsCtrl() { updateAccounts(); } public List<Account> getAccounts() { return accounts; } public List<Facility__c> getFacilities() { return facilities; } public void updateAccounts() { if (!accounts.isEmpty()) { accounts.clear(); } this.accounts = [SELECT ID, Name, ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode, ShippingCountry FROM Account WHERE ShippingStreet <> '' LIMIT 1]; } public void updateFacilities() { if (!facilities.isEmpty()) { facilities.clear(); } this.facilities = [SELECT ID, Name, Street_Address__c FROM Facility__c WHERE Street_Address__c <> '' LIMIT 1]; } public void updateAccountVariables() { for (Account a : accounts) { if (a.ID == accountId) { account1 = a; } } for (Facility__c f : facilities) { if (f.ID == facilityId) { facility2 = f; } } } public Boolean updateAccountAddresses() { if(( String.isBlank(account1.ShippingStreet) && String.isBlank(account1.ShippingCity) && String.isBlank(account1.ShippingState) && String.isBlank(account1.ShippingPostalCode) && String.isBlank(account1.ShippingCountry) ) && ( String.isBlank(facility2.Street_Address__c))) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Impossible build addresses for API call. Some fields are missing')); return false; } account1Address = EncodingUtil.urlEncode(( account1.ShippingStreet + ' ' + account1.ShippingCity + ' ' + account1.ShippingState + ' ' + account1.ShippingPostalCode + ' ' + account1.ShippingCountry).trim(), 'UTF-8'); facility2Address = EncodingUtil.urlEncode(( facility2.Street_Address__c).trim(), 'UTF-8'); System.debug('*** account1Address: ' + account1Address + ' , facility2Address: ' + facility2Address); return true; } public List<SelectOption> getAccountOptions() { List <SelectOption> options = new List <SelectOption>(); options.add(new SelectOption('', '--None--')); for (Account a : accounts) { options.add(new SelectOption(a.Id, a.Name)); } return options; } public void btnCalculateDistance() { if ((account1Id == null || account1Id =='') || (facility2Id == null || facility2ID =='')) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Select two Accounts then resubmit')); return; } updateAccountVariables(); boolean response = updateAccountAddresses(); if (response) { CalculateDistanceMatrixGoogleMaps gm = new CalculateDistanceMatrixGoogleMaps(account1Address, facility2Address); this.distance = isNull(gm.distance) ? 0.0 : gm.distance; this.duration = isNull(gm.duration) ? 'empty' : gm.duration; this.travelTime = isNull(gm.travelTime) ? 0 : gm.travelTime; } else { this.distance =0.0; this.duration ='empty'; this.travelTime =0; } } public List <Account> accounts =new List <Accounts>(); public Decimal distance {get; set;} public String duration {get; set;} public Integer travelTime {get; set;} public Boolean isNull(Object obj) { if (obj == null) { return true; } if (obj instanceof String) { if ((String)obj == '') { return true; } } if (obj instanceof Decimal) { if ((Decimal) obj == 0.0) { return true; } } if (obj instanceof Integer) { if ((Integer) obj == 0) { return true; } } return false; } public Boolean isDigit(Object obj) { Pattern p = Pattern.compile('^\\d+$'); Matcher m = p.matcher((String)obj); if (m.find()) { return true; } return false; } public Boolean isText(Object obj) { Pattern p = Pattern.compile('^[a-zA-Z ]*$'); Matcher m = p.matcher((String)obj); if(m.find()){ return true; } return false; } }