Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)+'%\'';   
share|improve this question
up vote 10 down vote accepted

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.

share|improve this answer
    
I keep getting this error: Error: AccountSearchControllerOriginal Compile Error: line 93:297 no viable alternative at character '%' at line 93 column 297 – NewbieDeveloper27 May 12 '14 at 20:31
    
You need to escape the single quotes like this: NOT LIKE \'4504%\'' – Mark Masterson May 12 '14 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%\') – NewbieDeveloper27 May 13 '14 at 13:29

Try adding NOT before the like as I show below:

AND NOT RecordType.Name LIKE
share|improve this answer
    
This is not correct SOQL syntax. dphil, you should try it yourself and remove the answer. – JannieT Oct 29 '15 at 9:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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