Salesforce coding lessons for the 99%
Finally, Apex tutorials for point-and-click admins! Written by a self-taught Google engineer.
  • Beginner Tutorials
    • Apex
    • Certifications
    • Career Info
    • Technical Architect
    • Visualforce
    • Videos
  • Apex Academy
  • Success Stories
  • About Me
  • Misc
    • Mailbag
    • Challenges
    • Links
    • Login to my Org
Follow @dvdkliuor SUBSCRIBE!

Comparison operators

October 12, 2013

Preface – This post is part of the Core Apex Tools series.

We must understand comparison operators before moving on to IF statements! A comparison operator is simply any statement the equals a boolean (TRUE or FALSE).

Try to guess which of these are TRUE and which are FALSE!

Comparisons for Number and Date fields:

david.Weight__c < 150; david.Weight__c >= marcBenioff.Weight__c;
// You need two equal signs (==) to test equality
david.Diet_Start_Date__c == Date.today(); david.Sleep_Hours__c == 5;
// How to test inequality
david.Donuts_Eaten__c != 0;

Comparisons for Text and Picklist fields:

// Strings also use == and !=
david.Favorite_Food__c == 'Cheeseburgers'; david.Favorite_Food__c != 'Liver';

Comparisons for Checkbox fields:

// Checkbox fields are always either TRUE or FALSE!
david.Likes_Ice_Cream__c;
// The exclamation point (!) takes the opposite of the statement! // I'm not training for a marathon, so this comes out to TRUE
!david.Training_for_Marathon__c;

Combine comparisons using || for OR, && for AND, and parentheses:

// Both of these are FALSE, so the outcome is FALSE
david.Weight__c < 150 || david.Training_for_Marathon__c;
// This is FALSE because I am lactose intolerant =(
david.Likes_Ice_Cream__c && !david.Is_Lactose_Intolerant__c;
// Combine ORs and ANDs using parentheses!
(david.Is_Hungry__c || david.Is_Bored__c) && david.Has_Ice_Cream__c;

Next post: IF statements!

16 Comments
Sahaj
October 9, 2014 @ 4:02 pm

Hi David

Please help me out with this code.

trigger def on User (before insert,before update)
{
for(User u : Trigger.new)
{
if(Profile == ServiceDesk Staff )
{
u.BMCServiceDesk__IsStaffUser__c = true;
}
}
}

I am getting this error Error: Compile Error: expecting a right parentheses, found ‘Staff’ at line 5 column 26
but no parentheses is missing.

Reply
    David Liu
    October 9, 2014 @ 7:59 pm

    You need single quotes around strings! ‘Like this’

    https://www.sfdc99.com/2013/09/28/data-types-strings-dates-numbers-and-sobjects/

    Reply
Salaad Nur
September 8, 2014 @ 11:53 am

Hi David,
Thanks for the great website. My question is the semicolon on the if statement. Took me sometime to figure it out but I needed to include a semicolon if I have more than one criteria. Here are the two that worked for me:

trigger UpdateAccount on Opportunity (after insert,after update)
{
for (Opportunity OppChange: Trigger.new){
if (OppChange.StageName==’Prospecting’){
Account currentAcc = new Account();
currentAcc.Id=OppChange.AccountId;
currentAcc.Ownership=’Public’;

update currentAcc;
}
}
}

trigger UpdateAccount on Opportunity (after insert,after update)
{
for (Opportunity OppChange: Trigger.new){
if (OppChange.StageName==’Prospecting’&&Oppchange.Account.Type=’Prospect’;){
Account currentAcc = new Account();
currentAcc.Id=OppChange.AccountId;
currentAcc.Ownership=’Public’;

update currentAcc;
}
}
}

Can you please let me know why the first didn’t require a semicolon and the second did?

Salaad Nur

