First we need to make sure address1_latitude and address1_longitude should be added under account form and filled for all account records based on their address.

Requirement

Let’s say we have a requirement to show all the child accounts (sub accounts) as pushpins on map under parent account and show their name and city when user will hover on pushpin.

Solution

We can simply develop an html web resource where we can use Bing Maps AJAX control to show pushpins for child account based on the latitude or longitude values. We can implement this in the following three steps:

  • Get Bing Maps API.
  • Need to retrieve all child accounts for current account.
  • Add pushpins to Bing Maps based on latitude and longitude returned from child accounts.

So let’s follow the below given steps:

  • To get Bing Maps key, you can follow instructions here.

  • We can create a new solution or can do this customization in base solution, (If you are new to solution, please refer our earlier posts to create solution).

  • Let’s first upload REST.SDK.js to your solution, we can use retrieve multiple method under this library to get child records and use the below given settings (Note: You can get this file under SampleCode\JS\RESTEndpoint\JavaScriptRESTAssociateDisassociate\JavaScriptRESTAssociateDisassociate\Scripts) under your CRM SDK folder, if you don’t have CRM SDK download and extract it first.

    CRM SDK folder

    Note: We have used him prefix in our publisher it could be new for you if you are not using specific prefix.

  • Create new HTML web resource and use the following details.

    HTML web resource

  • Use the following code for this html web resource.
    1. <html>
    2. <head>
    3. <script src="../../ClientGlobalContext.js.aspx"></script>
    4. <script src="../Script/SDK.REST.js" type="text/javascript"></script>
    5. <title>Show Child Accounts</title>
    6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    7. <script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0" type="text/javascript"></script>
    8. <script type="text/javascript">
    9. var map = null;
    10. var messageBox = null;
    11. var lat = null;
    12. var lon = null;
    13. var City = null;
    14. var AccountName = null;
    15. var pushpin = null;
    16. var pushpinCollection = new Microsoft.Maps.EntityCollection();
    17. var messageBoxCollection = new Microsoft.Maps.EntityCollection();
    18. document.onreadystatechange = function ()
    19. {
    20. if(document.readyState == "complete")
    21. {
    22. //initialise map
    23. getMap();
    24. //Get child account records
    25. getChildAccounts();
    26. }
    27. }
    28. function getChildAccounts()
    29. {
    30. //retrieve current entity id
    31. var parentaccountId = window.parent.Xrm.Page.data.entity.getId();
    32. var entitySchemaName = "Account";
    33. //get all child records based on parent customer id
    34. var odataQuery = "?$select=Name,Address1_City,Address1_Latitude,Address1_Longitude&" + "$filter=ParentAccountId/Id eq guid'" + parentaccountId + "'";
    35. if(typeof (SDK) != "undefined")
    36. {
    37. //The retrieveAccountsCallBack function is passed through as the successCallBack.
    38. SDK.REST.retrieveMultipleRecords(entitySchemaName, odataQuery, getnotesImagesCallback, function (error)
    39. {
    40. alert(error.message);
    41. }, function () {});
    42. }
    43. else
    44. {
    45. alert("Not able to load REST.SDK library");
    46. }
    47. }
    48. //callback method
    49. function getnotesImagesCallback(resultSet)
    50. {
    51. //initialise message box
    52. messageBox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(0, 0),
    53. {
    54. visible: false
    55. });
    56. messageBoxCollection.push(messageBox);
    57. //Show current account
    58. lat = window.parent.Xrm.Page.getAttribute("address1_latitude")
    59. .getValue();
    60. lon = window.parent.Xrm.Page.getAttribute("address1_longitude")
    61. .getValue();
    62. City = window.parent.Xrm.Page.getAttribute("address1_city")
    63. .getValue();
    64. AccountName = window.parent.Xrm.Page.getAttribute("name")
    65. .getValue();
    66. pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(lat, lon));
    67. pushpin.Description = AccountName + ", " + City;
    68. //show message box on mouse move
    69. Microsoft.Maps.Events.addHandler(pushpin, 'mouseover', displaymessagebox);
    70. //remove message box on mouse lost
    71. Microsoft.Maps.Events.addHandler(pushpin, 'mouseout', hidemessagebox);
    72. pushpinCollection.push(pushpin);
    73. //add collection to map
    74. map.entities.push(pushpinCollection);
    75. map.entities.push(messageBoxCollection);
    76. if(resultSet.length > 0)
    77. {
    78. TotalImages = resultSet.length;
    79. for(i = 0; i < resultSet.length; i++)
    80. {
    81. lat = resultSet[i].Address1_Latitude;
    82. lon = resultSet[i].Address1_Longitude;
    83. City = resultSet[i].Address1_City;
    84. AccountName = resultSet[i].Name;
    85. pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(lat, lon));
    86. pushpin.Description = AccountName + ", " + City;
    87. //show message box on move move
    88. Microsoft.Maps.Events.addHandler(pushpin, 'mouseover', displaymessagebox);
    89. //remove message box on mouse lost
    90. Microsoft.Maps.Events.addHandler(pushpin, 'mouseout', hidemessagebox);
    91. pushpinCollection.push(pushpin);
    92. }
    93. //add collection to map
    94. map.entities.push(pushpinCollection);
    95. map.entities.push(messageBoxCollection);
    96. }
    97. }
    98. function displaymessagebox(e)
    99. {
    100. messageBox.setOptions(
    101. {
    102. description: e.target.Description,
    103. visible: true,
    104. offset: new Microsoft.Maps.Point(0, 25)
    105. });
    106. messageBox.setLocation(e.target.getLocation());
    107. }
    108. function hidemessagebox(e)
    109. {
    110. messageBox.setOptions(
    111. {
    112. visible: false
    113. });
    114. }
    115. function getMap()
    116. {
    117. map = new Microsoft.Maps.Map(document.getElementById('bingMaps'),
    118. {
    119. credentials: 'Your BingMaps key',
    120. center: new Microsoft.Maps.Location(41.956690, -103.137798),
    121. mapTypeId: Microsoft.Maps.MapTypeId.road,
    122. zoom: 10
    123. });
    124. }
    125. </script>
    126. </head>
    127. <body>
    128. <div id="bingMaps" style="width: 600px; height: 500px; position: relative;"></div>
    129. </body>
    130. </html>
  • Save and publish your web resource and place this web resource to account form by navigating Insert ->Web Resource under form editor.

  • Save and publish form and open any account record, if current account will have any child record it will show pushpins for child accounts otherwise it will just show current account record pushpin.

    Map

References: BingMaps SDK