Find the distance using Geo Location fields.

Story: Contact has to visit a nearest branch for the respective work.

Idea: List the nearest business branches to a contact. Information can to be stored in Contact object text area field.

Snippet:

list<Contact> conLst = new List<Contact>();
list<BranchAddresses__c> otherAddressLst = [SELECT City__c, Geolocation__c FROM BranchAddresses__c];
Double miles_constant_value = 100.00;
for(Contact con : [SELECT Id, Geolocation__c, Branch_Distance__c FROM Contact]){
set<string> brCitySet = new set<string>();
Location conLocation = con.Geolocation__c;
for(BranchAddresses__c br: otherAddressLst){
Location brLocation = br.Geolocation__c;
Double distance = conLocation.getDistance(brLocation, 'mi');
if(conLocation.getDistance(brLocation, 'mi') < miles_constant_value){
brCitySet.add(br.City__c + '--> ' + distance);
}
}
con.Branch_Distance__c = null;
if(!brCitySet.isEmpty()){
for(string brStr: brCitySet)con.Branch_Distance__c += brStr + '\n';
con.Branch_Distance__c = con.Branch_Distance__c.removeStart('null');
conLst.add(con);
}else if(brCitySet.isEmpty()){
con.Branch_Distance__c = null;
conLst.add(con);
}
}
Database.SaveResult[] results = Database.Update(conLst, false);
PS: I have created this idea keeping nearest ATM find concept in my thoughts. This is not a requirement as such.

Comments