+ Start a Discussion
Vigneshwaran LoganathanVigneshwaran Loganathan 

System.QueryException: expecting a colon, found 'Paid'

Hi,

I got a task like, I need to sort a field from the table.. written the Dynamic query for it and faced the issue that i mentioned,

Error is in For Loop
 
public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
    }
    public String sortField {
    get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
    set;
    }
  
 // toggles the sorting of query from asc<-->desc
    public void toggleSort() {
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query again
    getpayslip();}

public List<payslipwrapper> getpayslip()
    {
        Payslip__c myVariable = new Payslip__c(Payment_Status__c ='Paid');
        string status = myVariable.Payment_Status__c;
        string payrolldate = payment2.payroll_cycle_date__c;
        string year = payment2.payroll_cycle_year__c;
        soql='select id,Name,Name__c,PaymentStatus__c,Account_Number__c,Billing_Specialist__c,PayRoll_Cycle__c,Year__c,Total_Pay_for_this_period__c,Total_Pay_in_Peso__c,Payment_Status__c from Payslip__c where PaymentStatus__c !=';       
        for(payslip__c pay : Database.query(soql + status + ' and PayRoll_Cycle__c=:'+ payrolldate + ' and Year__c=:'+ year +' order by ' + sortField + ' ' + sortDir + ' Limit 999')) // Error At this  Line 
		 payslipList.add(new payslipwrapper(pay)); 
        if(payslipList.size()== 0)
        {
            // some codes
        }   
        return payslipList;
    }
 

 
Best Answer chosen by Vigneshwaran Loganathan
KaranrajKaranraj
Try with the below lines of code, you can directly append the variable in the query string
soql='select id,Name,Name__c,PaymentStatus__c,Account_Number__c,Billing_Specialist__c,PayRoll_Cycle__c,Year__c,Total_Pay_for_this_period__c,Total_Pay_in_Peso__c,Payment_Status__c from Payslip__c where PaymentStatus__c !=:
status and PayRoll_Cycle__c =: payrolldate and Year__c=: year';      
  for(payslip__c pay : Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' Limit 999')) {

}

 

All Answers

KaranrajKaranraj
Try with the below lines of code, you can directly append the variable in the query string
soql='select id,Name,Name__c,PaymentStatus__c,Account_Number__c,Billing_Specialist__c,PayRoll_Cycle__c,Year__c,Total_Pay_for_this_period__c,Total_Pay_in_Peso__c,Payment_Status__c from Payslip__c where PaymentStatus__c !=:
status and PayRoll_Cycle__c =: payrolldate and Year__c=: year';      
  for(payslip__c pay : Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' Limit 999')) {

}

 
This was selected as the best answer
Vigneshwaran LoganathanVigneshwaran Loganathan
Hi karan,

You are simply Great :) was stucking with this error for more than an hour :) 

really appreciate. Thsnks