GEOLOCATION() Function in Salesforce | Calculate geolocation in Salesforce

In this Salesforce tutorial, we will learn the GEOLOCATION() function in Salesforce and how we can use it for calculating geolocation based on latitude and longitude. Moreover, we will focus on the syntax of the Salesforce GEOLOCATION() function and some practical examples for implementation.

Recently I worked for a company that utilizes Salesforce as a CRM tool. While working here, I was given the responsibility to display the shipping price of the product based on customer location and warehouse location.

So as a solution, I discovered a GEOLOCATION() function. Let’s go into more detail and discovered how to use Salesforce Lightning and Salesforce Classic components for using the GEOLOCATION() function.

What is GEOLOCATION() function in Salesforce

There are various mathematical functions available in Salesforce that are used to perform mathematical calculations and GEOLOCATION() is one of them.

This GEOLOCATION() function is used to create geolocation values. It is also used to return geolocation based on latitude and longitude values. In addition, make sure that this GEOLOCATION() function must be used with the DISTANCE() function of Salesforce.

Moreover, keep one thing in mind this GEOLOCATION() function only supports the “GeoLocation” data type.

Syntax and Usage of GEOLOCATION() function in Salesforce

Below is the syntax for the GEOLOCATION() function in Salesforce:

GEOLOCATION(latitude, longitude)

Here, we have two parameters as follows:

  • latitude: This parameter specifies the latitude value of the geolocation. It takes the value as a number, field, or formula.
  • longitude: This parameter specifies the longitude value of the geolocation. The value of the parameter can be a field, a constant number, or a complex formula.

To deeply understand this concept, let’s see a Salesforce example:

As I have discussed above that I got a task to display the shipping price of the product based on customer location distance with warehouse location and for this, I will use the GEOLOCATION() function with the DISTANCE() function in Salesforce.

Here, is the formula:

IF(
DISTANCE(Coustmer_Location__c, GEOLOCATION(Warehouse_Location__c), 'km') > 500,
1000,
0
)

With this, we have understood the GEOLOCATION() function with syntax and one of the real-life usage examples. Now, we will move and next learn to use the GEOLOCATION() function in Salesforce Lightning.

Read MAX() Function in Salesforce | Calculate maximum value of field in Salesforce

How to use GEOLOCATION() function in Salesforce Lightning

The steps to use GEOLOCATION() function in Salesforce Lightning:

Step 1: Log in to Salesforce Lightning mode, then click on the “Gear Icon” located in the top right corner of the page. To proceed, select “Setup” from the dropdown menu.

GEOLOCATION Function in Salesforce

Step 2: From the menu’s left side, click on “Object Manager”.

How to Calculate geolocation in Salesforce Lightning Example

Step 3: Now, search for the object for which you want to use the GEOLOCATION() function in the search bar. As I want to track the campaign location, in this example, I look up the “Campaign” object.

See also  EXP() Function in Salesforce | Calculate exponential value in Salesforce

Step 4: Click on the “Campaign” object, and it will redirect to the campaign page.

GEOLOCATION Function in Salesforce Example

Step 5: From the menu on the left-hand side of the page, click on “Field and Relationship”.

Step 6: To create a formula field where we will use the GEOLOCATION() function, click on the “New” button.

GEOLOCATION Function in Salesforce Lightning Example

Step 7: Now, it redirects to the “Choose the field type” page. Here, select the “Formula” as the data type by checking the radio button next to it.

Step 8: To proceed, click on the “Next” button.

Calculate geolocation in Salesforce

Step 9: After entering the “Field Label” for the formula, the “Field Name” will be filled in automatically. In this instance, I entered “Campaign Location” as the field label.

Step 10: Select the “Data Type” for the field on which we want to apply the GEOLOCATION() function. In this case, I choose the “Number” as the formula return type and also set its “Decimal Value” to 2.

Step 11: Click on the “Next” button to proceed to the next step.

Calculate geolocation in Salesforce Example

Step 12: To compute “Campaign Location” by using the GEOLOCATION() formula, enter the formula in the advanced formula editor. The formula is as follows:

DISTANCE(Client_Location__c, GEOLOCATION(Venue_Location_Latitude__s, Venue_Location_Longitude__s), 'km')
  • In this formula, the DISTANCE() function is used to find the distance between the client location “Client_Location__c”.
  • Next, the GEOLOCATION() function is used to get the latitude and longitude value of the venue location “Venue_Location_Latitude__s” and “Venue_Location_Longitude__s” respectively.
  • Finally, the formula returns the distance between the client location and the venue location in ‘km’ kilometers.

Step 13: To make sure that there is no mistake in the formula, click on the “Check Syntax” button.

Step 14: Optionally, we may define the “Description”“Help Text”, and as well as “Handle the empty field” if you like.

Step 15: To continue, click on the “Next” button.

Calculate geolocation in Salesforce Lightning

