locked
Need issue resolution for a JavaScript with fetchXml - FilteredLookup RRS feed

  • Question

  • Hi All,

    Can anyone please help me resolving the below issue.

    Requirement: I am writing a JavaScript on an entity which has two lookup values (new_configtypeid and new_configid). I need to filter the second lookup with the value selected in the first lookup. I have written the below code to achieve it but it is throwing a null exception which I am not able to trace out the cause for it.

    Here is the code. Please let me know where I am going wrong?

    Error: Unable to get property 'addCustomView' of undefined or null reference

    function DoseFilteredLookup()
    {
        debugger;
        var viewId = "{d76b2c46-c28e-4e5e-9ddf-951b71202c9d}"; //Assigning any GUID
        var entityName = "new_configuration";// Entity to be filtered
        var configType = Xrm.Page.getAttribute("new_configtypeid").getValue();

        if (configType != null)
        {
            //Name for the lookup field.
            Xrm.Page.ui.controls.get("new_configid").setDisabled(false);

            var configTypeValue = drugType[0].name;

            //Custom Name for the lookup window
            var customViewName = configTypeValue;

            var fetchXml = null;

            fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                            "<entity name='new_configuration'>" +
                                "<attribute name='new_name' />" +
                                "<attribute name='new_type1id' />" +
                                "<attribute name='new_type2id' />" +
                              "<order attribute='new_name' descending='false' />" +
                              "<filter type='and'>" +
                              "<condition attribute='new_name' operator='like' value='%" + configTypeValue + "%' />" +
                              "<condition attribute='statecode' operator='eq' value='0' />" +
                              "</filter>" +
                            "</entity>" +
                          " </fetch>";

            var layoutXml = "<grid name='resultset' " +
                                "object='1' " +
                                "jump='name' " +
                                "select='1' " +
                                "icon='1' " +
                                "preview='1'>" +
                                "<row name='result' " +
                                    "id='new_configurationid'>" + // Id attribute of the entity to be filtered
                                    "<cell name='new_name' " + //Column 1 – name
                                    "width='100' />" +
                                    "<cell name='new_type1id' " + //Column 2
                                    "width='100' />" +
                                "</row>" +
                            "</grid>";

            //Part3 add new view to the lookup
            Xrm.Page.getControl("new_configid").addCustomView(viewId, entityName, customViewName, fetchXml, layoutXml, true);
        }
    }

    Thanks,

    Sri

    Monday, June 15, 2015 5:08 AM

Answers

  • Hi All,

    The code seems to be fine. I made some mistake in my code with attribute names and corrected it now.

    It is working now as expected.

    Thanks for the response.

    Regards,

    Sri

    • Marked as answer by K_Srinivas Friday, July 10, 2015 9:32 AM
    Tuesday, June 16, 2015 9:32 AM

All replies

  • Hi,

    Where is the drugType defined in your code. You are doing var configTypeValue = drugType[0].name;

    Check the below blog. You need to set all these as given below:-

    “<condition attribute=’new_categoryid’ ” +

    “operator=’eq’ ” +

    “uiname='” + {categoryName} + “‘ ” +

    “uitype=’new_category’ ” +

    “value='” + {categoryGUID} + “‘ />” +

    https://rajeevpentyala.wordpress.com/2013/06/17/filtering-lookup-view-based-on-custom-logic-crm-2011/

    Hope this helps!!!

    Thanks,

    Prasad

    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, June 15, 2015 5:28 AM
  • Hi Prasad,

    Thanks for the reply.

    It was a typo. It is actually written in the code as  var configTypeValue = configType[0].name;

    I just wanted to check if I am doing anything wrong in fetchXML or layoutXML in my code?

    Thanks,

    Sri

    Monday, June 15, 2015 5:38 AM
  • Hi All,

    Can anybody let me know what's wrong in my code above?

    Thanks,

    Sri

    Monday, June 15, 2015 6:05 AM
  • Hi,

    try changing the below mentioned things :-

    As you have mentioned you want to filter new_configid with value selected in new_configtypeid, in that case your line

     "<condition attribute='new_name' operator='like' value='%" + configTypeValue + "%' />" +

    looks to be wrong. try something like

    var configType = Xrm.Page.getAttribute("new_configtypeid").getValue();

     var configTypeid = configType[0].id;
     var configTypename = configType[0].name;

    and in the filter condition :-

    "<filter type='and'>" +
    "<condition attribute='new_configtypeid' " +
    "operator='eq' " +
    "uiname='" + {configTypename} + "' " +
    "uitype='new_configtype' " +
    "value='" + {configTypeid} + "' />" +
    "/filter>"

    Hope this helps!!!

    Thanks,

    Prasad

    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, June 15, 2015 6:08 AM
  • Hi Prasad,

    My Requirement is -

    - I need filter new_configid(lookup) with value selected in new_configtypeid(lookup).

    - new_configid lookup should show only the records of name containing the new_configtypeid name.

    So, I am getting the name from new_configtypeid and querying on new_configid reference entity by filtering with the respective name. The above lines given by you may not satisfy my requirement. Can have any other solution pls?

    Thanks,

    Sri

    Tuesday, June 16, 2015 8:26 AM
  • Hi All,

    The code seems to be fine. I made some mistake in my code with attribute names and corrected it now.

    It is working now as expected.

    Thanks for the response.

    Regards,

    Sri

    • Marked as answer by K_Srinivas Friday, July 10, 2015 9:32 AM
    Tuesday, June 16, 2015 9:32 AM
  • Hello,

    Please read this post to see how you can programmatically filter 1:N or N:N subgrids in CRM, the proper way.


    Cornel Croitoriu - Senior Software Developer

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Biz-Forward.comCroitoriu.NET

    Thursday, June 25, 2015 8:28 AM