Dynamic Filtered Views in CRM2011
If you want to create a dynamic filterd lookup in crm2011
Ex:- If you have a account lookup & a contact lookup in a form & you want to filter & view the only contacts who's parentcustomer is equal to the selected account.
You can use the following
var defaultViewId;
// FUNCTION: formOnLoad
function formOnLoad() {
var lookupFieldName = 'new_contactid';
var accountFieldName = 'new_accountid';
defaultViewId = Xrm.Page.getControl(accountFieldName).getDefaultView();
setLookup(accountFieldName, lookupFieldName, false);
}
// FUNCTION: setLookup
function setLookup(accountFieldName, lookupFieldName, resetSelection) {
// Get the selected Account Id in the [accountFieldName] indicated control
var account = Xrm.Page.getAttribute(accountFieldName).getValue();
if (account != null) {
var accountid = account[0].id;
var accountname = account[0].name;
if (resetSelection == true) {
// reset old selection for Contact
Xrm.Page.getAttribute(lookupFieldName).setValue(null);
}
// use randomly generated GUID Id for our new view
var viewId = "{"+ guidGenerator() +"}";
var entityName = "contact";
// give the custom view a name
var viewDisplayName = "Active Contacts for " + accountname + "";
// find all contacts where [Parent Customer] = [Account indicated by AccountId]
// AND where [Statecode] = Active
var fetchXml = "" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
// build Grid Layout
var layoutXml = "
"object='1' " +
"jump='contactid' " +
"select='1' " +
"icon='1' " +
"preview='1'>" +
"
"id='contactid'>" +
" "width='200' />" + " "width='250' />" + " "width='250' />" + " | | |
" +
" ";
// add the Custom View to the indicated [lookupFieldName] Control
Xrm.Page.getControl(lookupFieldName).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
}
else {
// no Account selected, reset Contact Lookup View to the default view such that all Contacts are displayed for selection
Xrm.Page.getControl(lookupFieldName).setDefaultView(defaultViewId);
}
}
function guidGenerator() {
var S4 = function () {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
Ex:- If you have a account lookup & a contact lookup in a form & you want to filter & view the only contacts who's parentcustomer is equal to the selected account.
You can use the following
var defaultViewId;
// FUNCTION: formOnLoad
function formOnLoad() {
var lookupFieldName = 'new_contactid';
var accountFieldName = 'new_accountid';
defaultViewId = Xrm.Page.getControl(accountFieldName).getDefaultView();
setLookup(accountFieldName, lookupFieldName, false);
}
// FUNCTION: setLookup
function setLookup(accountFieldName, lookupFieldName, resetSelection) {
// Get the selected Account Id in the [accountFieldName] indicated control
var account = Xrm.Page.getAttribute(accountFieldName).getValue();
if (account != null) {
var accountid = account[0].id;
var accountname = account[0].name;
if (resetSelection == true) {
// reset old selection for Contact
Xrm.Page.getAttribute(lookupFieldName).setValue(null);
}
// use randomly generated GUID Id for our new view
var viewId = "{"+ guidGenerator() +"}";
var entityName = "contact";
// give the custom view a name
var viewDisplayName = "Active Contacts for " + accountname + "";
// find all contacts where [Parent Customer] = [Account indicated by AccountId]
// AND where [Statecode] = Active
var fetchXml = "
"
"
"
"
"
"
"
"
"" +
"" +
"";
// build Grid Layout
var layoutXml = "
"object='1' " +
"jump='contactid' " +
"select='1' " +
"icon='1' " +
"preview='1'>" +
"
"id='contactid'>" +
"
"width='200' />" +
"
"width='250' />" +
"
"width='250' />" +
"
"
// add the Custom View to the indicated [lookupFieldName] Control
Xrm.Page.getControl(lookupFieldName).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
}
else {
// no Account selected, reset Contact Lookup View to the default view such that all Contacts are displayed for selection
Xrm.Page.getControl(lookupFieldName).setDefaultView(defaultViewId);
}
}
function guidGenerator() {
var S4 = function () {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
Comments
Post a Comment