Step 16: Select the appropriate “Level Security” option. In this case, I have selected the “Visible” checkbox as I want it to be visible to everyone. Then, click on the “Next” button from the menu.

Calculate geolocation in Salesforce Lightning Example

Step 17: Now, choose the “Page Layout” that will display your content.

See also  MCEILING() Function in Salesforce

Step 18: To save the advanced formula field, click on the “Save” button.

How to Calculate geolocation in Salesforce

We can use the formula field once we’ve created it. Let’s look at an example:

  • Open the Campaigns Tab and create a new campaign with the fields “Venue_Location_Latitude__s”, “Client_Location__c” and Venue_Location_Longitude__s.
  • After that, click on the Details section, and here the “Campaign Location” field appears which shows you the result of the GEOLOCATION() function.
How to Calculate geolocation in Salesforce Lightning

As a result, we now know how to use GEOLOCATION() function in Salesforce Lightning. Now, we’ll look at how to use GEOLOCATION() function in Salesforce Classic.

Read LN Function in Salesforce | Calculate natural logarithmic value in Salesforce

How to use GEOLOCATION() function in Salesforce Classic

The steps to use GEOLOCATION() function in Salesforce Classic:

Step 1: Open your Salesforce Classic edition from the Salesforce Lightning. Click on the “Switch to Salesforce Classic” by selecting “Avtar” first.

GEOLOCATION Function in Salesforce Classic

Step 2: Then select “Setup” from the menu.

GEOLOCATION Function in Salesforce Classic Example

Step 3: Scroll down to the “Build” section. Then under “Customize”, select the object for whom field we want to use the GEOLOCATION() function. In this instance, I use the “Product” object because I want to monitor the possibility of delivery in a specific area.

Step 4: Click on the “Products” dropdown and select the “Fields” option to create a new formula field.

Calculate geolocation in Salesforce Classic

Step 5: Now move to the “Custom Fields & Relationships” and click on the “New” button to start the process of creating a new custom field.

How to Calculate geolocation using Salesforce Classic Example

Step 6: After this, you will be redirected to the “Choose the field type” screen on which you have selected the data type for the field. Here, I choose the “Formula” as the data type of the field by checking the radio button next to the data type.

Step 7: Click on the “Next” button to move to the next proceeding step.

Calculate geolocation in Salesforce Classic Example

Step 8: Enter the “Field Label” for the formula and the unique “Field Name” will be filled out automatically. In this example, I specify the “Delivery Possibility”.

Step 9: Choose the “Data Type” for the field where we are going to calculate the delivery possibility using the geolocation function.

In this case, I select the “Text” as the formula return data type by clicking on the radio button in front of it.

Step 10: Click on the “Next” button to proceed to the next step.

How to Calculate geolocation in Salesforce Classic

Step 11: Go to the advanced formula editor and type the formula to compute the geolocation value using the GEOLOCATION() function. In this example, I want to use the GEOLOCATION() function to compute the delivery possibility.

See also  Round() Function in Salesforce

The formula is as follows:

IF(
DISTANCE(Wearhouse_Location__c ,GEOLOCATION( Client_Location__Latitude__s , Client_Location__Longitude__s ), 'km') > 50,
'Delivery Not Possible',
'Delivery Possible'
)
  • Here, we first use the GEOLOCATION function to convert the client’s latitude and longitude into geolocation values. Then, we get the distance of the warehouse location using the DISTANCE function.
  • After this, we use the IF statement that checks whether the distance between the client’s location and the warehouse location is greater than 50.
  • If it is, it prints the text “Delivery Not Possible” and otherwise prints “Delivery Possible”.

Step 12: Select “Check Syntax” to review the formula and ensure there are no errors.

Step 13: If we’d like, we may specify the “Description” and “Help text” as well as “Handle the empty field”.

Step 14: To proceed, click on the “Next” button.

How to Calculate geolocation in Salesforce Classic Example

Step 15: Choose the appropriate “Field Level Security”. Because I want it to be visible to everyone, I have chosen “Visible” in this instance. Select “Next” to continue after that.

Calculate geolocation using Salesforce Classic

Step 16: Next, choose the suitable “Page Layout” for the area where we wish to display this field.

Step 17: In order to save the formula field, click the “Save” button.

Calculate geolocation using Salesforce Classic Example

Once we’ve established the formula field, we may use it. Let’s examine an illustration:

  • Create a new product with the field “Discount” and “Original Price” by opening the Product tab.
  • When we click Save, we are taken to the Detail page, where the “Delivery Possibility” field displays the GEOLOCATION() function output.
How to Calculate geolocation using Salesforce Classic

As a result, we now know how to use GEOLOCATION() function in Salesforce Classic.

Conclusion

In a nutshell, we have learned about the GEOLOCATION() function in Salesforce with its syntax and real-life examples. In addition, we learned the steps to calculate the geolocation based on latitude and longitude values in Salesforce Lightning and Salesforce Classic.

You may also like: