locked
Error when setting lookup value through jscript, CRM 2011 RRS feed

  • Question

  • I'm trying to pre-populate a custom lookup on the order form, based on a custom lookup on the opportunity form.  The entity type of the lookup is account.

    All the documentation I've seen says that the three named parameters to be passed to the lookup are 'id', 'name' and 'entityType', like this:

    Xrm.Page.getAttribute(“CRMFieldSchemaName”).setValue( [{id: idValue, name: textValue, entityType: typeValue}]);

    However, when I pass my parameters on this form an error is thrown.  The error message is in Norwegian, but it seems that 'entityType' is not the name that CRM expects for the last parameter.

    Our emerging CRM system is hosted by our ASP supplier, while my development platform is currently CRM Online.  Eventually, the development platform will also be hosted by our ASP (when they get their act together, that is...), but for now CRM Online will have to do.  Anyways, I found that replacing 'entityType' with 'typename' works in CRM Online:

    Xrm.Page.getAttribute(“CRMFieldSchemaName”).setValue( [{id:  idValue, name: textValue, typename: typeValue}]);

    However, this still doesn't work in our ASP hosted environment.  I'm stumped, please help!

    Wednesday, May 16, 2012 9:50 AM

Answers

  • OK, I finally found the answer to this one.

    In addtion to the problem I had with pre-populating lookups, I've had severe problems with several other, system-defined lookup controls. 

    E.g:

    1. When selecting a value for any lookup, custom or system defined, the icon would not appear in the lookup field after selection, and the lookup text would frequently be incorrectly formatted, until the form was saved.
    2. When creating a product line for an offer or sales order, I would select a product from the product catalog but the sales unit would be disabled even after a valid product had been picked.  Clicking 'save' would generate an error, but the sales unit lookup would then be enabled and I could complete the order line entry.

    Turns out the culprit is the lookup field's 'most recently used items' property.  I now have to go through every form that has a lookup field, and for each field disable this property!  I have used too much time trying to track down this problem and, no, I AM NOT HAPPY ABOUT THIS.  This is not just a problem with our ASP's installation, I have had exactly the same problems in CRM Online! 

    Disabling this property makes the lookup work as it should.


    • Marked as answer by 78 north Tuesday, June 5, 2012 7:56 AM
    • Edited by 78 north Tuesday, June 5, 2012 7:58 AM
    Tuesday, June 5, 2012 7:56 AM

