+ Start a Discussion

need help: Pass parameters to query

Dear all,
I want to pass parameters(sobject name.queryfield) entered through input fields to the query.
i ttried the following code. But it's not working. error given is: Save error: unexpected token: :

public class ComponentController2 {
   ComponentController2 comCon; 
Opportunity opportunity;
OpportunityContactRole[] oppContactRole;
String oppContactName;
OpportunityContactRole contactRole;
String sobjectName;
String queryfield;

 public opportunity getOpportunity() {
        return opportunity;
    public OpportunityContactRole[] getoppContactRole() {         
        return oppContactRole;

public List<SelectOption> getItems() {
//    String queryString=' select ' + queryfield + ' from'+ sobjectName +'WHERE OpportunityId= :ApexPages.currentPage().getParameters().get(\'id\')' ;
//    if(sobjectName=='OpportunityContactRole'){
//    this.oppContactRole=[queryString];
   // this.oppContactRole = Database.query(queryString);
            this.oppContactRole = [SELECT :comCon.queryfield FROM OpportunityContactRole
            OpportunityId= :ApexPages.currentPage().getParameters().get('id') ORDER BY IsPrimary DESC, CreatedDate ASC LIMIT 3];           
        }catch (QueryException e){
List<SelectOption> options = new List<SelectOption>();
for(OpportunityContactRole con:oppContactRole){
    //String  s='';
String s=con.Contact.Name;
options.add(new SelectOption(con.Id,s));
return options;

public void setSobjectName(String sobjectName){

public String getSobjectName(){
return sobjectName;

public void setQueryfield(String queryfield){

public String getQueryfield(){
return queryfield;

public String getoppContactName() {
return oppContactName;

public void setoppContactName(String oppContactName) {
this.oppContactName = oppContactName;


pls help me to solve this problem.

Thanks in advance!

Ron HessRon Hess
you cannot use ':' in any part of the SOQL except the where clause

Message Edited by Ron Hess on 08-19-2008 08:55 AM