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.
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}"