Using JavaScript to Navigate to Google Maps in Dynamics 365

 This guide demonstrates how to use JavaScript in Dynamics 365 to enable navigation buttons that open Google Maps with predefined locations.

JavaScript Code for Navigation

The following code focuses on opening Google Maps using the values from specific fields on the form (dsl_pickupsite and dsl_destination):


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

    // Determine the action
    let actionAttr = formContext.getAttribute("dsl_action");
    if (actionAttr) {
        let actionVal = actionAttr.getValue();

        if (actionVal) {
            actionAttr.setValue(null); // Clear the action to avoid re-triggering

            // Navigate to Pickup Site
            if (actionVal === "Navigate to Pickup Site") {
                let pickupAttr = formContext.getAttribute("dsl_pickupsite");
                if (pickupAttr && pickupAttr.getValue()) {
                    window.open("https://maps.google.com/?q=" + encodeURIComponent(pickupAttr.getValue()), "_blank");
                }
            }

            // Navigate to Destination
            else if (actionVal === "Navigate to Destination") {
                let destAttr = formContext.getAttribute("dsl_destination");
                if (destAttr && destAttr.getValue()) {
                    window.open("https://maps.google.com/?q=" + encodeURIComponent(destAttr.getValue()), "_blank");
                }
            }
        }
    }
}



How It Works

  1. Action Field (dsl_action):

    • This field determines the action (Navigate to Pickup Site or Navigate to Destination).
  2. Field Values:

    • dsl_pickupsite: Holds the pickup location address.
    • dsl_destination: Holds the destination address.
  3. Navigation Logic:

    • Checks if the action field matches either navigation case.
    • Retrieves the value from the corresponding field.
    • Opens Google Maps in a new tab with the location.

Implementation Steps

  1. Add the JavaScript code to a web resource in Dynamics 365.
  2. Link the function (processNavigation) to an event, such as a button click or field change.
  3. Populate the dsl_pickupsite or dsl_destination field and set the dsl_action field to the desired action.
  4. Trigger the function and verify that Google Maps opens correctly.

This code enables efficient navigation to pickup or destination locations directly from a Dynamics 365 form.

No comments:

Post a Comment