locked
lookup set default view not working RRS feed

  • Question

  • I have 2 fields optionset and lookup, based on the optionset value i want to set lookup default view dynamically using javascript.

    When i try to set Lookup field's DefaultView using javascript with the fallowing code

    Xrm.Page.getControl("productid").setDefaultView(customViewid);

                      (OR)

    Xrm.Page.getControl("productid").addCustomView(viewId, entityName, viewDisplayName, fetchXml,

    layoutXml, isDefault);

    on quoteproduct form in both the cases lookup default view doesnt changed to new defaultview
    when productid lookup properties are configured that "view selector off"
    but both the above two cases are working when view selector does not set to off

    Is there any way to set default view even "view selector off" or any way to change view

    selector and then change to lookup default view using java script

    Thanks in advance

     P V Y MAHESWAR

    Friday, March 11, 2011 9:05 PM

Answers

  • Hi Jim,

    setDefaultView and addCustomView functions of lookup are up now  even though lookup properties Default View and View Selector fields are disabled under lookup properties.

    The Trick is enable view picker using jquery

    $("#productid").attr("disableViewPicker", "0");
    Xrm.Page.getControl("productid").addCustomView(viewId, entityName, viewDisplayName, fetchXml,layoutXml, isDefault);
    $("#productid").attr("disableViewPicker", "1");
    

    Here the complete code i used to existedproduct lookup on quoteproduct form, placed in form load where line type is my custom option set represents product type

     

     

    // Advanced Filtered Lookup
    function AdvancedFilteredLookup2(lookupSchemaName, viewId, entityName, primaryKeyName, primaryFieldName, viewDisplayName) {
     var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical'>" +
    	"<entity name='" + entityName + "'>" +
    	"<attribute name='name' />" +
     "<attribute name='producttypecode' />" +
     "<attribute name='productnumber' />" +
     "<attribute name='subjectid' />" +
     "<attribute name='" + primaryFieldName + "' />" +
    	"<order attribute='name' descending='false' />"+
    	"<filter type='and'>" +
    	"</filter><filter type='and'>";
     fetchXml += "<condition attribute='statecode' operator='eq' value='0' />";
     if (Xrm.Page.getAttribute("new_linetype").getSelectedOption().text == "Product") {
     fetchXml += "<condition attribute='producttypecode' operator='eq' value='100000000' />";
     viewDisplayName = "Available Product Items";
     }
     else {
     fetchXml += "<condition attribute='producttypecode' operator='eq' value='100000001' />";
     viewDisplayName = "Available Resource Items";
     }
     fetchXml += "</filter></entity></fetch>";
    
     var layoutXml = "<grid name='resultset' " +
    	"object='1024' " +
    	"jump='name' " +
    	"select='1' " +
    	"icon='1' " +
    	"preview='1'>" +
    	"<row name='result' " +
    	"id='" + primaryKeyName + "'>";
    
     layoutXml += "<cell name='name' width='300' />";
     layoutXml += "<cell name='productnumber' width='100' />";
     layoutXml += "<cell name='producttypecode' width='150' />";
     layoutXml += "<cell name='subjectid' width='100' />";
     layoutXml += "</row></grid>";
    
     try {
     var lookupControl = Xrm.Page.ui.controls.get(lookupSchemaName);
     lookupControl.addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
     }
     catch (err) {
     }
    }
    
    function AddNewView2() {
    
     try {
     $("#productid").attr("disableViewPicker", "0");
     // Parameters
     var customViewId = "{FD140AAF-4DF4-11DD-BD17-0019B9312238}"; // new id
     var customViewName;
     var lookupFieldName = "productid";
     var entityName = "product";
     var primaryKeyName = "productid";
     var primaryFieldName = "productid";
     // Create Dynamics View
     AdvancedFilteredLookup2(lookupFieldName, customViewId, entityName, primaryKeyName, primaryFieldName, customViewName);
     $("#productid").attr("disableViewPicker", "1");
     }
     catch (err) {
     }
    }
    Thanks,
    P V Y MAHESWAR
    Tuesday, March 15, 2011 5:22 PM

