+ Start a Discussion
GGill31GGill31 

Unexpected token in SOQL query

Hi there,

What can be possible problem in the following code ? Its giving me unexpected token on barcodeID.
My requirement is to fetch the record from Item custom object on the basis of the Barcode_ID and populate the Borrow custom object record using the information from Item object. Borrow object has lookup relationship with Item object. The values for barcode_id and employee_id are coming from the visualforce page.

public class BorrowController {
    public String employeeID {get; set;}
    public String barcodeID {get; set;}
    
    public BorrowController(){}
    
    public void createBorrowRecord() {
        Item__c item = [SELECT Name, Item_ID__c, Price__c, Status__c from Item__c where Barcode_ID__c = barcodeID];
        if(item != null) {
            Borrow__c b = new Borrow__c();
            b.Employee_ID__c = employeeID;
            b.Item_ID__c = item.Item_ID__c;
            b.Barcode_ID__c = barcodeID;
            b.Item_Price__c = item.Price__c;
        
            b.insert;
        }
        
    }
    
    public PageReference recordCreation() {
        createBorrowRecord();
    }
}

Any help would be greatly appreciated!
Best Answer chosen by GGill31
KrishnaAvvaKrishnaAvva
Hi,
Change the query to
[SELECT Name, Item_ID__c, Price__c, Status__c from Item__c where Barcode_ID__c =: barcodeID];
I think you missed the ":".

Regards,
Krishna Avva

All Answers

KrishnaAvvaKrishnaAvva
Hi,
Change the query to
[SELECT Name, Item_ID__c, Price__c, Status__c from Item__c where Barcode_ID__c =: barcodeID];
I think you missed the ":".

Regards,
Krishna Avva
This was selected as the best answer
GGill31GGill31
Thank you Krishna Avva. I further have an issue where in I'm trying to insert an autonumber type Employee ID into Borrow Object which has lookup relation with Employee object.
This is what i'm trying to do. Its giving me a 'StringException Id: E-0001'. I have this autonumber employee id in my employee records.
Item__c i = [SELECT Name, Item_ID__c, Price__c, Status__c from Item__c where Barcode_ID__c =: 'K0L7N'];
        Borrow__c b = new Borrow__c();
        b.Employee_ID__c = 'E-0001';
        b.Item_ID__c = i.Item_ID__c;
        b.Barcode_ID__c = 'K0L7N';
        b.Item_Price__c = i.Price__c;
insert b;
GGill31GGill31
And its giving me the same error for  b.Item_ID__c = i.item_ID__c;
Item_ID is also an autonumber field in Item Object and lookup field in Borrow object.
Aslam iqbal - SoqlAslam iqbal - Soql
first issue, I can see is error in the select SOQL statement
you don't need colon (:) for values, only used for variables, such as below
string barcodeID='K0L7N';
Item__c i = [SELECT Name, Item_ID__c, Price__c, Status__c from Item__c where Barcode_ID__c =:barcodeID];

try below
Item__c i = [SELECT Name, Item_ID__c, Price__c, Status__c from Item__c where Barcode_ID__c = 'K0L7N' limit 1];
        Borrow__c b = new Borrow__c();
        b.Employee_ID__c = 'E-0001';
        b.Item_ID__c = i.Item_ID__c;
        b.Barcode_ID__c = 'K0L7N';
        b.Item_Price__c = i.Price__c;
insert b;

is there is an error again, this could be data types