Find your content:

Search form

You are here

How to use "Does Not Start With" in Where clause for SOQL

 
Share

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)+'%\'';   

Attribution to: NewbieDeveloper27

Possible Suggestion/Solution #1

Try adding NOT before the like as I show below:

AND NOT RecordType.Name LIKE

Attribution to: dphil

Possible Suggestion/Solution #2

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.


Attribution to: PepeFloyd
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34934

My Block Status

My Block Content