locked
Multi Lookup OnChange Error RRS feed

  • Question

  • Hi Everybody,

    I'm about to pull my hair out with this one, I have used this script multiple times without any issues but for some reason this doesn't want to work now. I'm sure it something simple that I am overlooking. Here is the situation.

    I have a Lookup where the user can select either an Account or a Contact, When I click the search button I have the option to select either but when I select either an account or a contact I get the following error.

    There was an error with the fields customized event
    Field: new_ownercompanyid
    Event:onchange
    Error:Object doesn't support this property or method

    Here is my setup on the entity

    3 Fields
    new_ownercompanyid - Lookup to the Account Entity
    new_ownercontactid - Lookup to the Contact Entity
    new_owner - NVARCHART used for display in views




    OnChange Script on the new_ownercompanyid field




    crmForm.all.new_ownercontactid.DataValue = crmForm.all.new_ownercompanyid.DataValue[0].name;<br/>
    




    OnLoad on the Form




    var accountLookup = crmForm.all.new_ownercompanyid;
    var contactLookup = crmForm.all.new_ownercontactid;
    

    accountLookup.lookuptypes = "1,2"; accountLookup.lookuptypenames = "account:1,contact:2"; accountLookup.lookuptypeIcons = "/_imgs/ico_16_1.gif:/_imgs/ico_16_2.gif"; if (contactLookup.DataValue != null) { accountLookup.DefaultValue = contactLookup.DataValue; accountLookup.DataValue = contactLookup.DataValue; if (typeof (accountLookup.DataValue[0].data) != "undefined") { accountLookup.DefaultValue[0].data = accountLookup.DataValue[0].data; } } crmForm.all.new_ownercontactid_c.style.display = "none"; crmForm.all.new_ownercontactid_d.style.display = "none"; crmForm.all.new_owner_c.style.display = "none"; crmForm.all.new_owner_d.style.display = "none";



    OnSave on the Form



    var accountLookup = crmForm.all.new_ownercompanyid;
    var contactLookup = crmForm.all.new_ownercontactid;
    
    if (accountLookup.DataValue == null) {
       contactLookup.DataValue = null;
    }
    else {
       var customer = accountLookup.DataValue[0];
       if (customer.type == "1") {
          contactLookup.DataValue = null;
       }
       else {
          contactLookup.DataValue = accountLookup.DataValue;
          accountLookup.DataValue = null;
       }
    }
    
    Thanks for the help
    Friday, November 20, 2009 10:58 AM

Answers

  • It seems that you've mistyped in your script.
    You told that new_ownercontactid is a lookup so onchange script must look like:

    crmForm.all.new_ownercontactid.DataValue = crmForm.all.new_ownercompanyid.DataValue;

    other hand it seems that you want one field for displaying the field value so code must be like:

    crmForm.all.new_owner.DataValue = crmForm.all.new_ownercompanyid.DataValue[0].name;
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Friday, November 20, 2009 12:25 PM
    Moderator

