Specify Lead Source in Dynamics CRM 2011

Paul Nieuwelaar, 01 June 2011

In a previous post of mine I described how to make a field appear when ‘Other’ is selected from the Relationship type option set on the Account form in Dynamics CRM 4.0. In this post I will be doing a similar task, however I will be making the field appear when someone selects ‘Other’ from the Lead Source option set on the Lead, and I will be doing this in Dynamics CRM 2011.

Specify Lead Source in Dynamics CRM 2011 

Ideally the image above shows how we would want this new ‘Specify’ field to appear. It needs to be directly below the Lead Source option set, so it is easily accessible, and it needs to disappear again if anything else is selected. It also needs to be required, to make sure the Lead Source always contains data.

To do this we will need to add a little bit of JScript to the Lead form. First you will need to take note of a few things. Open the Lead Source option set, and note down the field schema name (for Lead Source this is ‘leadsourcecode’), also note down the Value of the ‘Other’ option (on the Lead Source this is ‘10’ – if this number has any commas, eg ‘2,000,000’, ignore the commas and just take note of the number, eg ‘2000000’).

Specify Lead Source in Dynamics CRM 2011 

Next you will need to create a new field for the ‘Specify’ field that will appear if the ‘Other’ option is selected. After creating this field take note of the schema name, (will be something like ‘new_specify’)

 Specify Lead Source in Dynamics CRM 2011

Now open your form editor for the Lead entity, and add the new ‘Specify’ field to the form directly below the Lead Source on a new line.

Here comes the tricky part, click on ‘Form Properties’ on the form editor ribbon, and then in the new window that pops up you will see at the top of the Events tab ‘Form Libraries’. Here you will want to click ‘Add’ from the toolbar.

Specify Lead Source in Dynamics CRM 2011 

In the Look Up Record window click ‘New’ at the bottom. This will open a new Web Resource. Give it a name and display name of something like ‘showField’. For the type, select ‘Script (Jscript)’, for the language select your default, then click Save.

Once the form has saved click the Text Editor button beside the Type picklist, and then paste in the following code:

function showField(optionSet, optionValue, hiddenField, reqLevel) {
    if (Xrm.Page.getAttribute(optionSet).getValue() == optionValue) {
        Xrm.Page.getAttribute(hiddenField).setRequiredLevel(reqLevel);
        document.getElementById(hiddenField + "_c").style.display = '';
        document.getElementById(hiddenField + "_d").style.display = '';
    }
    else {
        Xrm.Page.getAttribute(hiddenField).setRequiredLevel("none");
        document.getElementById(hiddenField + "_c").style.display = 'none';
        document.getElementById(hiddenField + "_d").style.display = 'none';
    }
}

Click OK, and then click Save on the Web Resource. Once it has saved click Publish, and then close the Web Resource. Select the ‘showField’ option from the Look Up Record window and click ok. From within the Form Properties window you should now see the ‘showField’ item displaying under Form Libraries.

 Specify Lead Source in Dynamics CRM 2011

Scroll down to Event Handlers at the bottom of the Form Properties window, and click ‘Add’. In the Handler Properties window, select the ‘showField’ library, and in the Function field, type in ‘showField’. Leave the Enabled tickbox checked. In the ‘comma separated parameters’, paste in the following line:

"leadsourcecode", '10', "new_specify", "required"

Where leadsourcecode is the option set schema name, 10 is the value of the ‘Specify’ option, new_specify is the new field schema name, and required is the requirement level you want the ‘Specify’ field to be when it appears.

Specify Lead Source in Dynamics CRM 2011 

Click OK when you’re happy with the values, and then click OK to close the Form Properties window.

Next you need to open the Lead Source option set properties (double click the field), and then click the Events tab of the Field Properties window. Under the Event Handlers section click ‘Add’. Use the same method as above to set the Library, Function, and Parameters.

Close out of the Field Properties window, and then test that it is working by clicking ‘Preview’ on the ribbon, and then selecting ‘Create Form’. If it is working correctly, when you select ‘Other’ from the Lead Source option set, the new ‘Specify’ field will display, and then when you select another option it will be hidden.

Once you’re happy with it Save and Close the form editor, and then Publish your Customizations.

Specify Lead Source in Dynamics CRM 2011 

You can use this same code on any option set in your system by simply setting the Parameters specific to that scenario, to save you needing to rewrite any code.

Please feel free to post a comment below if you have any trouble getting this set up.