Monday, November 26, 2012

Calculate Mileage in CRM 2011 using Google API

Some companies are using Mileage functionality to give there employees Travel allowance. To automate this process we can use Google API to Get the distance and give allowance to there employee.
Google Map API requires two fields Origin, Destination and it gives us its complete detail i.e. Correct address, Distance etc. But for now we only need distance to give our employee Travel allowance depending upon the distance that they travels. It is really helpful in any Marketing Company or any other company that gives Travel allowance to its employees.

Prerequisites for Integration between CRM 2011 and Google Map Api

1. CRM 2011
2. Google Map API Script File (Download)

Note: This code works on Zip code. Enter the Origin, Destination and then Cost per mile.

Here is the complete code you only need to change the Bold lines (Schema Names)


 function calculateCost()  
 {  
 var distance = Xrm.Page.data.entity.attributes.get("new_distance").getValue();  
 var costpermile = Xrm.Page.data.entity.attributes.get("new_costpermile").getValue();  
 var totalCost = distance * costpermile;  
 var Name = Xrm.Page.data.entity.attributes.get("new_totalcost");   
   Name.setValue(totalCost);  
 }  

 function GetMilesFromZipcodes()  
 {  

 //Please pass your origin and destination values here.
 var origin1 = Xrm.Page.data.entity.attributes.get("new_origin").getValue();   
 var destinationA = Xrm.Page.data.entity.attributes.get("new_destination").getValue(); 
 var distance = Xrm.Page.data.entity.attributes.get("new_distance");
 var service = new google.maps.DistanceMatrixService();  
 service.getDistanceMatrix(  
  {  
   origins: [origin1],  
   destinations: [destinationA],  
   travelMode: google.maps.TravelMode.DRIVING,  
   avoidHighways: false,  
   avoidTolls: false  
  }, callback);  
 function callback(response, status) {  
 if (status == google.maps.DistanceMatrixStatus.OK) {  
   var origins = response.originAddresses;  
   var destinations = response.destinationAddresses;  
   for (var i = 0; i < origins.length; i++) {  
    var results = response.rows[i].elements;  
    for (var j = 0; j < results.length; j++) {  
     var element = results[j];  
     var distance = element.distance.value;  
     //Convert KM to Miles
     var distance1 = (distance * 0.621371)/1000;  
    
   distance.setValue(distance1);  
     var duration = element.duration.text;  
     var from = origins[i];  
     var to = destinations[j];  
    }  
   }  
  }  
 }  
 }  
 window.google = window.google || {};  
 google.maps = google.maps || {};  
 (function() {  
  function getScript(src) {  
   document.write('<' + 'script src="' + src + '"' +  
           ' type="text/javascript"><' + '/script>');  
  }  
  var modules = google.maps.modules = {};  
  google.maps.__gjsload__ = function(name, text) {  
   modules[name] = text;  
  };  
  google.maps.Load = function(apiLoad) {  
   delete google.maps.Load;  
   apiLoad([0.009999999776482582,[[["https://mts0.googleapis.com/vt?lyrs=m@199000000\u0026src=api\u0026hl=en-US\u0026","https://mts1.googleapis.com/vt?lyrs=m@199000000\u0026src=api\u0026hl=en-US\u0026"],null,null,null,null,"m@199000000"],[["https://khms0.googleapis.com/kh?v=121\u0026hl=en-US\u0026","https://khms1.googleapis.com/kh?v=121\u0026hl=en-US\u0026"],null,null,null,1,"121"],[["https://mts0.googleapis.com/vt?lyrs=h@199000000\u0026src=api\u0026hl=en-US\u0026","https://mts1.googleapis.com/vt?lyrs=h@199000000\u0026src=api\u0026hl=en-US\u0026"],null,null,"imgtp=png32\u0026",null,"h@199000000"],[["https://mts0.googleapis.com/vt?lyrs=t@129,r@199000000\u0026src=api\u0026hl=en-US\u0026","https://mts1.googleapis.com/vt?lyrs=t@129,r@199000000\u0026src=api\u0026hl=en-US\u0026"],null,null,null,null,"t@129,r@199000000"],null,[[null,0,7,7,[[[330000000,1246050000],[386200000,1293600000]],[[366500000,1297000000],[386200000,1314843700]]],["https://mt0.gmaptiles.co.kr/mt?v=kr1.17\u0026hl=en-US\u0026","https://mt1.gmaptiles.co.kr/mt?v=kr1.17\u0026hl=en-US\u0026"]],[null,0,8,8,[[[330000000,1246050000],[386200000,1279600000]],[[345000000,1279600000],[386200000,1286700000]],[[354690000,1286700000],[386200000,1314843700]]],["https://mt0.gmaptiles.co.kr/mt?v=kr1.17\u0026hl=en-US\u0026","https://mt1.gmaptiles.co.kr/mt?v=kr1.17\u0026hl=en-US\u0026"]],[null,0,9,9,[[[330000000,1246050000],[386200000,1279600000]],[[340000000,1279600000],[386200000,1286700000]],[[348900000,1286700000],[386200000,1302000000]],[[368300000,1302000000],[386200000,1314843700]]],["https://mt0.gmaptiles.co.kr/mt?v=kr1.17\u0026hl=en-US\u0026","https://mt1.gmaptiles.co.kr/mt?v=kr1.17\u0026hl=en-US\u0026"]],[null,0,10,19,[[[329890840,1246055600],[386930130,1284960940]],[[344646740,1284960940],[386930130,1288476560]],[[350277470,1288476560],[386930130,1310531620]],[[370277730,1310531620],[386930130,1314843700]]],["https://mt0.gmaptiles.co.kr/mt?v=kr1.17\u0026hl=en-US\u0026","https://mt1.gmaptiles.co.kr/mt?v=kr1.17\u0026hl=en-US\u0026"]],[null,3,7,7,[[[330000000,1246050000],[386200000,1293600000]],[[366500000,1297000000],[386200000,1314843700]]],["https://mt0.gmaptiles.co.kr/mt?v=kr1p.17\u0026hl=en-US\u0026","https://mt1.gmaptiles.co.kr/mt?v=kr1p.17\u0026hl=en-US\u0026"]],[null,3,8,8,[[[330000000,1246050000],[386200000,1279600000]],[[345000000,1279600000],[386200000,1286700000]],[[354690000,1286700000],[386200000,1314843700]]],["https://mt0.gmaptiles.co.kr/mt?v=kr1p.17\u0026hl=en-US\u0026","https://mt1.gmaptiles.co.kr/mt?v=kr1p.17\u0026hl=en-US\u0026"]],[null,3,9,9,[[[330000000,1246050000],[386200000,1279600000]],[[340000000,1279600000],[386200000,1286700000]],[[348900000,1286700000],[386200000,1302000000]],[[368300000,1302000000],[386200000,1314843700]]],["https://mt0.gmaptiles.co.kr/mt?v=kr1p.17\u0026hl=en-US\u0026","https://mt1.gmaptiles.co.kr/mt?v=kr1p.17\u0026hl=en-US\u0026"]],[null,3,10,null,[[[329890840,1246055600],[386930130,1284960940]],[[344646740,1284960940],[386930130,1288476560]],[[350277470,1288476560],[386930130,1310531620]],[[370277730,1310531620],[386930130,1314843700]]],["https://mt0.gmaptiles.co.kr/mt?v=kr1p.17\u0026hl=en-US\u0026","https://mt1.gmaptiles.co.kr/mt?v=kr1p.17\u0026hl=en-US\u0026"]]],[["https://cbks0.googleapis.com/cbk?","https://cbks1.googleapis.com/cbk?"]],[["https://khms0.googleapis.com/kh?v=66\u0026hl=en-US\u0026","https://khms1.googleapis.com/kh?v=66\u0026hl=en-US\u0026"],null,null,null,null,"66"],[["https://mts0.googleapis.com/mapslt?hl=en-US\u0026","https://mts1.googleapis.com/mapslt?hl=en-US\u0026"]],[["https://mts0.googleapis.com/mapslt/ft?hl=en-US\u0026","https://mts1.googleapis.com/mapslt/ft?hl=en-US\u0026"]],[["https://mts0.googleapis.com/vt?hl=en-US\u0026","https://mts1.googleapis.com/vt?hl=en-US\u0026"]]],["en-US","US",null,0,null,null,"https://maps.gstatic.com/mapfiles/","https://csi.gstatic.com","https://maps.googleapis.com","https://maps.googleapis.com"],["https://maps.gstatic.com/intl/en_us/mapfiles/api-3/11/0","3.11.0"],[3613555064],1.0,null,null,null,null,0,"",null,null,1,"https://khms.googleapis.com/mz?v=121\u0026",null,"https://earthbuilder.google.com","https://earthbuilder.googleapis.com"], loadScriptTime);  
  };  
  var loadScriptTime = (new Date).getTime();  
  getScript("https://maps.gstatic.com/intl/en_us/mapfiles/api-3/11/0/main.js");  
 })();  

Screen Shot



1 comment:

  1. Interesting post. thanks for sharing helpful information with us. Appconsultio is an app development company in Mumbai that empowers endless Development to make a comprehensive, practical future. From concept to launch, we pride ourselves on delivering cutting-edge solutions that empower businesses to thrive in the digital landscape.

    ReplyDelete