Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Filtering sub grid base on lookup - javascript

(0) ShareShare
ReportReport
Posted on by 2,001

HI,

I have a lead from. Lead form has a field name customer(which is account entity). Account has a relationship with custom entity- Project.

Now  i have a grid on lead form of project entity. Based on customer lookup, i need to filter the grid.

For which i have wrote a code java script but it is not working as expected.

P.S. Crm version 2016 update 1.

Code snippet:

function filterProjectGridOnCustomer()
{
debugger;
//var grid =Xrm.Page.ui.controls.get('PastProjects').control;
var grid =window.parent.document.getElementById('PastProjects');
//var grid=Xrm.Page.ui.controls.get('PastProjects');
var Grid=Xrm.Page.getControl('PastProjects');
Grid=Grid.getGrid();

var lookupfield = new Array;

//Get the lookup field

lookupfield = Xrm.Page.getAttribute('customerid').getValue();

if (lookupfield != null) {

var lookupid = lookupfield[0].id;


}
else {return;}

if (grid ==null){
/*This statement is used to wait for 2 seconds and recall the function until the grid is loaded.*/
setTimeout('filterProjectGridOnCustomer()', 2000);
return;
}


var fetchXMl = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
fetchXMl +="<entity name='msdyn_project'>";
fetchXMl +="<attribute name='msdyn_projectid' />";
fetchXMl +="<attribute name='msdyn_subject' />";
fetchXMl +="<attribute name='new_totalprojectcostcr' />";
fetchXMl +="<attribute name='new_outstandingamountcr' />";
fetchXMl +="<attribute name='new_location' />";
fetchXMl +="<attribute name='new_invoicecollectedcr' />";
fetchXMl +="<attribute name='new_invoicebilledcr' />";
fetchXMl +="<link-entity name='account' from='accountid' to='msdyn_customer' alias='ad'>";
fetchXMl +="<filter type='and'>";
fetchXMl +="<condition attribute='accountid' operator='eq' uiname=' ' uitype='account' value='"+lookupid +"' />";
fetchXMl +="</filter>";
fetchXMl +="</link-entity>";
fetchXMl +="</entity>";
fetchXMl +="</fetch>";


grid.control.SetParameter("fetchXml", fetchXMl );
grid.control.refresh();

}

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: Filtering sub grid base on lookup - javascript

    Hello Abhishek,

    This type of requirement I would suggest to create custom HTML webresource and implement jquery easy UI or kendo grid.

    Unfortunately there is no supported way you can filter subgrid dynamically.

  • Suggested answer
    Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: Filtering sub grid base on lookup - javascript

    Hi Abhishek,

    Using set parameter is an unsupported way.

    I had a similar requirement to this ,we achieved it by writing retrieve multiple plugin .

    In your case follow below steps:

    1)Create a view on project entity and in filter criteria add description = 'CustomViewProject'

    2)Create a retrieve multiple plugin on pre operation of project 

    3)Obtain the query from context

    4)Check if the filter criteria contains the above condition

    4)If yes,Get the leadid from filter criteria of query and remove condition which we added in step 1

    5)Get customer lookup value using leadid 

    6)Add a filter condition in query with step 5 values

    7)Set the query to context

    When your code will execute you will get the results filtered on your lookup value.

    Hope it helps.

    -Shaminder

  • RE: Filtering sub grid base on lookup - javascript

    Am using D365 CRM and need to filter subgrid  based on a lookup value.

    Is there any supported way? without using dom.

  • Suggested answer
    BharatPremji Profile Picture
    BharatPremji 2,485 on at
    RE: Filtering sub grid base on lookup - javascript

    Hi Abhishek,

    Have a look at this thread, it may give you some pointers:

    https://community.dynamics.com/crm/f/117/p/208890/547980#547980

    Bharat

  • Abhishek Gupta Profile Picture
    Abhishek Gupta 2,001 on at
    RE: Filtering sub grid base on lookup - javascript

    Hi,

    I tried all these JavaScript. Found on some blogs that these java Script was working well till CRM version 2015 update 1.

  • Suggested answer
    PS Profile Picture
    PS 23,577 on at

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,407 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans