Friday, 9 August 2019

Add Custom View to lookup field and hide all other views in Dynamic 365 in javascript


Follow the below steps to add custom view to lookup field and hide all other views.

1)Go to lookup field on form, select the lookup field and click on change properties button.
Go to the additional properties and set view selector to off.


Step 2)  register this function with form onload event.


function formOnLoad(context) {
        debugger;
        try {
            var formContext = context.getFormContext();
           
            if (formContext.getControl("lookupfieldname") != null) {
                formContext.getControl("lookupfieldname").addPreSearch(function () {
                    filterServiceTypeLookup(context);
                });
            }
         
        } catch (ex) {

        }
    }


function to filter your lookup field record

    function  filterServiceTypeLookup(context) {
       
        try {
            var formContext = context.getFormContext();
            var opp = formContext.getAttribute("ab_opportunityid");
            if (opp != null) {
                var oppValue = opp.getValue();
                if (oppValue != null) {
                    var oppId = oppValue[0].id;
                    oppId = oppId.replace("{", "");
                    oppId = oppId.replace("}", "");
                   
 var fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical'    distinct='true' no-lock='true'>";
                    fetchXML += "<entity name='ab_servicetype'>";
                    fetchXML += "<attribute name='ab_servicetypeid' />";
                    fetchXML += "<attribute name='ab_name' />";
                    fetchXML += "<attribute name='createdon' />";
                    fetchXML += "<order attribute='ab_name' descending='false' />";
                    fetchXML += "<link-entity name='ab_ab_servicetype_opportunity' from='ab_servicetypeid' to='ab_servicetypeid' visible='false' intersect='true'>";
                    fetchXML += "<link-entity name='opportunity' from='opportunityid' to='opportunityid' alias='aa'>";
                    fetchXML += "<filter type='and'>";
                    fetchXML += "<condition attribute='opportunityid' operator='eq' value='" + oppId + "' />";
                    fetchXML += "</filter>";
                    fetchXML += "</link-entity>";
                    fetchXML += "</link-entity>";
                    fetchXML += "</entity>";
                    fetchXML += "</fetch>";

                    var layoutXML = "<grid name='' icon='1' select='1' jump='ab_name' object='10086' preview='1'>";
                    layoutXML += "<row id='ab_servicetypeid' name='ab_servicetype'>";
                    layoutXML += "<cell name='ab_name' width='200' imageproviderfunctionname='' imageproviderwebresource='$webresource:' />";
                    layoutXML += "<cell name='createdon' width='200' imageproviderfunctionname='' imageproviderwebresource='$webresource:' />";
                    layoutXML += "</row>";
                    layoutXML += "</grid>";

                    var viewId = "{AEFB3055-52B4-491C-B0CF-11AEFDF82825}"  // lookup view id

                    var entityName = "entityname";
                    var viewDisplayName = "Filtered View Name";

                    formContext.getControl("ab_servicetypeid").addCustomView(viewId, entityName, viewDisplayName, fetchXML, layoutXML, true);
                 
                }
            }
        } catch (ex) {

        }
    }


Step 3)
 view id is the lookup view id of the entity
 var viewId = "{AEFB3055-52B4-491C-B0CF-11AEFDF82825}

No comments:

Post a Comment

Read only field , get latest value from read only field in in power app portal

Below line is used to make field readonly in power app portal through js $( "#bdo_relationshiprole_name" ).parent().css( "po...