Select to view content in your preferred language

query.where clause using variable for field name instead of hard coded

1793
6
Jump to solution
10-11-2013 11:14 AM
TracySchloss
Honored Contributor
I'm sure if it wasn't Friday afternoon, I'd figure this out on my own.

I am trying to set up a more generic function to construct and execute a query task.   Instead of having my query.where clause be something like
selQuery.where = "DISTRICT= '" + myCode + "'";  //myCode coming from a FilteringSelect value


I'd like the field name to also be a variable.  But I think the syntax for the where clause needs to be in the form of "FIELDNAME" = 'myValue', and I'm not quite sure how to get the quotes properly formatted in the string. 

Please someone who's brain is still functioning, help me out!
0 Kudos
1 Solution

Accepted Solutions
ChristopherBlinn1
Deactivated User
Tracy,

Just make sure your field variable is a string.  There where property is type string..

var myField = "DISTRICT"; . . . selQuery.where = myField + " = '" + myCode + "'"


I actually build the entire where statement in a string variable in most of my applications, then set .where to the variable.

Hope this helps and Happy Friday,
Chris B.

View solution in original post

0 Kudos
6 Replies
ChristopherBlinn1
Deactivated User
Tracy,

Just make sure your field variable is a string.  There where property is type string..

var myField = "DISTRICT"; . . . selQuery.where = myField + " = '" + myCode + "'"


I actually build the entire where statement in a string variable in most of my applications, then set .where to the variable.

Hope this helps and Happy Friday,
Chris B.
0 Kudos
TracySchloss
Honored Contributor
I tried that first, before I sent this and it wasn't working.  I've changed several places in this function to use variables instead, maybe I should be expand to other lines.  This might not be the problem at all!

Like I said - Friday - brain dead.
0 Kudos
ChristopherBlinn1
Deactivated User
If you would like to post your entire function, I'd gladly take a look at it.

Or if your application is publicly accessible I can look from there.
0 Kudos
TracySchloss
Honored Contributor
Thanks for the offer.  I have a feeling it's something right in front of me, so I'm going to try  a little longer.  No sense in embarrassing myself.
0 Kudos
TracySchloss
Honored Contributor
Sure enough, I had renamed my errorHandler function, but forgot to change the queryTask.on event listener
 queryTask.on('error', populateErrorHandler); 
0 Kudos
ChristopherBlinn1
Deactivated User
Glad you found it!
0 Kudos