Authored by Zishan Razzaq
Showing posts with label SOQL Query. Show all posts
Showing posts with label SOQL Query. Show all posts

Thursday, November 29, 2012

How to use 'NOT LIKE' in SALESFORCE SOQL

Hi everyone:
    Business Scenario:
        They would like something update where a field on the record is NOT LIKE "adviser." Or in simplest terms does not contain "Adviser".

Solution for developers in APEX:

APEX will not allow someone to write a simple query such as:
SELECT Name From ABC where id=:Ownerid and Name NOT Like '%adviser%';

So solution is simple:
SELECT NAME From ABC where id=:Ownerid and (NOT Name Like  '%adviser%');

If you are utilizing GROUP By statements here is an example of one:


 AggregateResult[] groupedResults = 
         [SELECT Ownerid,Count(Id) ce 
         FROM Opportunity Where 
         Withdrawn__c = True 
         AND StageName = 'Closed Won' 
         AND Withdrawal_Status__c ='Withdrawn' 
         AND Withdrawn_Date__c = THIS_MONTH  
         AND Ownerid=:id 
         AND (NOT Name Like '%COMP%')
         GROUP BY Ownerid];  

Hope this Helps.
Thanks
Zishan



Wednesday, February 25, 2009

Date Formats and Date Literals in SOQL Query

Hi.. Welcome back..
I always had issues with SOQL Query utilizing dates in particular. So I wanted to share some tips and tricks on writing SOQL queries using some Built in Date Functions...

Now you do your query in 2 ways:

1. Specify a date in your query such as
Select Id,CreatedDate from Account where CreatedDate=DateValue(2008-02-05)
This will return all Id's for all the Account Object that were Created 2008-02-05.

2. Do it with a Fixed Expression or Built in Function such as:
Select Id, CreatedDate from Account where CreatedDate = YESTERDAY
This will return all Id's for the Account Object that were Created yesterday.
This is called a Date Literal.

Now before we go on:
You must know the Syntax for Date in Force.com :

YYYY-MM-DD

For DateTime the Syntax is as follows:

YYYY-MM-DDThh:mm:ss+hh:mm 2009-02-24T23:01:01+01:00
YYYY-MM-DDThh:mm:ss-hh:mm 2009-02-24T23:01:01-08:00
YYYY-MM-DDThh:mm:ssZ 2009-02-24T23:01:01Z

So Lets go into Date Literals: I took this off from the Salesforce.com Api docs...
The link is: http://www.salesforce.com/us/developer/docs/api/index.htm

Go to search and type in Date Literals... This should come up.(See Below)