You need to sign in to do that
Don't have an account?
Alex Calder 10
I am not a coder, but I am pretty ok at finding code and making it work. Can someone please help with with this. Why is it not finding the record.
HTTP API to Google DistanceMatrix
public class DriveCode{ @future (callout=true) // future method needed to run callouts from Triggers public static void getDistance(){ // gather account info Property__c [] a = [SELECT Street_Address__c, City__c, State__c, Zip__c, Service_Center__c, NeedDistance__c FROM Property__c WHERE Id ='a031Q00001wPRnYQAW']; if(a[1].NeedDistance__c == true){ // build callout String dstreet = ' '; String dcity = ' '; String dstate = ' '; String dzip = ' '; String saddress = ' '; if(a[1].Street_Address__c != null){dstreet = a[1].Street_Address__c;} if(a[1].City__c != null){dcity = a[1].City__c;} if(a[1].State__c != null){dstate = a[1].State__c;} if(a[1].Zip__c != null){dzip = a[1].Zip__c;} if(a[1].Service_Center__c != null){saddress = a[1].Service_Center__c;} String destaddress = EncodingUtil.urlEncode(dstreet + ' ' + dcity + ' ' + dstate + ' ' + dzip, 'UTF-8'); String serveaddress = EncodingUtil.urlEncode(saddress, 'UTF-8'); Http h = new Http(); HttpRequest req = new HttpRequest(); //req.setEndpoint('http://maps.googleapis.com/maps/api/distancematrix/json?origins=' + a[1].Origin_Zip__c + '&destinations=' + a[1].Destination_Zip__c + '&mode=driving&units=imperial&sensor=false'); req.setEndpoint('http://maps.googleapis.com/maps/api/distancematrix/json?origins=' + destaddress +'&destinations=' + serveaddress + '&key=AIzaSyDgO1iTw2QWJMBbgn73ODqgryrZ6sLtWTw'); //req.setEndpoint(EncodingUtil.urlEncode('http://maps.googleapis.com/maps/api/distancematrix/json?origins=' + a[1].Origin_Street__c + '+' + a[1].Origin_City__c + '+' + a[1].Origin_State__c + '+' + a[1].Origin_Zip__c + '&destinations=' + a[1].Destination_Street__c + '+' + a[1].Destination_City__c + '+' + a[1].Destination_State__c + '+' + a[1].Destination_Zip__c + '&mode=driving&units=imperial&sensor=false', 'UTF-8')); //req.setEndpoint(GEOCODING_URI_BASE + EncodingUtil.urlEncode(address, 'UTF-8')); req.setMethod('GET'); req.setTimeout(60000); integer distance = null; string tdistance = null; //HttpResponse res = h.send(req); // callout HttpResponse res = h.send(req); System.debug(res.getBody()); // parse coordinates from response JSONParser parser = JSON.createParser(res.getBody()); while (parser.nextToken() != null) { if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'distance')){ parser.nextToken(); // object start while (parser.nextToken() != JSONToken.END_OBJECT){ String txt = parser.getText(); parser.nextToken(); if (txt == 'text') tdistance = parser.getText(); else if (txt == 'value') distance = parser.getIntegerValue(); } } } // update coordinates if we get back if (distance != null){ a[1].Distance_Miles__c = distance; //a[1].tdistance__c = tdistance; a[1].NeedDistance__c = False; //a[1].Description1__c = req.getEndpoint(); //a[1].Description2__c = res.getBody(); //String originaddress = a[1].Origin_Street__c != null ? a[1].Origin_Street__c + ' ' : '' + //a[1].Origin_City__c != null ? a[1].Origin_City__c + ' ' : '' + //a[1].Origin_State__c != null ? a[1].Origin_State__c + ' ' : '' + //a[1].Origin_Zip__c != null ? a[1].Origin_Zip__c : ''; //String originaddress = EncodingUtil.urlEncode(a[1].Origin_Street__c + ' ' + a[1].Origin_City__c + ' ' + a[1].Origin_State__c + ' ' + a[1].Origin_Zip__c, 'UTF-8'); a[1].LinkToDirections__c = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=' + destaddress +'&destinations=' + serveaddress + '&key=AIzaSyDgO1iTw2QWJMBbgn73ODqgryrZ6sLtWTw'; //a[1].Description1__c = EncodingUtil.urlEncode(originaddress, 'UTF-8') + '---' + originaddress; update a; } } Else { a[1].LinkToDirections__c = 'test'; a[1].NeedDistance__c = True; update a; } } }
@isTest private class DrivefutureTest { @isTest static void test1() { // startTest/stopTest block to run future method synchronously Test.startTest(); DriveCode.getDistance(); Test.stopTest(); } }
I am not a coder, but I am pretty ok at finding code and making it work. Can someone please help with with this. Why is it not finding the record.
I hope you are doing well .....!!
Please use https instead of http while request the API.
Just Copy below link and paste in your browser :
https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&key=AIzaSyDgO1iTw2QWJMBbgn73ODqgryrZ6sLtWTw
For more info: https://developers.google.com/maps/documentation/distance-matrix/intro
Hope this helps you.
If this helps kindly mark it as BEST ANSWER so that it may help others in the future.
Thanks & Regards,
Foram Rana