Microsoft CRM v3 and AJAX
Posted in Code
Last dog watch, 1 bell (6:58 pm)

I've been trying for at least a year to figure out a way to do some dynamic updating in Microsoft CRM v3 (or Dynamics, or whatever). Anyway, the problem is you don't have access to the source code for the pages, and even the customizable onLoad() or onChange() functionality just isn't enough sometimes. Perhaps you need an onKeyDown() or onBlur() function linked to an entity, which was my problem. Well, today I finally figured out how.

In the entity's onLoad() function box (we're talking Account, Contact, Lead etc), you need to write the javascript function to handle the job (I'm using some AJAX myself), and you need to attach an event to the element of the form you need to watch.

The element name is the ID found in the customization interface (in my case, I'm looking at a zip code as it is changing), the one I needed was address1_postalcode:

var xmlHttp = null;
var formZip = document.getElementById("address1_postalcode");
var formCity = document.getElementById("address1_city");
var formState = document.getElementById("address1_stateorprovince");

formZip.attachEvent("onkeydown", show);

function setCity(city) {
  formCity.value = city;

function getCity(zip) {
  xmlHttp = GetXmlHttpObject();
  if(xmlHttp == null) {
    alert("Browser does not support HTTP requests!");
  var uri = "/path/to/ajax/page.php?zip=" + zip;
  xmlHttp.onreadystatechange = stateChanged;"GET", uri, true);

function stateChanged() {
  if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
    var results = xmlHttp.responseText.split(",");

function GetXmlHttpObject() {
  var objXMLHttp = null;
  if(window.XMLHttpRequest) {
    objXMLHttp = new XMLHttpRequest();
  } else {
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
  return objXMLHttp;

function show() {
  var zip = formZip.DataValue;
  switch(zip.length) {
    case 5:
      return true;
  return true;

Everything goes into the entity's onLoad block, and not the individual attribute's onChange block. Obviously, you'd need to code your own URI to submit to, making sure it returned the properly formatted data, but this should be plenty to get anybody going with AJAX in Microsoft CRM.

The key to everything working, for me, was figuring out the attachEvent bit. Once I had that, everything else came together pretty quickly. You can change the event type to any of the other standard javascript events that happen on normal web pages.

Leave a Comment »