Problem: I came across situation where I need to create a default custom view when the page is loaded for a lookup field. Along with that I also didn’t wanted to disable the view selector option so user cannot select any other view except my custom view.
To achieve this target I knew that we need a javascript that executes when the form is loaded.
1. First step is to
create a web resource that can hold this javascript. (click here learn how tocreate a web resource)
2. Once your web
resource is ready we need to form the fetchxml query that grabs the required
data based on our requirement.
3. So you can simply
go to advanced finds and form the query logic to grab your data. Suppose you want
all grab all opportunities linked with specific account for the enity. (This is
pretty simple task custom views are used for more complex task).
4. Once the query is
form hit the “Download fetchxml” button on top ribbon in Advanced Finds. This
will give you a fetchxml of the query. You need to copy this in your javascript
code.
5. Next step is to
form the layoutxml , this determines which columns from the entity is display
in your lookup result. Finally your code will look like this.
function LoadCustomView()
{
//get the guid of account
var acctId = Xrm.Page.getAttribute("lookupfieldnameforAccount").getValue()[0].id;
//Fetch xml from the advanced find. Need to format this
var fetchxml=
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='opportunity'>" +
"<attribute name='name' />" +
"<attribute name='customerid' />" +
"<attribute name='opportunityid' />" +
"<order attribute='customerid' descending='false' />" +
"<link-entity name='account' from='accountid' to='customerid' alias='aa'>" +
"<filter type='and'>" +
"<condition attribute='accountnumber' operator='eq' value='" + acctId + "' />" +
"</filter>"+
"</link-entity>" +
"</entity>" +
"</fetch>"
//Layout xml shows that row and column structure in view
var layoutXml =
"<grid name='opportunities' object='10017' jump='name' select='1' icon='1' preview='1'>" +
"<row name='opportunity' id='opportunityid'>" +
"<cell name='name' width='200' />" +
"<cell name='customerid' width='200' />" +
"</row>" +
"</grid>";
var viewId = "{00000000-0000-0000-0000-000000000000}";
var entityName = "opportunity";
var viewDisplayName = "Any name you want to show up";
//pass all parameters to function and last value true makes this view as default view.
Xrm.Page.getControl('lookupfieldnameforopp').addCustomView
(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
}
· So first I grab the account id and use that in Fetchxml to grab all opportunities associated with
that account.
· Make sure this lookup field is set properly on form. The view selector should show "show all
views".
6. Now you want to disable the view selector panel so that user cannot select any other view that except the default custom view you provide. For that you need to add following line after your addcustomview line.
document.getElementById("lookfieldforopp").setAttribute("disableViewPicker","1");
7. Now you can save and publish the web resource and check the out your view by opening up the page.
Hope this helps,
Thanks
No comments:
Post a Comment