Answered by:
CRM 2013 - Javascript - Custom Filter for lookup

Question
-
Hi,
I have a custom Entity called "Issue"
On issue the user can input an account into a lookup field. From this I have another lookup field which is going to be used for the user to select a Order No, my issue is that I would like that lookup to only show records associated with the account previously selected.
I understand that a custom filter could be applied. I found the following code but had no luck
var accountID = Xrm.Page.getAttribute("new_companylookup"); var AccountIDObj= accountID.getValue(); if (AccountIDObj != null) { var disabled= Xrm.Page.ui.controls.get("new_companylookup"); disabled.setDisabled(true); var fetchFilter = '<filter type="and"><condition attribute="customerid" operator="eq" value="' + AccountIDObj + '" /></filter>'; Xrm.Page.getControl('new_ordernolookup').addCustomFilter(fetchFilter); }
Does anyone have any suggestions as to what I've done wrong or what I could try to make this work?
Thanks, Shaun
S.Harrison
Monday, July 28, 2014 7:45 AM
Answers
-
Hi
try by using " " in place of ' '
Xrm.Page.getControl("new_ordernolookup").addCustomFilter(fetchFilter);
and also use .preFilterSearch try below links..
http://blogs.msdn.com/b/shraddha_dhingra/archive/2013/12/21/addpresearch-and-addcustomfilter-in-crm-2013.aspx
http://www.magnetismsolutions.com/blog/nathaneccles/2013/09/30/crm-2013-javascript-lookup-filtering-using-addcustomfilter
- Edited by Suresh Sorde Monday, July 28, 2014 10:41 AM
- Marked as answer by Shaun Harrison Monday, July 28, 2014 11:11 AM
Monday, July 28, 2014 10:40 AM -
Hi Shaun,
I have updated the fetchXml, using the lookup type and lookup record name. Hopefully this should work.
var accountID = Xrm.Page.getAttribute("new_companylookup"); var AccountIDObj= accountID.getValue(); if (AccountIDObj != null) { var disabled= Xrm.Page.ui.controls.get("new_companylookup"); disabled.setDisabled(true); var fetchFilter = "<filter type='and'><condition attribute='customerid' uitype='" + AccountIDObj[0].entityType + "' operator='eq' value='" + AccountIDObj[0].id + "' uiname='" + AccountIDObj[0].name + "' /></filter>"; Xrm.Page.getControl('new_ordernolookup').addCustomFilter(fetchFilter); }
- Marked as answer by Shaun Harrison Monday, July 28, 2014 11:11 AM
Monday, July 28, 2014 10:56 AM -
Hi
Thank you both for the suggestions
It was a combination of both that finally got this working
The Code is as follows;
function accountorder() { Xrm.Page.getControl("new_ordernolookup").addPreSearch(function () { addlookup(); }); function addlookup() { var accountID = Xrm.Page.getAttribute("new_companylookup"); var AccountIDObj= accountID.getValue(); if (AccountIDObj != null) { var disabled= Xrm.Page.ui.controls.get("new_companylookup"); disabled.setDisabled(true); var fetchFilter = "<filter type='and'><condition attribute='customerid' uitype='" + AccountIDObj[0].entityType + "' operator='eq' value='" + AccountIDObj[0].id + "' uiname='" + AccountIDObj[0].name + "' /></filter>"; Xrm.Page.getControl("new_ordernolookup").addCustomFilter(fetchFilter); } } }
Thanks Again
Shaun
S.Harrison
- Marked as answer by Shaun Harrison Monday, July 28, 2014 11:11 AM
Monday, July 28, 2014 11:07 AM
All replies
-
Hi,
I believe the "new_companylookup" is a lookup field, so you'll need to get the Id. I have modified the code accordingly:
var accountID = Xrm.Page.getAttribute("new_companylookup"); var AccountIDObj= accountID.getValue(); if (AccountIDObj != null) { var disabled= Xrm.Page.ui.controls.get("new_companylookup"); disabled.setDisabled(true); var fetchFilter = "<filter type='and'><condition attribute='customerid' uitype='" + AccountIDObj[0].entityType + "' operator='eq' value='" + AccountIDObj[0].id + "' uiname='" + AccountIDObj[0].name + "' /></filter>"; Xrm.Page.getControl('new_ordernolookup').addCustomFilter(fetchFilter); }
Admin QuikView Solution for CRM 2013
- Proposed as answer by DynaDeveloper Monday, July 28, 2014 8:03 AM
- Edited by Anupam Bishui Monday, July 28, 2014 10:51 AM
Monday, July 28, 2014 7:57 AM -
Hi Dynamotion
I tried the changes you made but still no luck, Order lookup is still displaying all orders
Thanks, Shaun
S.Harrison
Monday, July 28, 2014 8:08 AM -
Hi Shaun,
You can also use OOB lookup filtering that CRM provides. Any reason for not using that?
Monday, July 28, 2014 8:12 AM -
Hi,
Haven't come across that method, I'll look into it now
Thank you
S.Harrison
Monday, July 28, 2014 8:13 AM -
Hi Shaun,
I have modified the fetchxml, there were some quote issues.
var accountID = Xrm.Page.getAttribute("new_companylookup"); var AccountIDObj= accountID.getValue(); if (AccountIDObj != null) { var disabled= Xrm.Page.ui.controls.get("new_companylookup"); disabled.setDisabled(true); var fetchFilter = "<filter type='and'><condition attribute='customerid' uitype='" + AccountIDObj[0].entityType + "' operator='eq' value='" + AccountIDObj[0].id + "' uiname='" + AccountIDObj[0].name + "' /></filter>"; Xrm.Page.getControl('new_ordernolookup').addCustomFilter(fetchFilter); }
Admin QuikView Solution for CRM 2013
- Edited by Anupam Bishui Monday, July 28, 2014 10:52 AM
Monday, July 28, 2014 8:23 AM -
Hi Dynamotion
Sorry got caught up doing other stuff
Still no luck with the code, still displays all orders in the lookup
Thanks, Shaun
S.Harrison
Monday, July 28, 2014 10:36 AM -
Hi
try by using " " in place of ' '
Xrm.Page.getControl("new_ordernolookup").addCustomFilter(fetchFilter);
and also use .preFilterSearch try below links..
http://blogs.msdn.com/b/shraddha_dhingra/archive/2013/12/21/addpresearch-and-addcustomfilter-in-crm-2013.aspx
http://www.magnetismsolutions.com/blog/nathaneccles/2013/09/30/crm-2013-javascript-lookup-filtering-using-addcustomfilter
- Edited by Suresh Sorde Monday, July 28, 2014 10:41 AM
- Marked as answer by Shaun Harrison Monday, July 28, 2014 11:11 AM
Monday, July 28, 2014 10:40 AM -
Hi Shaun,
I have updated the fetchXml, using the lookup type and lookup record name. Hopefully this should work.
var accountID = Xrm.Page.getAttribute("new_companylookup"); var AccountIDObj= accountID.getValue(); if (AccountIDObj != null) { var disabled= Xrm.Page.ui.controls.get("new_companylookup"); disabled.setDisabled(true); var fetchFilter = "<filter type='and'><condition attribute='customerid' uitype='" + AccountIDObj[0].entityType + "' operator='eq' value='" + AccountIDObj[0].id + "' uiname='" + AccountIDObj[0].name + "' /></filter>"; Xrm.Page.getControl('new_ordernolookup').addCustomFilter(fetchFilter); }
- Marked as answer by Shaun Harrison Monday, July 28, 2014 11:11 AM
Monday, July 28, 2014 10:56 AM -
Hi
Thank you both for the suggestions
It was a combination of both that finally got this working
The Code is as follows;
function accountorder() { Xrm.Page.getControl("new_ordernolookup").addPreSearch(function () { addlookup(); }); function addlookup() { var accountID = Xrm.Page.getAttribute("new_companylookup"); var AccountIDObj= accountID.getValue(); if (AccountIDObj != null) { var disabled= Xrm.Page.ui.controls.get("new_companylookup"); disabled.setDisabled(true); var fetchFilter = "<filter type='and'><condition attribute='customerid' uitype='" + AccountIDObj[0].entityType + "' operator='eq' value='" + AccountIDObj[0].id + "' uiname='" + AccountIDObj[0].name + "' /></filter>"; Xrm.Page.getControl("new_ordernolookup").addCustomFilter(fetchFilter); } } }
Thanks Again
Shaun
S.Harrison
- Marked as answer by Shaun Harrison Monday, July 28, 2014 11:11 AM
Monday, July 28, 2014 11:07 AM