Lookups Showing Same Custom View in Unified Interface because of Same viewId in addCustomView JavaScript

Recently in one of my Dynamics 365 projects, I had to filter the lookup using JavaScript because of the nature of the filter which cannot be achieved using OOB filter.

I just simply write the following code to filter and set the viewId by generating some random GUID as set a static value.
var viewId = "c83ee442-91d4-4b47-bb1b-92523c80e06b";  // Random GUID
var entityName = "contact";  
var displayName = "Filtered Contacts";  
var fetchXml = "<fetch><entity name='contact' >................</entity></fetch>";  
var layoutXml = "<grid name='resultset' object='1' jump='fullname' select='1' icon='1' preview='1'><row name='result' id='contactid'>......</row></grid>";
formContext.getControl(lookupFieldName).addCustomView(viewId, entityName, displayName, fetchXml, layoutXml, true);  

Later in the development stage, we have to add another lookup of the same entity to the form but with the different filter parameter.
Since the nature of the filter is similar, I just extract a function and pass different parameters for the different conditions for each lookup. During the function extraction, I just kept the static viewId and didn't try to make it dynamically but it was just working fine. (in the legacy web client)

When the same form is transitioned to Unified Interface, a user raised an issue that all of the lookups are showing the same recordset with the same filter. The issue was resolved with I generated the viewId dynamically whenever my lookup filter function is called.
generateGuid() function is from here.
var viewId = generateGuid();  
var entityName = "contact";  
var displayName = "Filtered Contacts";  
var fetchXml = "<fetch><entity name='contact' >................</entity></fetch>";  
var layoutXml = "<grid name='resultset' object='1' jump='fullname' select='1' icon='1' preview='1'><row name='result' id='contactid'>......</row></grid>";
formContext.getControl(lookupFieldName).addCustomView(viewId, entityName, displayName, fetchXml, layoutXml, true);

function generateGuid()  
{  
    return (this.S4() + this.S4() + "-" + this.S4() + "-4" + this.S4().substr(0,3) + "-" + this.S4() + "-" + this.S4() + this.S4() + this.S4()).toLowerCase();  
}
function S4()
{  
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);  
}  

In summary, same viewId in addCustomView for multiple controls of lookups is working in the legacy web client but it is not working well in Unified Interface (in which all lookup controls will show the same view)

Lesson learnt. Never use static values.

Comments

Popular Posts