Gravatar for corrac@paragon-inc.com

Question by Cris Corra, Oct 8, 2014 9:34 AM

In Coveo for Sitecore, how do I select an item in the list of a facet using a lookup field (foreign key).

Coveo for Sitecore 72 - Advanced Search Edition 3.0 (558) (July 2014 release). Coveo Enterprise Search 7.0 x64 (6830)

I have a facet using a foreign key setting. I need to select a value in the facet list. I tried using the "selectValue" method on the facet, passing the guid of the list item, or the lookup name of the list item, neither of which worked.

Cris Corra

Gravatar for cbolduc@coveo.com

Comment by Carl Bolduc, Oct 8, 2014 9:38 AM

Similar to this question, it's possible that your JavaScript UI needs an upgrade, validating.

Gravatar for corrac@paragon-inc.com

Comment by Cris Corra, Oct 8, 2014 9:52 AM

I don't think so, as I've not had issues selecting values in other facets using the selectValue method. The difference here is that this facet uses a foreignkey/lookup field. When I click a value in the facet, the url param are different than a regular facet:

sort=%40foriginalz32xpostedz32xdate29877 descending&f:ThemeFacet=[{1F02F990-45D0-463C-8360-E607348F93C4}en]&f:ThemeFacet:lookupvalues={"{1F02F990-45D0-463C-8360-E607348F93C4}en" : "Clinical Updates and Discoveries"}

2 Replies
Gravatar for olamothe@coveo.com

Answer by olamothe, Oct 15, 2014 10:16 AM

Hi Cris, sorry I couldn't take a look yesterday.

The problem is in your on buildingQuery function

.on("buildingQuery", function(e, args) {
                    var fval = getCoveoUrlParameterByName("theme");
                    if (fval != null && fval != "") {
                        //CAC: make sure theme facet is first (or change the order it is referenced here)
                        Coveo.$('.CoveoFacet').eq(1).coveo('selectValue', fval);
                    }
[...]


Place a breakpoint in there, i'm sure you'll see what is wrong with this. There is a couple problem with the logic,

First, you select the value from ?theme=, which is correct on the first load of the page. Then when the user deselect the value, you still try to reselect it by reading the value from ?theme=. Even if they just deselected it.

Also, the function getCoveoUrlParameterByName return the wrong value after the first load. Try to search only in location.search (return only the query string portion of your url) instead of the whole location.href ( query string + hash values added by the framework ). That's what is causing the selected value to have a weird sort parameter

You might be able to easily accomplish what you want be generating your url so that the frameworks takes care of selecting your facet value for you, and remove the code that automatically select the value

http://stage.acc.org/latest-in-cardiology#f:@ftheme51364=[%7B1F02F990-45D0-463C-8360-E607348F93C4%7Den]&f:@ftheme51364:lookupvalues={"%7B1F02F990-45D0-463C-8360-E607348F93C4%7Den" : "Clinical%20Updates%20and%20Discoveries"} 



Hope that helps you debugging. !

Gravatar for corrac@paragon-inc.com

Comment by Cris Corra, Oct 16, 2014 2:25 PM

That makes sense..thank you for your help! The generated url is not an option as this is a link in Sitecore and it only allows typical query string params (it doesn't understand the # query string indicator). I will play with removing that value in the url once the page is loaded? Not sure if that will work.

Gravatar for olamothe@coveo.com

Comment by olamothe, Oct 16, 2014 2:30 PM

Or, you can only bind/execute the function that read that parameter once (on the first initial loading of the page), instead of every time a query is executed.

An easy way is to just use a simple boolean flag that changes it's value after the first query is executed. You could also use JQuery.one() to achieve that http://api.jquery.com/one/

Gravatar for olamothe@coveo.com

Answer by olamothe, Oct 8, 2014 10:15 AM

Hi,

How do you call the selectValue function ? Do you pass only the GUID of the complete string ?

It should be something like this :

      $('#facet').coveo("selectValue", "{1F02F990-45D0-463C-8360-E607348F93C4}en");

When you call this, in the browser console you should see stuff like :

     ["INFO", Facet, "Selecting facet value", FacetValueElement] 



If the facet value is not visible when you call the function (because it's not in the current list of facet value being displayed) , you'll need to trigger a query before it is rendered in your facet. You can do this programmatically like this :

$("#search").coveo("executeQuery")



If you pass in the wrong value to the select function (for example, juste the GUID), it should still be selected, and rendered after you trigger a query. It just won't have any impact on the query (since it is not a valid facet value in your index). The count value would also be invisible (since it is effectively 0)

Gravatar for corrac@paragon-inc.com

Comment by Cris Corra, Oct 8, 2014 11:06 AM

I did try it this way: $('#facet').coveo("selectValue", "{1F02F990-45D0-463C-8360-E607348F93C4}en"); Which did select the value and filter the query. However, when I tried to unselect the item (by clicking) it treated it as a select. Then if I clicked it again, a new item with the guid showed up in the facet. http://cl.ly/image/0b2M010c2n1M/Windows7VSNET2013.png

Gravatar for olamothe@coveo.com

Comment by olamothe, Oct 8, 2014 4:17 PM

Yes, this is weird indeed. As you can see, the content of the facet is the guid itself, plus some kind of sort parameters that seems to come from the # of the url. It should be working correctly using the method I described. Lookup value and "normal" facet value are not very different. It's pretty hard to know why exactly it's not behaving for you, as it heavily depends on your implementation.

Is there any public page I can access where you have your stuff deployed ? I could probably look into the JS code and find out why it's doing that pretty quickly.

Gravatar for corrac@paragon-inc.com

Comment by Cris Corra, Oct 9, 2014 11:05 AM

We won't have a public page ready with the code until Monday. I will update this post when it is available for you to debug. Thanks!

Gravatar for corrac@paragon-inc.com

Comment by Cris Corra, Oct 13, 2014 11:37 AM

Here is the url you can test with:

http://stage.acc.org/latest-in-cardiology?theme={1F02F990-45D0-463C-8360-E607348F93C4}en

Thanks! Cris

Gravatar for corrac@paragon-inc.com

Comment by Cris Corra, Oct 15, 2014 9:46 AM

Hello. Please let me know if you've gotten a chance to work on this. If I don't hear from you or can't get a resolution soon, I will need to submit this as a support ticket.

Thanks! Cris

Ask a question