How to Get the User's Current Location in Dynamics 365 Using JavaScript

 In Dynamics 365, you can use JavaScript to retrieve the user's current geographical location and store it in custom fields on the form. This guide explains how to implement a function that utilizes the browser's geolocation API.

JavaScript Code to Retrieve Current Location

Below is the JavaScript function to capture and set the user's latitude and longitude on the Dynamics 365 form:


function setCurrentUserLocation(executionContext) {
    let formContext = executionContext.getFormContext();

    // Check if geolocation is supported
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            function (position) {
                // Set latitude
                let latAttr = formContext.getAttribute("dsl_currentlocationlatitude");
                if (latAttr) {
                    latAttr.setValue(position.coords.latitude.toString());
                }

                // Set longitude
                let longAttr = formContext.getAttribute("dsl_currentlocationlongitude");
                if (longAttr) {
                    longAttr.setValue(position.coords.longitude.toString());
                }
            },
            function (error) {
                console.log("Error fetching location: " + error.message);
            },
            { enableHighAccuracy: true } // Enable high-accuracy mode
        );
    } else {
        console.log("Geolocation not supported by this browser.");
    }
}


How It Works

  1. Check Geolocation Support
    The function first checks if the browser supports the navigator.geolocation API. If not, it logs an error message.

  2. Fetch Current Position
    The getCurrentPosition method retrieves the user's latitude and longitude.

  3. Set Form Fields

    • Latitude is stored in the field dsl_currentlocationlatitude.
    • Longitude is stored in the field dsl_currentlocationlongitude.
  4. Error Handling
    If geolocation fails (e.g., permissions denied, timeout), an error message is logged to the console.


Steps to Implement

  1. Create Fields on the Form

    • Latitude Field: dsl_currentlocationlatitude (Single Line of Text or Decimal Number)
    • Longitude Field: dsl_currentlocationlongitude (Single Line of Text or Decimal Number)
  2. Add the JavaScript Code

    • Create a new web resource in Dynamics 365 and paste the above JavaScript code.
    • Publish the web resource.
  3. Attach the Function to an Event

    • Go to the form where you want to capture the location.
    • In the form editor, open Form Properties.
    • Add the web resource to the form.
    • Set the function setCurrentUserLocation as an event handler for a button click, form load, or another trigger.
  4. Test the Feature

    • Open the form and trigger the function.
    • Grant location permissions when prompted by the browser.
    • Verify that the latitude and longitude fields are populated.

Use Cases

  • Check-In/Check-Out Tracking: Capture the user's location during specific actions.
  • Delivery Management: Record the current location for pickup or drop-off validation.
  • Audit Logs: Store user location as part of activity logging.

By following these steps, you can efficiently capture the user's current location and use it for various business processes in Dynamics 365. 

No comments:

Post a Comment