Reply
    David Liu
    September 8, 2014 @ 8:47 pm

    Also a great candidate for the forums!
    https://www.sfdc99.com/forums/forum/beginning-apex/

    Reply
    Reshmi
    December 8, 2014 @ 2:26 am

    Hi Salaad,
    In your code, in the if Statement, can’t we use “”” if ((O.StageName == ‘Prospecting’)&& (O.Account.Type == ‘Prospect’)) “”” statement ?
    let me know if i am wrong.

    Reply
saurabh
July 30, 2014 @ 12:37 am

Hey David!
i tried this code and its showing some error but i am unable to get the words so can you please tel where am i wrong.
and the error is

“createNewAccountOpportunity: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.createNewAccountOpportunity: line 17, column 1”

Thanks.

trigger createNewAccountOpportunity on Account (after insert) {
List listOpportunities = new List();

for (Account oAccount : trigger.new) {
Opportunity oOpportunity = new Opportunity();
oOpportunity.Name = oAccount.Name;
oOpportunity.AccountId = oAccount.Id;
oOpportunity.Stage = ‘Proposal’;
oOpportunity.CloseDate = System.today() + 30; //Closes 30 days from today

listOpportunities.add(oOpportunity);
}

if (listOpportunities.isEmpty() == false) {
Database.update(listOpportunities);
}
}

Reply
    David Liu
    August 4, 2014 @ 10:35 pm

    Use Database.insert instead =)

    Reply
neha
November 18, 2013 @ 9:43 pm

Here is the example ..which i tried

trigger AddToPG on User (after insert, after update) {
List GMlist = new List();
for(User U : Trigger.New ) {
if(U.isActive && !u.profile.equals (‘%DRM%’ ) ) {
GroupMember GM = new GroupMember();
GM.GroupId = ’00GU0000001Zjeq’;
GM.UserOrGroupId = U.Id;
GMList.add(GM);
}
}
if(!GMList.isEmpty()) {
System.debug(‘Group Member List is ‘ + GMList);
insert GMList;
}
}

and i am facing problem as below:

Error: Compile Error: Method does not exist or incorrect signature: [SOBJECT:Profile].equals(String) at line 4 column 27

How to resolve this

Reply
    neha
    November 18, 2013 @ 9:48 pm

    I understood from the error that profile is a Sobject and i cant compare with the string. then how can i compare in this situation?

    Reply
      David Liu
      November 18, 2013 @ 10:20 pm

      The reason why your code didn’t work is because you were comparing a profile to DRM. Since profile is an sObject in Salesforce, you need to use Profile.Name

      You were very close!!

      Reply
    David Liu
    November 18, 2013 @ 10:20 pm

    Looks like you need an Apex solution to this! My other solution was for SOQL.

    Here’s how to do it in Apex:

    if (u.Profile.Name.contains(‘DRM’)) {
    // Other code here
    }

    Contains is a standard method you can use with any string:
    http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_String_contains.htm

    Hope this helps!
    David

    Reply
      neha
      November 19, 2013 @ 12:37 am

      Thank u sooo much david..:) It is really helpful to me

      Reply
        David Liu
        November 19, 2013 @ 5:41 am

        No problem Neha, always happy to help =)

        Reply
neha
November 18, 2013 @ 9:36 pm

In apex code.. ( I mean in trigger )i have a requirement like .. i have to check USER object with the condition that field profile should not have ‘DRM’ in its profile name(
profile NOT IN ‘%DRM%’).. i tried in all means..watever i know and got failed…pls help me David

Reply
    David Liu
    November 18, 2013 @ 10:17 pm

    No problem Neha!

    You probably want something like this:
    SELECT Id, Username, Profile.Name FROM User WHERE NOT (Profile.Name LIKE ‘%DRM%’)

    The syntax for NOT is a little strange, but you’ll get used to it =)

    Hope this helps!
    David

    Reply
      neha
      November 19, 2013 @ 3:11 am

      Yes, Thanks for ur reponse.. I am able to find my mistakes now

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


*

*

Theme: Simple Style by Fimply