Do you want a drop down box for the state, but you don’t want to break core CRM functionality?
Ok. This little onload script will show the states for the United States and Canada (You can always modify it to have more options). This will help restrict what people put in the state field by providing them a drop down box. It will also allow them to view the old text box and enter any custom information just by clicking on ‘* ENTER A CUSTOM PROVINCE *’.
This script will work on the onload for a lead, contact, account, and sales-contact.
Please post a comment and let me know what you think!
If you’re a consultant and you use this, please at least give us credit and provide the customer with the web address to our blog.
Onload Jscript Code:
function ddlState ()
{
// Get the state or province cell.
var provinceid = 'stateorprovince';
var statecell = document.getElementById(provinceid + '_d');
if (!statecell)
{
provinceid = 'address1_stateorprovince';
statecell = document.getElementById(provinceid + '_d');
}
var stateobject = document.getElementById(provinceid);
stateobject.style.display = 'none';
var statevalue = stateobject.value;
var ddl = document.createElement('select');
ddl.setAttribute('id','new_stateorprovince');
ddl.setAttribute('class','ms-crm-SelectBox');
ddl.setAttribute('req','2');
ddl.setAttribute('height','4');
ddl.setAttribute('style','IME-MODE: auto');
// Build some lists.
var CStateName = new Array();
var CStateAbbr = new Array();
var USStateName = new Array();
var USStateAbbr = new Array();
// If nothing is entered for state, show SELECT A STATE
var found=false;
if (statevalue.length<1)
{
stateoption=document.createElement('option');
stateoption.setAttribute('value','');
stateoption.innerHTML = ' ** SELECT A STATE ** ';
ddl.appendChild(stateoption);
found=true;
}
// Build Canada Array
CStateName[0] = "Alberta"; CStateAbbr[0] = "AB";
CStateName[1] = "British Columbia"; CStateAbbr[1] = "BC";
CStateName[2] = "Manitoba"; CStateAbbr[2] = "MB";
CStateName[3] = "New Brunswick"; CStateAbbr[3] = "NB";
CStateName[4] = "Newfoundland and Labrador"; CStateAbbr[4] = "NL";
CStateName[5] = "Northwest Territories"; CStateAbbr[5] = "NT";
CStateName[6] = "Nova Scotia"; CStateAbbr[6] = "NS";
CStateName[7] = "Nunavut"; CStateAbbr[7] = "NU";
CStateName[8] = "Ontario"; CStateAbbr[8] = "ON";
CStateName[9] = "Prince Edward Island"; CStateAbbr[9] = "PE";
CStateName[10] = "Quebec"; CStateAbbr[10] = "QC";
CStateName[11] = "Saskatchewan"; CStateAbbr[11] = "SK";
CStateName[12] = "Yukon"; CStateAbbr[12] = "YT";
// Build US Array
USStateName[0] = "Alabama"; USStateAbbr[0] = "AL";
USStateName[1] = "Alaska"; USStateAbbr[1] = "AK";
USStateName[2] = "Arizona"; USStateAbbr[2] = "AZ";
USStateName[3] = "Arkansas"; USStateAbbr[3] = "AR";
USStateName[4] = "California"; USStateAbbr[4] = "CA";
USStateName[5] = "Colorado"; USStateAbbr[5] = "CO";
USStateName[6] = "Connecticut"; USStateAbbr[6] = "CT";
USStateName[7] = "District of Columbia"; USStateAbbr[7] = "DC";
USStateName[8] = "Delaware"; USStateAbbr[8] = "DE";
USStateName[9] = "Florida"; USStateAbbr[9] = "FL";
USStateName[10] = "Georgia"; USStateAbbr[10] = "GA";
USStateName[11] = "Hawaii"; USStateAbbr[11] = "HI";
USStateName[12] = "Idaho"; USStateAbbr[12] = "ID";
USStateName[13] = "Illinois"; USStateAbbr[13] = "IL";
USStateName[14] = "Indiana"; USStateAbbr[14] = "IN";
USStateName[15] = "Iowa"; USStateAbbr[15] = "IA";
USStateName[16] = "Kansas"; USStateAbbr[16] = "KS";
USStateName[17] = "Kentucky"; USStateAbbr[17] = "KY";
USStateName[18] = "Louisiana"; USStateAbbr[18] = "LA";
USStateName[19] = "Maine"; USStateAbbr[19] = "ME";
USStateName[20] = "Maryland"; USStateAbbr[20] = "MD";
USStateName[21] = "Massachusetts"; USStateAbbr[21] = "MA";
USStateName[22] = "Michigan"; USStateAbbr[22] = "MI";
USStateName[23] = "Minnesota"; USStateAbbr[23] = "MN";
USStateName[24] = "Mississippi"; USStateAbbr[24] = "MS";
USStateName[25] = "Missouri"; USStateAbbr[25] = "MO";
USStateName[26] = "Montana"; USStateAbbr[26] = "MT";
USStateName[27] = "Nebraska"; USStateAbbr[27] = "NE";
USStateName[28] = "Nevada"; USStateAbbr[28] = "NV";
USStateName[29] = "New Hampshire"; USStateAbbr[29] = "NH";
USStateName[30] = "New Jersey"; USStateAbbr[30] = "NJ";
USStateName[31] = "New Mexico"; USStateAbbr[31] = "NM";
USStateName[32] = "New York"; USStateAbbr[32] = "NY";
USStateName[33] = "North Carolina"; USStateAbbr[33] = "NC";
USStateName[34] = "North Dakota"; USStateAbbr[34] = "ND";
USStateName[35] = "Ohio"; USStateAbbr[35] = "OH";
USStateName[36] = "Oklahoma"; USStateAbbr[36] = "OK";
USStateName[37] = "Oregon"; USStateAbbr[37] = "OR";
USStateName[38] = "Pennsylvania"; USStateAbbr[38] = "PA";
USStateName[39] = "Rhode Island"; USStateAbbr[39] = "RI";
USStateName[40] = "South Carolina"; USStateAbbr[40] = "SC";
USStateName[41] = "South Dakota"; USStateAbbr[41] = "SD";
USStateName[42] = "Tennessee"; USStateAbbr[42] = "TN";
USStateName[43] = "Texas"; USStateAbbr[43] = "TX";
USStateName[44] = "Utah"; USStateAbbr[44] = "UT";
USStateName[45] = "Vermont"; USStateAbbr[45] = "VT";
USStateName[46] = "Virginia"; USStateAbbr[46] = "VA";
USStateName[47] = "Washington"; USStateAbbr[47] = "WA";
USStateName[48] = "West Virginia"; USStateAbbr[48] = "WV";
USStateName[49] = "Wisconsin"; USStateAbbr[49] = "WI";
USStateName[50] = "Wyoming"; USStateAbbr[50] = "WY";
// Build the drop down
var stateoption;
stateoption=document.createElement('option');
stateoption.setAttribute('value','');
stateoption.innerHTML = '---United States--------';
ddl.appendChild(stateoption);
var i=0;
for(i=0;i<USStateName.length;i++)
{
stateoption=document.createElement('option');
stateoption.setAttribute('value',USStateAbbr[i]);
if (USStateAbbr[i]==statevalue)
{
stateoption.setAttribute('selected','selected');
found = true;
}
stateoption.innerHTML = USStateAbbr[i] +
' (' + USStateName[i] + ')';
ddl.appendChild(stateoption);
}
stateoption=document.createElement('option');
stateoption.setAttribute('value','');
stateoption.innerHTML = '---Canada---------------';
ddl.appendChild(stateoption);
var i=0;
for(i=0;i<CStateName.length;i++)
{
stateoption=document.createElement('option');
stateoption.setAttribute('value',CStateAbbr[i]);
if (CStateAbbr[i]==statevalue)
{
stateoption.setAttribute('selected','selected');
found = true;
}
stateoption.innerHTML = CStateAbbr[i] + ' (' + CStateName[i] + ')';
ddl.appendChild(stateoption);
}
stateoption=document.createElement('option');
stateoption.setAttribute('value','customselection');
stateoption.innerHTML = ' ** ENTER A CUSTOM PROVINCE ** ';
if (found==false)
{
stateoption=document.createElement('option');
stateoption.setAttribute('value',statevalue);
stateoption.setAttribute('selected','selected');
stateoption.innerHTML = statevalue + ' (Custom)';
ddl.appendChild(stateoption);
}
ddl.appendChild(stateoption);
// Add the drop down and size accordingly.
statecell.appendChild(ddl);
ddl.onchange = textState;
ddl.style.width = '100%';
}
function textState()
{
// Find drop down and real textbox
provinceid = 'stateorprovince';
var stateobject = document.getElementById(provinceid);
if (!stateobject)
{
provinceid = 'address1_stateorprovince';
stateobject = document.getElementById(provinceid);
}
var ddl = document.getElementById('new_stateorprovince');
// if dropdown value is customselection, show real textbox
if (ddl.value=='customselection')
{
// Get the state or province cell.
var statecell = document.getElementById(provinceid + '_d');
statecell.removeChild(ddl);
stateobject.style.display = 'inline';
} else {
// if dropdown is not customselection,
// put value from ddl into real textbox.
stateobject.value = ddl.value;
}
}
ddlState();


