Wednesday, May 14, 2014

Address autocomplete for CRM 2013

Google is providing free services for address lookup using Google Places.
We can implement address lookup using google places into CRM 2013.
NOTE: THIS IS UNSUPPORTED CRM 2013 CUSTOMIZATION.

For Code check https://addressautolookup.codeplex.com

To implement address lookup in CRM 2013, we need to add two JavaScript files into CRM.
And need to call “PopulateAddresses” function on load event.
Here are step by step details
  •  Create First JavaScript web resource with name “GoogleJsapi.js”

You will find this script by browsing http://www.google.com/jsapi
Copy script to our GoogleJsapi.js file.
  • Create another JavaScript web resource with name “GoogleAddressAutocomplete.js”

This is custom script we have to create. To add autocomplete functionality for CRM attributes.
function PopulateAddresses() {

    //If google library not loaded then settime
    if (google == null) {
        google.load("maps", "3", { other_params: "libraries=places&sensor=false", "callback": PopulateAddresses });
        setTimeout(PopulateAddresses, 2000);
        return;
    }
    //If google library loaded but google maps libray not loaded then add those and settime to recall same function again
    if (google != null && google.maps == null) {
        google.load("maps", "3", { other_params: "libraries=places&sensor=false", "callback": PopulateAddresses });
        setTimeout(PopulateAddresses, 2000);
        return;
    }
    //if google map places is undefined then recall function after some time..
    if (google.maps.places == null) {
        setTimeout(PopulateAddresses, 2000);
        return;
    }

    //Get input control for Google places  
    var autocomplete;         
    var control = Xrm.Page.getAttribute('address1_composite').controls.get(0);
    if (control != null) {
        control.setFocus();
        //var input = $("#" + attributeName + "_i")
        var input = $("#address1_composite_compositionLinkControl_address1_line1_i");

        /*Sometimes google places list is not showing thats why need to add this style*/
        var pacContainerInitialized = false;
        input.keypress(function () {
            if (!pacContainerInitialized) {
                $('.pac-container').css('z-index', '9999');
                pacContainerInitialized = true;
            }
        });

        //add input to google autocomplete
        autocomplete = new google.maps.places.Autocomplete(document.getElementById('address1_composite_compositionLinkControl_address1_line1_i'));
    }

   
    /* Set up event listener for place selection */
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        /* Get place details */
        var place = autocomplete.getPlace();
        if (!place.geometry) {
            return;
        }
        /* Loop through the address components for the selected place and fill
         the corresponding input fields in CRM */

        var houseNumber, StreetName, city, state, Zip, Country;

        for (i = 0; i < place.address_components.length; i++) {

            var type = place.address_components[i].types[0];
            if (type == 'street_number') {
                houseNumber = place.address_components[i].long_name + "";
            }
            if (type == 'route') {
                StreetName = place.address_components[i].long_name;
            }
            if (type == 'locality' || type == 'administrative_area_level_3') {
                city = place.address_components[i].long_name;
            }
            if (type == 'postal_code') {
                Zip = place.address_components[i].long_name;
            }
            if (type == 'administrative_area_level_1') {
                state = place.address_components[i].short_name;
            }
            if (type == 'country') {
                Country = place.address_components[i].long_name;
            }
        }
            Xrm.Page.getAttribute('address1_line1').setValue(houseNumber);
            Xrm.Page.getAttribute('address1_line2').setValue(StreetName);
            Xrm.Page.getAttribute('address1_city').setValue(city);
            Xrm.Page.getAttribute('address1_postalcode').setValue(Zip);
            Xrm.Page.getAttribute('address1_stateorprovince').setValue(state);
            Xrm.Page.getAttribute('address1_country').setValue(Country);
    });

}
  •        Add both Scripts to CRM address ( Account/contact/ lead) entity.
  •  Add onload event
  • Call PopulateAddresses function. 





  • For google autocomplete HTMLInput type element is required.

But in CRM 2013 we will not get directly HTMLInput element for any attribute directly.
CRM 2013 is uing inline input element, so when we click on any attribute on form then dynamically input field is generated and displayed on Form.
For this we can us control.setFocus();  CRM function. When focus is set on field then we will get input element as “attributename_i”
var control = Xrm.Page.getAttribute('address1_composite').controls.get(0);
    if (control != null) {
        control.setFocus();
        //var input = $("#" + attributeName + "_i")
        var input = $("#address1_composite_compositionLinkControl_address1_line1_i");

 and then call google autocomplete
autocomplete = new google.maps.places.Autocomplete(document.getElementById('address1_composite_compositionLinkControl_address1_line1_i'));

  • Save Customization
  • Publish your customization.
  •  Now open any account/ contact or lead form
  • Click on Address Composite control


I



  • Start typing in Street1 field, because we added google place autocomplete on this field

address1_composite_compositionLinkControl_address1_line1_i

I



  • Select required address
  • The selected address will be populated in address fields




For more information on GOOGLE places



Tuesday, February 25, 2014

Total record count in MS CRM grid

By default CRM is not giving total number of records for entity in grid. If there are more than 5000 records, then CRM will display 5000+ records and when you navigate through 5000+ then count is changing per page size.
If want to show count of total number of records in grid, then need to modify database values.
Steps:
1.       Open MSCRM_CONFIG database in SQL server management studio

select * from DeploymentProperties where ColumnName ='TotalRecordCountLimit'

2.       In this table IntColumn default value is 5000.
3.       Update IntColumn value to -1

Update DeploymentProperties Set IntColumn=-1 Where ColumnName = 'TotalRecordCountLimit'

4.       When using -1, then CRM will take count of all records.
5.       If don’t want all records, but want say 15000 records count then update IntColumn value to number of record count want.
6.       Reset IIS.

7.       Try now.. 

Before



After







Monday, February 24, 2014

CRM 2013 Form change Footer Color for all forms

Footer before changing color

Footer After Changing Color



This is unsupported way of MS CRM 2013 customization.

Steps:
1.       Goto C:\Program Files\Microsoft Dynamics CRM\CRMWeb\_forms\styles
2.       Make a copy of read.css.aspx file
3.       Open read.css.aspx
4.       Find TABLE.ms-crm-Form-StandaloneSection section in CSS file.
5.       Add
background-color:#D1CBC5;
in TABLE.ms-crm-Form-StandaloneSection

Exiting
TABLE.ms-crm-Form-StandaloneSection
{
table-layout: fixed;
width: 100%;
line-height: 16px;
}

Replace with

TABLE.ms-crm-Form-StandaloneSection
{
table-layout: fixed;
width: 100%;
line-height: 16px;
background-color:#D1CBC5;
}

6.       Save file.
7.       Clear browser cache

8.       Try now.