Skip to content

Instantly share code, notes, and snippets.

@liuxiachanghong
Last active January 27, 2021 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuxiachanghong/f042d78d2e5153ee181151ff11a3718a to your computer and use it in GitHub Desktop.
Save liuxiachanghong/f042d78d2e5153ee181151ff11a3718a to your computer and use it in GitHub Desktop.
Apex Code to plot accounts and leads on map
public with sharing class AccountSearchController {
@AuraEnabled
public static List<Account> searchAccounts( String searchTerm, String searchOption, Integer searchLimit, Integer searchRadius ) {
List<Account> accounts = new List<Account>();
if ( String.isNotBlank( searchTerm ) ) {
List<List<SObject>> results = [FIND :searchTerm IN ALL FIELDS
RETURNING Account(
Id, BillingAddress, BillingLatitude, BillingLongitude
WHERE BillingLatitude != Null AND BillingLongitude !=null
LIMIT 1)
];
Account[] tempAccts = (Account[])results[0];
if (tempAccts != null) {
Account centerAcct = tempAccts.get(0);
accounts = [
SELECT Id,Name,BillingAddress,Recent_Closed_Won_Date__c,Last_Sales_Activity__c,Last_Sales_Activity_By__c,Related_Opportunities_y__c,Owner_Alias__c,Client_Type_new__c,Industry,BillingState,BillingCity,BillingStreet,BillingPostalCode,BillingLatitude,BillingLongitude
FROM Account
WHERE DISTANCE (BillingAddress, Geolocation(:centerAcct.BillingLatitude, :centerAcct.BillingLongitude),'km')<:searchRadius
ORDER BY Related_Opportunities_y__c DESC
LIMIT :searchLimit
];
}
} else {
accounts = [
SELECT Id,Name,BillingAddress,Recent_Closed_Won_Date__c,Last_Sales_Activity__c,Last_Sales_Activity_By__c,Related_Opportunities_y__c,Owner_Alias__c,Client_Type_new__c,Industry,BillingState,BillingCity,BillingStreet,BillingPostalCode,BillingLatitude,BillingLongitude
FROM Account
WHERE BillingState = '東京都' AND BillingLatitude !=null AND BillingLongitude !=null
ORDER BY Related_Opportunities_y__c DESC
LIMIT :searchLimit
];
}
return accounts;
}
@AuraEnabled
public static List<Lead> searchLeads( String searchTerm, String searchOption, Integer searchLimit, Integer searchRadius ) {
List<Lead> leads = new List<Lead>();
if ( String.isNotBlank( searchTerm ) ) {
List<List<SObject>> results = [FIND :searchTerm IN ALL FIELDS
RETURNING Lead(
Id, Latitude, Longitude
WHERE Latitude != Null AND Longitude !=null
LIMIT 1)
];
Lead [] tempLeads = (Lead[])results[0];
if (tempLeads != null) {
Lead centerLead = tempLeads.get(0);
leads = [
SELECT Id,LastName,Address,Company,State,City,Street,PostalCode,Latitude,Longitude,Owner__c, Client_Type_new__c
FROM Lead
WHERE Latitude !=null AND Longitude !=null AND DISTANCE (Address, Geolocation(:centerLead.Latitude, :centerLead.Longitude),'km')<:searchRadius
ORDER BY Company DESC
LIMIT :searchLimit
];
}
} else {
leads = [
SELECT Id,LastName,Address,Company,State,City,Street,PostalCode,Latitude,Longitude,Owner__c, Client_Type_new__c
FROM Lead
WHERE State='東京都' AND Latitude !=null AND Longitude !=null
ORDER BY Company DESC
LIMIT :searchLimit
];
}
return leads;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment