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 equalitydavid.Diet_Start_Date__c == Date.today(); david.Sleep_Hours__c == 5;// How to test inequalitydavid.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 FALSEdavid.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!
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.
You need single quotes around strings! ‘Like this’
https://www.sfdc99.com/2013/09/28/data-types-strings-dates-numbers-and-sobjects/
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
Also a great candidate for the forums!
https://www.sfdc99.com/forums/forum/beginning-apex/
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.
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);
}
}
Use Database.insert instead =)
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
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?
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!!
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
Thank u sooo much david..:) It is really helpful to me
No problem Neha, always happy to help =)
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
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
Yes, Thanks for ur reponse.. I am able to find my mistakes now