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
Action Field (
dsl_action
):- This field determines the action (
Navigate to Pickup Site
orNavigate to Destination
).
- This field determines the action (
Field Values:
dsl_pickupsite
: Holds the pickup location address.dsl_destination
: Holds the destination address.
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
- Add the JavaScript code to a web resource in Dynamics 365.
- Link the function (
processNavigation
) to an event, such as a button click or field change. - Populate the
dsl_pickupsite
ordsl_destination
field and set thedsl_action
field to the desired action. - 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