All replies

  • Is everything the same in both environments, variable names etc.

    I would turn on the trace to see if you can get more information or at least step through the code using F11 javascript debugger

    this blog post is a great javascript reference 

    http://gtcrm.wordpress.com/2011/03/16/jscript-reference-for-microsoft-crm-2011/

    maybe try the code below 

    // 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); 
        } 
    }


    Ben Hosking
    Check out my CRM Blog
    Linked-In Profile
    Follow Me on Twitter!

    Wednesday, May 16, 2012 10:19 AM
  • The lookup field names are not the same in both environments, but that's not where the problem is.  When populating the lookup, the id and name seems to be set correctly.  I.e., the target field displays the passed value but the icon on the lookup field is not set.  I've tried to step through the code, but the error is cast when the array is passed to .setValue.  I've tried the code you posted and it throws the same error.
    Wednesday, May 16, 2012 10:30 AM
  • Each lookup represents one end of the 1:N entity relationship.

    You are saying that both lookups are configured to allow a lookup to an account record. I would double check to make sure that the type of entity being passed is valid for that particular relationship.

    I've used the Set Lookup Attribute Value sample from the SDK many times with no issues.


    Jim Daly Technical Writer Microsoft Dynamics CRM

    Thursday, May 24, 2012 10:27 PM
    Answerer
  • Hi

    Please refer this link

    http://crmbusiness.wordpress.com/2011/02/18/crm-2011-how-to-set-up-a-lookup-using-javascript/

    Regards

    Dhamodharan

    Friday, May 25, 2012 7:36 AM
  • OK, I finally found the answer to this one.

    In addtion to the problem I had with pre-populating lookups, I've had severe problems with several other, system-defined lookup controls. 

    E.g:

    1. When selecting a value for any lookup, custom or system defined, the icon would not appear in the lookup field after selection, and the lookup text would frequently be incorrectly formatted, until the form was saved.
    2. When creating a product line for an offer or sales order, I would select a product from the product catalog but the sales unit would be disabled even after a valid product had been picked.  Clicking 'save' would generate an error, but the sales unit lookup would then be enabled and I could complete the order line entry.

    Turns out the culprit is the lookup field's 'most recently used items' property.  I now have to go through every form that has a lookup field, and for each field disable this property!  I have used too much time trying to track down this problem and, no, I AM NOT HAPPY ABOUT THIS.  This is not just a problem with our ASP's installation, I have had exactly the same problems in CRM Online! 

    Disabling this property makes the lookup work as it should.


    • Marked as answer by 78 north Tuesday, June 5, 2012 7:56 AM
    • Edited by 78 north Tuesday, June 5, 2012 7:58 AM
    Tuesday, June 5, 2012 7:56 AM
  • 78 -

    I tried to reproduce what you are reporting and so far I can't.

    Here are the repro steps I tried.

    1. Install the managed solution in the SDK located at sdk\samplecode\js\formscripts\formscriptdemonstrations_1_0_0_0_managed.zip
    2. Open a new Account Record.
    3. In the Customize tab (assuming you are system administrator) you will see a Form Script Libraries control in the Sample Scripts group.
    4. Select SDK.AttributeSamples.js > setAttributeValues

    Expected results:

    • All fields in the Account form should be populated with some value, depending on the type of field.
    • The Parent Account lookup shoul be populated with the first Account record available in your system. There may be a slight delay in setting that value because of the query involved to retrieve that record.

    I am interested to know what results you get (both on your ASP and CRM Online environments) running the code from this solution.

    I'm sorry to hear that this has been such a problem. If you haven't already I hope you will contact Technical Support. This is the first I've heard of any connection between disabling the most recently used items for a field and your ability to set values using the Xrm.Page API.


    Jim Daly Technical Writer Microsoft Dynamics CRM

    Tuesday, June 5, 2012 2:47 PM
    Answerer
  • My issue seems to be related to general unexpected behavior from the lookup control.  I started using CRM 2011 online in Feb 2012, and from the very beginning there have been issues with the lookup, and this is across all entities even before I had started on any customizations.  Due to business requirements we ditched CRM Online and started on a fresh CRM installation in April, hosted by our ASP provider.

    A general description of the problem I've been experiencing follows, but note that this behavior is not just limited to loookups in the entity forms mentioned here, but to all lookups across the application.  This description is related to an instance of CRM Online, but the behavior is exactly the same in our ASP hosted instance.  Note that we use the Norwegian language pack.

    When starting a new session in in the morning, lookups work as expected for the first few operations, then the problem begins:

    Case study:  Entering a new product using the Product entity form.  I enter ID, Name and then click the Unit group lookup to select the unit group.  After selecting my group from the dialog and returning to the Product form, the lookup is not populated and the Standard unit field is still disabled. 

    Moving focus to a different field makes the Unit group display the correct text, but the formatting is wrong and the corresponding icon is not displayed in the lookup field.  The Standard unit field is still disabled.  I then enter a value in the Supported decimals field and click save.  An error message is displayed with the following error log:

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: The unit id is missing.Detail:

    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">

      <ErrorCode>-2147206387</ErrorCode>

      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />

      <Message>The unit id is missing.</Message>

      <Timestamp>2012-06-06T08:14:49.4051293Z</Timestamp>

      <InnerFault i:nil="true" />

      <TraceText i:nil="true" />

    </OrganizationServiceFault>

    When clicking OK on the error message and returning to the Product form, the unit field is enabled and I'm able to select the correct unit, but the formatting is still wrong.

    After saving, the form then renders as it should.  However, the problem frequently worsens and gets to the point where the the dependent lookup field is never enabled, making it impossible to continue using the form.

    Disabling 'most recently used items' property of the lookups made the problem described above go away and the lookups work as they should.

    Thursday, June 7, 2012 7:29 AM
  • Jim,

    I ran the test as you suggested, starting with the ASP hosted solution.  On the first try, the Parent account field rendered as it should.  However, on the second and all subsequent tries, the Parent account field was not properly formatted, i.e. there is no 'Account' icon in the lookup field, suggesting something went wrong.  There was no error message.

     

    After disabling the 'most recently used items' property of the parent account field, the lookup was correctly formatted and the 'Account' icon is displayed in the lookup field.

    Going to CRM Online, which I hadn't used at all today, the lookup at first rendered as it should for several tries.  I then tried to provoke the lookup to fail by going to the 'Product' entity and registering several dummy products.  I used the 'Save and new' button to register several products in rapid succession w/o error.  However, when I closed and then reopened the 'Product' entity, the next product registration failed in the same manner as described in the previous post.  Going back to 'Account', all subsequent attempts to run the script failed as described above.

    Thursday, June 7, 2012 8:03 AM
  • I registered for a new Crm Online trial, and installed the SDK demo on the new trial instance.  At first everything worked as it should.  I then started to register new products using the product entity form.  After a short time of registering new products, I was able to provoke the lookup formatting error as described above, and subsequently the Account auto-populate script also failed as described in the previous posts.

    My hunch is that, when performing some action such as 'Save' before a lookup control has finished with its actions triggers an error that persists across all lookups in the application until the current application instance goes ut of scope.


    • Edited by 78 north Thursday, June 7, 2012 9:35 AM
    Thursday, June 7, 2012 9:20 AM