1
votes

I have some troubles doing some simple customizations with javascript on the form assistant (MS CRM 4.0). What I am trying to achieve is when I open the form assistant to have different selected than the current (default) ones in the look ups.

For example as in the picture below, when I select Customer I want my default selection to be "Contact", instead of Account the current and default one.

alt text

So far for the main look up (Form assitant) I managed to change the focus like this:

crmForm.all.customer.SetFocus();

But somehow I can't get to the id of the other look up. I tried to dig it out of the html, but nothing I tried seemed to work.

I do appreciate any help, articles, documentation.

Thanks!

1

1 Answers

2
votes

This is easier than I first thought, though of course it's nothing Microsoft intended to be done. The following is for the Incident form, so you might have to tweak picklist indexes for the actual form used.

In your form's OnLoad code put the following function (the window. part is important lest the function fall out of scope when the OnLoad code is through):

window.setFormAssistantPicklist = function()
{
    var plMain = document.getElementById("ContextSelect");
    if ((plMain) && (1 == plMain.selectedIndex))
    {
        var plSub = document.getElementById("selObjects");
        if (plSub)
        {
            plSub.selectedIndex = 1;
            plSub.fireEvent("onchange");
        }
        else
        {
            setTimeout(setFormAssistantPicklist, 1000);
        }
    }
};

This will check if "Customer" is selected and if the lower pane of the form assistant has been loaded, Customer" is selected, and if so, select "Contact" in the second picklist and fire its OnChange event. If the second picklist cannot be found, the lower pane is still loading (I'm not sure whether there's a way to catch that load call's OnReadyStateChange event, which would of course be more elegant) and we check it again a second later.

Now we need to make sure this code is called when the selection in the upper picklist is changed. This also goes in your OnLoad code:

var pl = document.getElementById("ContextSelect");
if (pl)
{
    pl.onchange = function ()
    {
        RelatedInformationPane.LoadContextData(); // this is the standard OnChange code of the lookup selection picklist
        setFormAssistantPicklist();
    }
}

This will cause the lower pane to be loaded, and then the checks in setFormAssistantPicklist() will be run and possibly the picklist set accordingly.