5

I need to hide a few accounts that start with certain numbers in the CustomerMasterID__c field on the account. For example, I need to hide accounts that start with 4504 in their CustomerMasterID field. I have a code that works at the moment, but doesn't work when it comes to hiding the account. I thought this would work:

soql = 'select name, recordtype.name, Status_Flag__c, CustomerMasterId__c, Owner.Name,     ShippingStreet, ShippingCity, ShippingState, ShippingCountry, parent.name, (Select model_Code__c, id FROM R00N70000001hzZ0EAI__r order by Model_Code__c) from account where name !=null AND NOT customermasterid__c like '4504*'';

However, this soql statement keeps coming up with error messages stating that I cannot use '%'

public PageReference runSearch() {

  String name = Apexpages.currentPage().getParameters().get('name');
  String recordtype = Apexpages.currentPage().getParameters().get('recordtype');
  String statusflag = Apexpages.currentPage().getParameters().get('statusflag');
  String MasterId = Apexpages.currentPage().getParameters().get('MasterId');
  String owner = Apexpages.currentPage().getParameters().get('owner');
  String street = Apexpages.currentPage().getParameters().get('street');
  String city = Apexpages.currentPage().getParameters().get('city');
  String state = Apexpages.currentPage().getParameters().get('state');
  String country = Apexpages.currentPage().getParameters().get('country');
  String parent = Apexpages.currentPage().getParameters().get('parent');
  String installedproducts =     Apexpages.currentPage().getParameters().get('installedproducts');

 soql = 'select name, recordtype.name, Status_Flag__c, CustomerMasterId__c, Owner.Name, ShippingStreet, ShippingCity, ShippingState, ShippingCountry, parent.name, (Select model_Code__c, id FROM R00N70000001hzZ0EAI__r order by Model_Code__c) from account where name !=null AND NOT customermasterid__c like '4504*'';
  if (!name.equals(''))
    soql += ' and name LIKE \'%'+String.escapeSingleQuotes(name)+'%\'';
  if (!recordtype.equals(''))
    soql += ' and recordtype.name LIKE \''+String.escapeSingleQuotes(recordtype)+'%\'';
  if (!statusflag.equals(''))
    soql += ' and Status_Flag__c LIKE \''+String.escapeSingleQuotes(statusflag)+'%\'';     
  if (!MasterId.equals(''))
    soql += ' and CustomerMasterId__c LIKE \''+String.escapeSingleQuotes(MasterId)+'%\'';  
  if (!Owner.equals(''))
    soql += ' and owner.name LIKE \''+String.escapeSingleQuotes(owner)+'%\'';        
  if (!street.equals(''))
    soql += ' and ShippingStreet LIKE \''+String.escapeSingleQuotes(street)+'%\'';      
  if (!city.equals(''))
    soql += ' and ShippingCity LIKE \''+String.escapeSingleQuotes(city)+'%\'';
  if (!state.equals(''))
    soql += ' and ShippingState LIKE \''+String.escapeSingleQuotes(state)+'%\'';
  if (!country.equals(''))
    soql += ' and ShippingCountry LIKE \''+String.escapeSingleQuotes(country)+'%\'';  
  if (!parent.equals(''))
    soql += ' and parent.name LIKE \'%'+String.escapeSingleQuotes(parent)+'%\''; 
  if (!installedproducts.equals(''))
    soql += ' and R00N70000001hzZ0EAI__r.Account.model_Code__c LIKE \''+String.escapeSingleQuotes(installedproducts)+'%\'';   

2 Answers 2

12

Use '(NOT <field name> LIKE '4504%'). Also, not related to your question but you can improve your query by removing that unnecessary name!=null, the account name cannot be null anyway.

3
  • I keep getting this error: Error: AccountSearchControllerOriginal Compile Error: line 93:297 no viable alternative at character '%' at line 93 column 297 Commented May 12, 2014 at 20:31
  • You need to escape the single quotes like this: NOT LIKE \'4504%\'' Commented May 12, 2014 at 20:39
  • 1
    Thanks Mark! That helped a lot. I also did some research and I had to put parentheses around it so it ended up working like this: (Not customermasterid__c LIKE \'4504%\') Commented May 13, 2014 at 13:29
-2

Try adding NOT before the like as I show below:

AND NOT RecordType.Name LIKE
1
  • 1
    It needs parentheses around the NOT Clause. AND (NOT RecordType.Name LIKE 'x') Commented Sep 28, 2017 at 20:37

You must log in to answer this question.

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