All replies

  • Hi, Jacques.

    Have you placed new_ownercompanyid lookup field on the form?

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Friday, November 20, 2009 11:03 AM
    Moderator
  • Hi Andriy,

    All 3 fields are actually on the form, just hidden the new_ownercontactid and new_owner fields. The onchange script is actually on the new_ownercompanyid field
    Friday, November 20, 2009 11:10 AM
  • It seems that you've mistyped in your script.
    You told that new_ownercontactid is a lookup so onchange script must look like:

    crmForm.all.new_ownercontactid.DataValue = crmForm.all.new_ownercompanyid.DataValue;

    other hand it seems that you want one field for displaying the field value so code must be like:

    crmForm.all.new_owner.DataValue = crmForm.all.new_ownercompanyid.DataValue[0].name;
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Friday, November 20, 2009 12:25 PM
    Moderator
  • You are a genius, thanks so much.

    crmForm.all.new_owner.DataValue = crmForm.all.new_ownercompanyid.DataValue[0].name; was exactly what I needed.
    Friday, November 20, 2009 12:37 PM
  • Hi Andriy,

    I have a similar situation where I need a single look up (say customer) that allows both Account & contact. I was looking at your answers and modified the script to work for crm 2011 but i am getting errror. I have two fields

    1. new_accountname is a look up to Account
    2. new_contactname is  a look up to Contact (and I set the visibility to false). I want the account look up to include both Account & Contact data.

    On-load event of the Opty form:

    var accountLookup = Xrm.Page.getAttribute("new_accountname");
    var contactLookup = Xrm.Page.getAttribute("new_contactname");
    
    accountLookup.lookuptypes = "1,2";
    
    accountLookup.lookuptypenames = "account:1,contact:2";
    
    accountLookup.lookuptypeIcons = "/_imgs/ico_16_1.gif:/_imgs/ico_16_2.gif";
    if (contactLookup.getValue() != null) 
    {
      accountLookup.DefaultValue = contactLookup.getValue();
      accountLookup.setValue(contactLookup.getValue());
      if (typeof (accountLookup.getValue()[0].data) != "undefined") {
       accountLookup.DefaultValue[0].data = accountLookup.getValue()[0].data;
      }
    }
    crmForm.all.new_contactname_c.style.display = "none";
    crmForm.all.new_contactname_d.style.display = "none";
    

    what am I missing here? Appreciate your thoughts

    Friday, June 10, 2011 3:41 PM
  • Hello all,

    I add the same problem and I used Jacques Marais codes with some modifications and solved the problem.

    I tested it in CRM 2011 and it is working (also, ofcourse, use it in my work):

    function onLoadForm(){
        var accountLookup = crmForm.all.new_account;
        var contactLookup = crmForm.all.new_contact;
    
        accountLookup.lookuptypes = "1,2";
        accountLookup.lookuptypenames = "account:1,contact:2";
    
        if (contactLookup.DataValue != null) {
            crmForm.all.new_account.DataValue = crmForm.all.new_contact.DataValue;
    
            if (typeof(accountLookup.DataValue[0].data) != "undefined") {
                accountLookup.DefaultValue[0].data = accountLookup.DataValue[0].data;
            }
        }
    }
    
    function onSaveForm(){
        var accountLookup = crmForm.all.new_account;
        var contactLookup = crmForm.all.new_contact;
    
        if (accountLookup.DataValue == null) {
           contactLookup.DataValue = null;
        }
        else {
           var customer = accountLookup.DataValue[0];
           if (customer.type == "1") {
              contactLookup.DataValue = null;
           }
           else {
              contactLookup.DataValue = accountLookup.DataValue;
              accountLookup.DataValue = null;
           }
        }
    }
    
    function contactLookupChanged(){
        crmForm.all.new_account.DataValue = crmForm.all.new_contact.DataValue[0].name;
    }

    Although I`m not sure you will need to use the 3rd function, but I added it to be sure.

    Hope it will help anyone.

    Wednesday, March 14, 2012 9:29 AM
  • Hi,

    I have the same requirement that to view Account and contact in Custom look up view (by default its Account)

    So I used your above java script code but  I am able to see only Account in the look up and  the look for view is disabled mode.






    //Onload event.

    function onLoadForm()
    {
    var accountLookup = Xrm.Page.getAttribute('cv_parentaccountid');
    var contactLookup = Xrm.Page.getAttribute('cv_contactid');


    accountLookup.lookuptypes = "1,2";

    accountLookup.lookuptypenames = "account:1,contact:2";

    accountLookup.lookuptypeIcons = "/_imgs/ico_16_1.gif:/_imgs/ico_16_2.gif";

    if (contactLookup.getValue() != null) 
      {
       accountLookup.DefaultValue = contactLookup.getValue();
       accountLookup.setValue(contactLookup.getValue());

         if (typeof (accountLookup.getValue()[0].data) != "undefined")
          {
            accountLookup.DefaultValue[0].data = accountLookup.getValue()[0].data;
          }
      }

    }


    //onsave event.

    function onSaveForm()
    {

    var accountLookup = Xrm.Page.getAttribute('cv_parentaccountid');
    var contactLookup = Xrm.Page.getAttribute('cv_contactid');

    if (accountLookup.getValue() == null)
       {
       contactLookup.setValue(null);
       }
    else 
      {
        var customer = accountLookup.getValue()[0];
        if (customer.type == "1") 
         {
           contactLookup.setValue(null);
         }
          else
           {
              contactLookup.setValue(accountLookup.getValue());
              accountLookup.setValue(null);
           }
      }

    }


    // Onchange Event..

    function Change()
    {
     Xrm.Page.getAttribute('cv_parentaccountid').setValue(Xrm.Page.getAttribute('cv_accountid').getValue()[0].name);

    }

    Is That I am doing any thing wrong in java script?

    


    Krishn Prasad Shetty


    • Edited by Shetty KP Friday, February 15, 2013 2:35 PM
    Friday, February 1, 2013 8:46 AM