1. Create a custom field to show whether that account has closed won opportunity
  2. Create a custom button to get the number of closed won Accounts in below ranges a) >50 km b) Accounts between 30 km and 50 km c) less than 30 km

Things I have done- enter image description here

Record triggered flow with a condition set if opportunity stage=closedWon and if yes update the custom field as a checkbox with a tick.

enter image description here

The next part is to develop an apex code which fetches all these closedWon accounts in the flow and checks the distance with all the nearby accounts.And three conditions are to be set for each distance range update each custom field in the account.

I am stuck with the apex code. Code-enter image description here

I have used an invocable method to connect to the flow and getAccount queries the shipping address of the closedWon Accounts.Now I have to obtain the location of each closed won Account and check for nearby accounts.If there are any accounts within 50km range for each range(<30,30-50,>50)the value should be updated in the custom field of account.

1 Answer 1


Your apex function could use something like this

    public static List<List<Account>> getAccountsWithinDistance(List<Account> accs){
        List<List<Account>> rv = new List<List<Account>>();
        for (Account acc: accs) {
                SELECT Id, Name, ShippingAddress
                FROM Account
                WHERE DISTANCE(ShippingAddress, :acc.ShippingAddress, 'km') < 50
        return rv;

For each input account it returns a list of accounts within 50 kilometers of its shipping address.

This function is not properly bulkified though, with multiple inputs you can hit the limit on number of soql statements pretty quickly. Sadly I'm not aware of a way to do this more efficiently.


You must log in to answer this question.