0

I keep getting the error unexpected token 'skey' even though I have declared the variable.

public class skuControl{
 string skey{get;set;}

 public var skuController() {
    return [select ItemSKU__c from Product__c where Description__c LIKE skey};
         }
  }
0
  1. public var will not work. Its a wrong return type.
  2. [select ItemSKU__c from Product__c where Description__c LIKE 'skey'] Your brackets at the end are wrong
1
  • 3
    Quotation marks do not perform variable interpolation in SOQL. R83nLK82 would need to use Apex variable binding with :skey to achieve this, as in Enrik Hysko's answer.
    – David Reed
    Jun 28 '18 at 15:22
2

You have to use variable binding :

    return [select ItemSKU__c from Product__c where Description__c LIKE :skey];

Also var isn't a data type in Salesforce. If you want to return the Product__c record, then you have to return the 'Product__c' sObject type on your method, like:

// actually the query returns a list of Product__c 
public List<Product__c> doSomething(){
    return [SELECT ItemSKU__c from Product__c where Description__c LIKE :skey];
}

Seems that skuController() is the constructor of your Apex controller and the constructors shouldn't return anything. If that's the case, then on the constructor you can make initializing as below:

public class skuController {

    public List<Product__c> productList { get; set; }

    // controller
    public skuController() {
        productList =  [SELECT ItemSKU__c from Product__c where Description__c LIKE :skey];
    }
}

A better approach would be to use lazy loading if you are on Apex Visualforce controller:

public List<Product__c> getProducts() {
    get {
        if (productList == null) {
            productList =  [SELECT ItemSKU__c from Product__c where Description__c LIKE :skey];
        }
        return productList;
    }
    set;
}

And e.g. the VF page would be:

<apex:page controller="skuController">
    <apex:form >
        <apex:pageBlock>
            <apex:pageblockTable value="{!products}" var="p">
                <apex:column value="{!p.ItemSKU__c}"/>
            </apex:pageblockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.