Hot answers tagged

26 votes
Accepted

Best Practice discussion: Difference between SWITCH and IF/ELSE-IF/ELSE?

Switch, like the if statement, is a tool. If can do whatever switch can do and more, but it can't do what switch does as efficiently as it does. Switch can drastically reduce the complexity of some ...
sfdcfox's user avatar
  • 487k
12 votes
Accepted

Using AND OR in AmpScript IF THEN statement

Should be something like this: %%[ if (@PromoCode == "XXXX" OR @Tier == "XXX") AND @Price == "100" then ]%% %%[ endif ]%%
Adam Spriggs's user avatar
  • 30.4k
8 votes
Accepted

Using arithmetic comparison in if:true statement in Lightning web component

LWC is not supporting any conditional operators or expression evolution methods in markup. To achieve this you should develop a child component and pass the data and index attributes to child ...
Yaswanth Tikkisetti's user avatar
7 votes
Accepted

IN in an IF condition

You can create a Set that contains unique values and check if this Set contains value to check. Set<String> availableStatuses = new Set<String> { 'Dead', 'Duplicate', 'Cold' ...
Oleksandr Berehovskyi's user avatar
6 votes
Accepted

Ternary Operator error - Error: Compile Error: expecting a colon, found ')' at line

Don't use multiple ternary operators in one line. It's way too confusing to be worth it. Here's what you have: /*condition1*/ !GWB_Utility.isNull(cust.FinancialIndicator) ? /*truthy_value1*/ /*...
Adrian Larson's user avatar
  • 150k
6 votes

Using multiple criteria for IF/THEN function

You cannot check for multiple strings in a single variable like you did. You need to split the expression. Additionally you are missing two percent-signs for the elseif-part. So the following part: @...
Markus Dang - formerly Slabina's user avatar
6 votes

How to check object name by objectId?

You don't need the Describe API. Just use the Id class's methods. Adapted from the documentation: Contact a = new Contact(LastName = 'Test'); insert a; Id myId = a.id; System.assertEquals(Contact....
David Reed's user avatar
  • 92.6k
5 votes

IF this or this or this?

You have two basic options: Use the Set.contains method Join complete logical clauses The first strategy would look like: Set<Integer> multiplesOf4 = new Set<Integer>{0,4,8,12,...}; ...
Adrian Larson's user avatar
  • 150k
5 votes

IF Statement on the VF page to display/hide Related list(s)

Wrap it in a output panel, but to be fair itzmukeshy was correct also, you don't need to do the IF <apex:outputPanel rendered= "{!IF(yourLookup != null, true, false)}"> </apex:outputPanel>...
EricSSH's user avatar
  • 4,334
5 votes
Accepted

comparing and assign values to a variable on same line

It's called ternary assignment and the syntax is: String someVariable = someBoolean ? truthyValue : falsyValue; You can see more info in Understanding Expression Operators: Operator ? : Syntax x ? y ...
Adrian Larson's user avatar
  • 150k
5 votes
Accepted

IF and AND statements within Visualforce page

That should work, to be honest, but realistically, you don't need the IF statement--a Boolean value is already a Boolean value, so you don't need another Boolean value. rendered="{!AND(Opportunity....
sfdcfox's user avatar
  • 487k
5 votes
Accepted

An error occurred while checking the query syntax. Errors: Old style JOIN (ANSI JOINs) syntax is not allowed. Please use standard syntax

I'd do something like this with left joins: select 'Rolling' as [Group] , case when em.account_number is not null then 'Y' else 'N' end Emailable , case when sm.account_number is not ...
Adam Spriggs's user avatar
  • 30.4k
4 votes

comparing and assign values to a variable on same line

It's a simple "ternary operator": String s2 = s1 != null? 'Hello': null; This is also sometimes called the "Elvis operator". The general syntax is: condition ? trueValue : falseValue; Note that ...
sfdcfox's user avatar
  • 487k
4 votes

Aura :iteration and :if, WARNING: Performance degradation..

It turns out that using an :if in an :iteration is not recommended. From the salesforce developer documentation: Here are some use cases where you should consider alternatives to <aura:if>: You ...
nicstella's user avatar
  • 589
4 votes
Accepted

NullPointerException: Attempt to de-reference a null object in a batchclass

You can't directly set these parent fields from the Asset record. You have to query for the related Account and set it's Actief_onderhoud_Design_Solutions__c directly. So you'll prob need to do ...
Caspar Harmer's user avatar
4 votes
Accepted

Custom Button Formula URL IF condition

Merge fields start with {! and end with }. You don't use them in the middle of a formula. To merge in the middle of the formula, you want to use the concatenation operator, &. However, doing it ...
sfdcfox's user avatar
  • 487k
4 votes
Accepted

Is there a way a more efficient way to write the IF statement

We use the coding convention that the curly brackets can be skipped if the condition is placed on a single line to make these common cases less cluttered looking. (Such conventions generally cause ...
Keith C's user avatar
  • 136k
4 votes
Accepted

Syntax error in this IF statement?

At a first glance, I don't see anything that should be throwing a 500 error. When you are testing new functionality on a Cloudpage, it's always a good idea to wrap your scripts within a try/catch so ...
Jason Hanshaw's user avatar
4 votes
Accepted

Changing the colours of the fields depending on a formula?

You can use the IMAGE formula field to accomplish this. Your formula will be as below IF (A > B, IMAGE("/img/samples/color_red.gif", "red", 40,40), IMAGE("/img/...
Mohith Shrivastava's user avatar
4 votes

Multiple OR for one variable and with AND CONDITION

This question is honestly pretty bad and likely should be closed due to lack of details/efforts displayed, but as its a fairly simple answer, I thought I would put something up for those that face the ...
Gortonington's user avatar
  • 30.5k
3 votes
Accepted

Nested IF formulas for a simple logic field on the opportunity

Try this: IF( AND( Department_MQL__c <> 'SDR generated, relationship management', Department_SQL__c = 'SDR', ISPICKVAL(Account.Sector__c, '<>Supplier/Distributor')), ...
Boris Bachovski's user avatar
3 votes
Accepted

If Condition for validation rule to check two fields

Validation rules trigger when the formula evaluates to true, so I think you really just need to check that total fee is not zero and Invoiced Fee is greater than Total Fee. AND(Total_Fee__c>0, ...
sfdcfox's user avatar
  • 487k
3 votes

Best Practice discussion: Difference between SWITCH and IF/ELSE-IF/ELSE?

The accepted answer here Looping over a list of sObjects that has more than object type shows an Apex-specific extra capability of type matching that is kinda neat. Personally I'd rate a library ...
Keith C's user avatar
  • 136k
3 votes
Accepted

How to subtract apex variable in IF condition of Visualforce page

Once you're in a "merge field", you don't need to keep merging: {!IF(Project.pse__Planned_Hours__c - grandtotalAH > 0, Project.pse__Planned_Hours__c - Project.Total_Billable_Hours__c, 0)}
sfdcfox's user avatar
  • 487k
3 votes
Accepted

Logically comma separating address field to form a String

For things like this, I use an array: String[] addr = new String[] { record.Street, record.City, record.State, record.PostalCode, record.Country }; for(Integer i = 4; i >= 0; i--) { if(addr[i]...
sfdcfox's user avatar
  • 487k
3 votes
Accepted

Inline If statement not working

Firstly, I would use a CASE instead of IFin this case since you are always checking the same field value wrap.strdealerbaseparameter. I think that function below is more readable than what you have ...
smukov's user avatar
  • 5,543
3 votes
Accepted

How to write a trigger handler with trigger new

You don't need to specify the type or name of parameters when calling methods in Apex. This: handler.OnBeforeInsert(Invoice__c record Trigger.new); Should be: handler.OnBeforeInsert(Trigger.new); ...
Matt Lacey's user avatar
  • 25.6k
3 votes

Multiple OR (||) Conditions in If Statement

"OR" means "true if either value is true". If the ID is "XXXXXXXXX", for example, it will NOT be "YYYYYYYYY", therefore OR will result in a true value. You need to use "AND" (&&) instead, ...
sfdcfox's user avatar
  • 487k
3 votes

Multiple OR (||) Conditions in If Statement

@sfdcfox is correct, Although I would do a !( (new List<String>{'XXXX','YYYY','ZZZZ'}).contains(currentUserId) ) Chaining a bunch of Logical OR's gets messy, especially if you need to add to ...
Zach Hutchins's user avatar
3 votes
Accepted

Apex Unit Test for if == null and return statements

You're most of the way there, and I think you have all of the code you need, but there's a couple of key misconceptions here that are holding you back. Let's start here: Test.startTest(); String ...
David Reed's user avatar
  • 92.6k

Only top scored, non community-wiki answers of a minimum length are eligible