Onload of page my antivirus put alert, check pls.
Thanks
Please try it again. I had to change the less than symbols to be html compatible in the script posting.
Thanks for sharing. Always good to find a real expert.
hey this is a very interesting article!
Hi, good post. I have been wondering about this issue,so thanks for posting.
Hi very nice piece of code! I put this in my 2011 environment and it works, but if I bring up an Account record where the State is already filled in and I change the State and save the Account record, when I open that Account again it does not show the State that I changed it to. It shows the original state that I first set it to. Do you know why I cannot change the state?
I also have experienced the same issue. It work great displaying the information on the state, but it cannot be used as is to set or change a state. I’ll try to spend time adjusting this tonight.
Please check out the CRM 2011 way of doing it.
Hi, very interesting blog post and thanks for posting this functionality. Will this still allow synching with Outlook contact records or no?
Thanks,
Mario
Or more specifically, will the state still synch with the Outlook contact state data within the Outlook contact record?
Thanks,
Mario
Yes, it should since it is modifying the state stored on the contact record.
It should still work since it is still modifying the CRM value.
Ok thanks for the reply.
I’m assuming if I want to use this for multiple address’s on stored on the same form I will probably need to turn this into a function that can accept the name of the state field as a parameter or? Is there a better way?
Thanks again…
Mario
Mario, the best way would definitely be to parameterize the state field for CRM 4. Is this for CRM 4 or 2011?