0

Getting this annoying error when trying to save my code. I've been doing some and research and people have suggested deleting and rewriting but that doesn't seem to work either.

Any suggestions would be great and I'm going mad!

public class ContactSearchController {

private String soql {get;set;}

public LIST<Contact> contacts {get;set;}

public String sortDir {
get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
set;
}

public String sortField {
  get { if (sortField == null) {sortField = 'lastName'; } return sortField;  }
set;
}

public String debugSoql {
get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
set;
}

public ContactSearchController() {
 soql = 'select firstName, lastName, account.name, TUQ_Score__c, Pulse_Category__c,   Date_of_Last_Pulse_Score_1__c, How_could_we_get_you_to_a_10_TUQ__c  from contact where  account.name != null';
 runQuery();
  }

public void toggleSort() {
sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
runQuery();
}

public void runQuery() {

try {
  contacts = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
} catch (Exception e) {
  ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
}

}

 public PageReference runSearch() {

String firstName = Apexpages.currentPage().getParameters().get('firstName');
String lastName = Apexpages.currentPage().getParameters().get('lastName');
String accountName = Apexpages.currentPage().getParameters().get('accountName');
String TUQScore = Apexpages.currentPage().getParameters().get('TUQ_Score__c');
String PulseCategory = Apexpages.currentPage().getParameters().get('Pulse_Category__c');
String DateofLastPulseScore = Apexpages.currentPage().getParameters().get('Date_of_Last_Pulse_Score_1__c');
String Howcouldwegetyoutoa10 = Apexpages.currentPage().getParameters().get('How_could_we_get_you_to_a_10_TUQ__c');

soql = 'select firstName, lastName, account.name, TUQ_Score__c, Pulse_Category__c, Date_of_Last_Pulse_Score_1__c, How_could_we_get_you_to_a_10_TUQ__c from contact where account.name != null';
if (!firstName.equals(''))
  soql += ' and firstName LIKE ''+String.escapeSingleQuotes(firstName)+'%'';
if (!lastName.equals(''))
  soql += ' and lastName LIKE ''+String.escapeSingleQuotes(lastName)+ '%'';
if (!accountName.equals(''))
  soql += ' and account.name LIKE ''+String.escapeSingleQuotes(accountName)+ '%'';
if (!PulseCategory.equals(''))
  soql += ' and Pulse_Category__c includes (''+Pulse+'')';
renQuery();

return nill;
}

public List<String> PulseCategory {
get {
  if (PulseCategory == null) {

    PulseCategory = new List<String>();
    Schema.DescribeFieldResult field = Contact.Pulse_Category__c.getDescribe();

    for (Schema.PicklistEntry f : field.getPicklistValues())
      PulseCategory.add(f.getLabel());

  }
  return PulseCategory;          
}
set;
}

}
1
  • Don't use equals('') Instead use a String.isNotBlank(yourVariable) method of the String class. Dec 10, 2014 at 13:59

2 Answers 2

4

I presume this is the line below?

soql += ' and firstName LIKE ''+String.escapeSingleQuotes(firstName)+'%'';

Try changing it (and the other lines using %) to:

soql += ' and firstName LIKE \''+String.escapeSingleQuotes(name)+'%\'';
2
  • Amazing! Thanks... I'm reasonably new to this so could you explain what and why you changed what you did? Dec 10, 2014 at 14:28
  • 2
    As Mast0r mentions in his answer you have to mask/escape the apostrophe
    – Girbot
    Dec 10, 2014 at 14:35
3

The Problen here is that you need to mask an apostrophe in the query string:

soql += ' and account.name LIKE \''+String.escapeSingleQuotes(accountName)+ '%\'';

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .