Gravatar for carlos_dejesus@ultimatesoftware.com

Question by cdejesusdev, Jan 27, 2017 2:27 PM

How to pass form element values into "buildingQuery" event

Hi All,

I have added an event to the apex:SelectList to call on a Javascript function whenever the value is changed. How can I pass the value of the select1 picklist into the Coveo query and then execute the query.

Here is the javascript function:

function executeSearch() {
    console.log('::::::::::::Inside of executeSearch::::::::::::');
    var root = document.body;
    var select1 = j$('[id*=qWhat]').val();
    console.log('The value for select1: ' + select1);
    //I should be able to do something here to append the value of select1 to query before I execute it. 
    Coveo.executeQuery(root);
}
1 Reply
Gravatar for olamothe@coveo.com

Answer by olamothe, Jan 27, 2017 2:34 PM

Hi, I would invite you to either read or,(if you are really interested in learning more) doing the tutorial to get a basic grasp on the concepts related to the Coveo UI.

https://developers.coveo.com/display/JsSearchV1/JavaScript+Search+Framework+V1+Getting+Started+Tutorial

For your question in particular, for example, you would get your answer on Step 4 :

https://developers.coveo.com/display/JsSearchV1/Step+4+-+Modifying+the+Query

So, for your precise use case, you could read the value of your input inside the "buildingQuery" function that you hook, and append it to the query.

Even though you are inside a salesforce context, the basic concepts of the Coveo UI still applies.

Hope this helps you start !

Gravatar for carlos_dejesus@ultimatesoftware.com

Comment by cdejesusdev, Jan 27, 2017 3:07 PM

I've added the following before calling the Coveo.executeQuery(root);

Coveo.$$(root).on('buildingQuery', function(e, args) 
{
    console.log('::::: buildingQuery on executeSearch :::::');

    var select1 = document.getElementById('qWhat');
    console.log('what', select1.value);
    args.queryBuilder.expression.add(select1.value);
})

The problem now is that select1 is always null. this is because it cannot find the element with id = qWhat, since by the time it searches for it, it already has appended jd0.jd1 etc to the ID.

if I try to use JQuery to do a partial name search, then the problem becomes that j$ is not defined.

this only happens if I do it within the buildingQuery function. If I do it outside, I have no issues getting the values of the input.

Gravatar for carlos_dejesus@ultimatesoftware.com

Comment by cdejesusdev, Jan 27, 2017 3:20 PM

and we are using Coveo V2.

Gravatar for maveilleux@coveo.com

Comment by maveilleux, Jan 27, 2017 3:47 PM

Maybe you could use debugger; to figure out the selector to use. Here's the Salesforce documentation about it: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pagesbestpracticesaccessingid.htm

Ask a question