All replies

  • If the lookup is configured not to show the view selector - I don't think there is a way to switch it back on at runtime.

    Make sure you are using a unique value for the viewId.


    Jim Daly Technical Writer Microsoft Dynamics CRM
    Saturday, March 12, 2011 12:37 AM
    Answerer
  • Hi Jim,

    while I am using "setDefaultview" , i used the guid of existed view

    Xrm.Page.getControl("productid").setDefaultView(customViewid);

    and when i used  "addCustomView" I prepared fetchxml,layoutxml and new unique valid viewId nothing wrong in viewId.

    Thanks.

    Saturday, March 12, 2011 9:03 AM
  • I believe the question you should be asking is "Why doesn't the quotedetail productid lookup field allow setting a default view?" - It isn't a form scripting question.

    I took a look at the Field properties for the productid in the quotedetail entity form.

    The Default View and View Selector fields are disabled. This means you cannot configure this through the application.

    I don't know why this field is not configurable, but usually there is a good reason for it. So, I believe the design is to preserve that decision and also prevent changing it in code.

    Best regards,


    Jim Daly Technical Writer Microsoft Dynamics CRM
    Saturday, March 12, 2011 10:12 PM
    Answerer
  • Hi Jim,

    setDefaultView and addCustomView functions of lookup are up now  even though lookup properties Default View and View Selector fields are disabled under lookup properties.

    The Trick is enable view picker using jquery

    $("#productid").attr("disableViewPicker", "0");
    Xrm.Page.getControl("productid").addCustomView(viewId, entityName, viewDisplayName, fetchXml,layoutXml, isDefault);
    $("#productid").attr("disableViewPicker", "1");
    

    Here the complete code i used to existedproduct lookup on quoteproduct form, placed in form load where line type is my custom option set represents product type

     

     

    // Advanced Filtered Lookup
    function AdvancedFilteredLookup2(lookupSchemaName, viewId, entityName, primaryKeyName, primaryFieldName, viewDisplayName) {
     var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical'>" +
    	"<entity name='" + entityName + "'>" +
    	"<attribute name='name' />" +
     "<attribute name='producttypecode' />" +
     "<attribute name='productnumber' />" +
     "<attribute name='subjectid' />" +
     "<attribute name='" + primaryFieldName + "' />" +
    	"<order attribute='name' descending='false' />"+
    	"<filter type='and'>" +
    	"</filter><filter type='and'>";
     fetchXml += "<condition attribute='statecode' operator='eq' value='0' />";
     if (Xrm.Page.getAttribute("new_linetype").getSelectedOption().text == "Product") {
     fetchXml += "<condition attribute='producttypecode' operator='eq' value='100000000' />";
     viewDisplayName = "Available Product Items";
     }
     else {
     fetchXml += "<condition attribute='producttypecode' operator='eq' value='100000001' />";
     viewDisplayName = "Available Resource Items";
     }
     fetchXml += "</filter></entity></fetch>";
    
     var layoutXml = "<grid name='resultset' " +
    	"object='1024' " +
    	"jump='name' " +
    	"select='1' " +
    	"icon='1' " +
    	"preview='1'>" +
    	"<row name='result' " +
    	"id='" + primaryKeyName + "'>";
    
     layoutXml += "<cell name='name' width='300' />";
     layoutXml += "<cell name='productnumber' width='100' />";
     layoutXml += "<cell name='producttypecode' width='150' />";
     layoutXml += "<cell name='subjectid' width='100' />";
     layoutXml += "</row></grid>";
    
     try {
     var lookupControl = Xrm.Page.ui.controls.get(lookupSchemaName);
     lookupControl.addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
     }
     catch (err) {
     }
    }
    
    function AddNewView2() {
    
     try {
     $("#productid").attr("disableViewPicker", "0");
     // Parameters
     var customViewId = "{FD140AAF-4DF4-11DD-BD17-0019B9312238}"; // new id
     var customViewName;
     var lookupFieldName = "productid";
     var entityName = "product";
     var primaryKeyName = "productid";
     var primaryFieldName = "productid";
     // Create Dynamics View
     AdvancedFilteredLookup2(lookupFieldName, customViewId, entityName, primaryKeyName, primaryFieldName, customViewName);
     $("#productid").attr("disableViewPicker", "1");
     }
     catch (err) {
     }
    }
    Thanks,
    P V Y MAHESWAR
    Tuesday, March 15, 2011 5:22 PM