Pages

Friday, March 13, 2009

Microsoft CRM: Look Up Site Address button

One of the simpler hacks I've ever accomplished in CRM, is the ability to provide a button on an Entity form that allows you to populate address fields on that same Entity with the addresses from Sites. Most of this functionality comes from the Look Up Customer Address code by Michael Höhne over at Stunnware--which, if you haven't guessed by now, is like my CRM idol.

Here's the code:

var url = '/' + ORG_UNIQUE_NAME + '/_controls/lookup/lookupsingle.aspx?class=Site&objecttypes=4009&browse=0&bindingcolumns=address1_line1%2caddress1_line2%2caddress1_line3%2caddress1_city%2caddress1_stateorprovince%2caddress1_postalcode%2caddress1_country&ShowNewButton=0&ShowPropButton=1';

var selectedAddress = window.showModalDialog(url, null, 'dialogWidth:400px;dialogHeight:400px;resizable:yes'); 

if (selectedAddress != null) { 
  var addressFields = selectedAddress.items[0].values; 
  
  for (var index in addressFields) { 
    if (addressFields[index] && addressFields[index].name) { 
      var control = document.getElementById('<Prefix Value>' + addressFields[index].name.replace(/address1_/,'')); 
      
      if (control) { 
        control.DataValue = addressFields[index].value; 
      } 
    } 
  } 
}

This code works best when all of the address fields on your form use a prefix on the name of the fields originating in the Site entity. For example, the target custom_addressline1 field from a custom entity matches the field address1_line1 on the Site entity, making "custom_address" the value for <Prefix Value>. Otherwise, you'll end up mapping each of the fields in the script.

I take the liberty of stripping the "address1_" portion of each source field name from the Site. As you can see, there's an interesting feature of lookupsingle.aspx: the variable bindingcolumns can be used to set a series of invisible fields to return in the values array. I take the above code, and drop it into the ISV.config XML file under a button element for the entity's menu, and voila! The button now generates a nice, little lookup window populated with all my Sites. Selecting one automatically populates address fields on my form. It's possible that you could extend this functionality to System User records as well, and many other record types. I just haven't done so myself.

2 comments:

  1. Just so I understand correctly - in CRM 2011 - I want to add a field to the Account form that is a lookup for ALL 'Addresses'. This can't be done without code?

    I notice you can't create any new relationships on the Address entity.

    Side note: we need this temporarily because of an integration with a home-grown contract system that had N:N Addresses to Customers.

    ReplyDelete
  2. Addresses are one of the out-of-box entities that hasn't seen much love since CRM 3, unfortunately. Lack of custom relationships is a major sticking point, but lack of customization to support static lists of values for things such as Country, State, or City often lead developers down another path.

    To date, I've heard only stories about workarounds to these problems, but they all involve code. An enterprising ISV or developer may have produced a Solution for this issue on the Dynamics Marketplace, so you would do well to look for one there.

    One of the better recommendations I've seen is to use a custom Address proxy that relays information to and from the standard Address entity (to continue supporting existing references and uses); but it can still be tricky to manage, given the lack of custom relationships.

    Consider voting on the product suggestion over at Connect to elevate its importance to the Dynamics CRM Product Team:
    http://connect.microsoft.com/dynamicssuggestions/feedback/details/683769/address-entity-to-be-a-lookup-field-and-be-related-to-other-entities

    ReplyDelete

Unrelated comments to posts may be summarily disposed at the author's discretion.