locked
Unable to populate lookup value if it is first control on the page

    Question

  • Hi All,

    I am unable to populate lookup on a page load if the lookup is the first control on the page. I am using the following code. I am sending an

    Ajax call using odata end points and JQuery to populate parent customer of a contact to my custom entity on page load. The problem is sometimes

    the lookup appears blank (empty) but still alert message shows data populated in the lookup field. I am on rollup 8. Has any one faced this problem before?

    success: function(result, that){ var entity = result.results[0];
           if(entity && entity.ParentCustomerId.Id)
           {
                  var lookupValue1 = new Array();
                  lookupValue1[0] = new Object();
                  lookupValue1[0].id = entity.ParentCustomerId.Id;
                  lookupValue1[0].name = entity.ParentCustomerId.Name;
                  lookupValue1[0].entityType = "account";
                 
                  Xrm.Page.getAttribute("new_potentialcustomer").setValue(lookupValue1)
           alert("alert in if " + Xrm.Page.getAttribute("new_potentialcustomer").getValue());
     }
     else{
      alert("alert in else");
      Xrm.Page.getAttribute("new_potentialcustomer").setValue();
     }
    }

    -Devashish
    http://thecrmworld.wordpress.com
    http://ebizartisans.com





    Wednesday, September 12, 2012 11:49 AM

All replies

  • Please use the following code to set the lookup
    // Set the value of a lookup field 
    function SetLookupValue(fieldName, id, name, entityType) { 
        if (fieldName != null) { 
            var lookupValue = new Array(); 
            lookupValue[0] = new Object(); 
            lookupValue[0].id = id; 
            lookupValue[0].name = name; 
            lookupValue[0].entityType = entityType; 
            Xrm.Page.getAttribute(fieldName).setValue(lookupValue); 
        } 
    }



    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!

    • Proposed as answer by MubasherSharif Wednesday, September 12, 2012 11:54 AM
    • Unproposed as answer by Devashish Bajpai Wednesday, September 12, 2012 11:59 AM
    Wednesday, September 12, 2012 11:54 AM
  • My code looks exactly the same as yours. It even works some times but not always. The alert always shows that the lookup field has data but on the form it appears as blank.

    -Devashish
    http://thecrmworld.wordpress.com
    http://ebizartisans.com

    Wednesday, September 12, 2012 12:02 PM
  • Than make sure id, name and entity type is correct.

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!

    Wednesday, September 12, 2012 12:12 PM
  • Id and name is being returned from OData it should be correct, entity name is account in lower case which is also correct. Infact if you have looked at my code then I have added an alert also which shows that new_potentialcustomer has a value as well. 

    alert("alert in if " + Xrm.Page.getAttribute("new_potentialcustomer").getValue());


    -Devashish
    http://thecrmworld.wordpress.com
    http://ebizartisans.com




    Wednesday, September 12, 2012 12:16 PM
  • Davashish,

    I have experienced this same problem, only my symptom is if there is any onLoad code, the lookup field can't get the focus on the form.  I saw a post a while ago that contained a registry hack to fix it, but it didn't work for me...   This is definitely a bug..  I have also heard this may have been fixed in UR10, but my brief testing did not seem to show this as fixed...    You might try moving the field around on the form, as this primarily happens to me when the lookup field is the first field on the form...  HTH and good luck,

    Dave

    Wednesday, September 12, 2012 3:04 PM
  • Hi Dave,

    Thanks for the reply I am facing the same problem. If I move the field around it starts working. Do you have a link to Microsoft issue which you are talking about? 


    -Devashish
    http://thecrmworld.wordpress.com
    http://ebizartisans.com

    Wednesday, September 12, 2012 3:10 PM
  • Have you tried to get the attribute first, with

    var luAttr = document.getElementById(strLuAttrName);
    

    Then, if luAttr is null, exit the function and call it again, until luAttr is filled?

    Alternatively try

    var attr = Xrm.Page.getAttribute("new_potentialcustomer");

    if (attr != null) {
    attr.setValue(lookupValue1);
    }
    else
    {
    alert("I didn't find my lookup");
    }

    To me it sounds as if the Lookup hasn't loaded completely yet.

    Wednesday, September 12, 2012 3:19 PM
  • Davashish,

    I have not seen this as an "approved" MSFT bug, but I did find this one blog post discussing a solution.  (Again, didn't work for me, but good luck)

    http://blog.meteorit.co.uk/2012/01/26/problem-with-unresponsive-lookup-field-on-crm-2011-form/

    HTH,

    Dave

    Wednesday, September 12, 2012 3:25 PM
  • Thanks for the reply. Yes I have tried this. The Javascript tells me that lookup value is populated, the Javascript alert even shows id and name of the lookup populated, but the same is not visible on the UI. Kind of strange. Also this happens only when the lookup is the first value of the form. 

    -Devashish
    http://thecrmworld.wordpress.com
    http://ebizartisans.com


    Thursday, September 13, 2012 4:34 AM
  • Hi,

    I have investigated a bit more and I have implemented a hack/work around. I have added a dummy text field the the custom entity form and places this dummy text field as the first control of the form. I hide this dummy control on page load and every thing seems to be working fine without any problem. I am sure that this a MS issue can any one tell how to register an issue with MS?


    -Devashish
    http://thecrmworld.wordpress.com
    http://ebizartisans.com

    Saturday, September 15, 2012 4:06 PM