Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Calculate ETA using Google Maps API

testp21
Tera Contributor
Hi Everyone, I have created a string field on incident table. I have lat long of location & assigned_to. How can I calculate ETA using Google Maps API.? Please help.
1 ACCEPTED SOLUTION

Amit Pandey
Kilo Sage

You need to have API keys for this. You can create BR with following script-

 

(function executeRule(current, previous /*previous*/) {

    // Call Google Maps Directions API to get route information
    var directionsURL = "https://maps.googleapis.com/maps/api/directions/json" +
        "?origin=" + current.assigned_to.latitude + "," + current..assigned_to.longitude +
        "&destination=" + current.location.latitude + "," + current.location.longitude +
        "&key=ENTER API KEY";

    var response = new sn_ws.RESTMessageV2();
    response.setEndpoint(directionsURL);
    response.setHttpMethod("get");
    var responseBody = response.execute().getBody();
    var parsedResponse = new global.JSON().decode(responseBody);

    if (parsedResponse && parsedResponse.routes && parsedResponse.routes.length > 0) {
        var route = parsedResponse.routes[0];
        if (route && route.legs && route.legs.length > 0) {
            var leg = route.legs[0];
            var etaSeconds = leg.duration.value; // Duration in seconds
            var etaMinutes = Math.round(etaSeconds / 60); // Convert to minutes
            current.u_eta = etaMinutes; // Store ETA in the u_eta field
            current.update(); // Update the record
        }
    }

})(current, previous);

View solution in original post

2 REPLIES 2

Amit Pandey
Kilo Sage

You need to have API keys for this. You can create BR with following script-

 

(function executeRule(current, previous /*previous*/) {

    // Call Google Maps Directions API to get route information
    var directionsURL = "https://maps.googleapis.com/maps/api/directions/json" +
        "?origin=" + current.assigned_to.latitude + "," + current..assigned_to.longitude +
        "&destination=" + current.location.latitude + "," + current.location.longitude +
        "&key=ENTER API KEY";

    var response = new sn_ws.RESTMessageV2();
    response.setEndpoint(directionsURL);
    response.setHttpMethod("get");
    var responseBody = response.execute().getBody();
    var parsedResponse = new global.JSON().decode(responseBody);

    if (parsedResponse && parsedResponse.routes && parsedResponse.routes.length > 0) {
        var route = parsedResponse.routes[0];
        if (route && route.legs && route.legs.length > 0) {
            var leg = route.legs[0];
            var etaSeconds = leg.duration.value; // Duration in seconds
            var etaMinutes = Math.round(etaSeconds / 60); // Convert to minutes
            current.u_eta = etaMinutes; // Store ETA in the u_eta field
            current.update(); // Update the record
        }
    }

})(current, previous);

testp21
Tera Contributor

Spot on! Thank you so much Sir!