Thursday, July 21, 2011

Filtered Lookup onclick of a pickList(Optionset) MS CRM 2011

Hi, all we know that the beautiful feature in MS CRM 2011, has given the filtered lookup.
this u can do as in figure
•Selecting which view to use to display possible Lookup selections, as well as controlling which Views the CRM User is allowed to select from.
•Selecting a filter to show only records that match a certain dynamic criteria, with the option to control whether the User is allowed to remove the Filter or not.
This allows us to control how the Lookup will appear in CRM 2011 through simple customisation, and so avoid developing any code to produce a Filtered Lookup.

While creating the lookup, by configuring it, we can get the filtered values.
Here i am going to put, how can i get the filtered lookup values on basis of a value from the slected picklist(optionset).
IDs i have used here, so u need to have these things in your hand

Parent Entity : new_parententity
optionsetname : new_optionset
lookupattribute : new_childenitytid (this is in the parent enity, this will be present in the form, but can be hidden)

lookup entity : new_childentity
id: new_childentityid
objecttype : , this u can get, just open a recird and see the etc in the url
list the columns u want: new_column1,new_column2,createdon
condtional attribute : new_conditionaloptionset

now onchange of your picklist call the following function

function onchange_new_optionset(){
//get the optionset value and index
var name = 1;//get the value
var value = 'values of the selected optionset';
_setLookup(name, "ihis_polyclinicid", value);
document.getElementById("ihis_polyclinicid").click(); // this is tricky part, onchange of teh optioset, fire the click event of the lookup filed which you have hided earlier
}

// now this is the main function
function _setLookup(typeName, lookupFieldName, typeValue) {
var defaultViewId = Xrm.Page.getControl(lookupFieldName).getDefaultView();

if (typeName != null) {
// use randomly generated GUID Id for our new view
var viewId = "{1DFB2B35-B07C-44D1-868D-258DEEAB88E2}";
var entityName = "new_childentity";

// give the custom view a name
var viewDisplayName = "Polyclinic Directory for " + typeValue + "";

var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='new_childentity'>" +
"<attribute name='new_column1' />" +
"<attribute name='new_column2' />" +
"<attribute name='createdon' />" +
"<order attribute='new_column1' descending='false' />" +
"<filter type='and'>" +
"<condition attribute='new_conditionaloptionset' operator='eq' value='" + typeName + "' />" +
"<condition attribute='statecode' operator='eq' value='0' />" +
"</filter>" +
"</entity>" +
"</fetch>";

// build Grid Layout
var layoutXml = "<grid name='resultset' " +
"object='object_type' " +
"jump='new_childentityid' " +
"select='1' " +
"icon='1' " +
"preview='1'>" +
"<row name='result' " +
"id='new_childentityid'>" +
"<cell name='new_column1' " +
"width='200' />" +
"<cell name='new_column2' " +
"width='250' />" +
"<cell name='createdon' " +
"width='250' />" +
"</row>" +
"</grid>";

// 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);
}
}

No comments:

Post a Comment