function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RedSalesRedSales 

Using Case or IF Statements in Formula logic for a field update

Hi, I'm trying to perform the following in a formula value in a workflow rule field update.

 

If value x is not null

 - update field with value x

else

 - update the field with value y

 

I tried this out using an If statement and Case statements but got errors both times.

is it true that an if statement can only return true or fase?  I can't say 

If (x not null), x, y

 

The above is just pseudocode but should outline what i am trying to do.

 

Any suggestions would be welcomed.

 

Thanks!

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Kent ManningKent Manning

What kind of errors are you getting in your formula? 

 

An If statement can return a value, a true or a false, or a constant.  The only thing that you have to be aware of is the data type.  So if your are trying to update a date field with a formula that contains an if statement, the value that the if statement returns has to be a date data type.  Also the condition logic in the if statement must be either true or false.  So for example you can't compare a number to a text value.  That will not work.  

 

The case statement is a bit different.  Here you have to return the same data type.  So if you are evaluating a text field then you have to return text.    Example: Case (AccountName, "Acme", "This result has to be a text value" )  I can't say Case(AccountName, "Acme", True)  This will not work because I'm looking up a text value but I'm returning a logic value.

 

Your If statement should work.  I would write it as IF((x <> null), x, y).  The <> is the operator for not equals

 

Hope that helps.

 

All Answers

SabrentSabrent

Can you please provide some more details -

What is x? If it is a field__c, what is the datatype?

If y is a field__c too, what is the data type? or is y a constant number or text?

LegendLegend

Hi,

IF statement only returns either true or false.

 

You can use CASE like:

Case(<fieldName>, NULL, "Hello", "WORLD")

Kent ManningKent Manning

What kind of errors are you getting in your formula? 

 

An If statement can return a value, a true or a false, or a constant.  The only thing that you have to be aware of is the data type.  So if your are trying to update a date field with a formula that contains an if statement, the value that the if statement returns has to be a date data type.  Also the condition logic in the if statement must be either true or false.  So for example you can't compare a number to a text value.  That will not work.  

 

The case statement is a bit different.  Here you have to return the same data type.  So if you are evaluating a text field then you have to return text.    Example: Case (AccountName, "Acme", "This result has to be a text value" )  I can't say Case(AccountName, "Acme", True)  This will not work because I'm looking up a text value but I'm returning a logic value.

 

Your If statement should work.  I would write it as IF((x <> null), x, y).  The <> is the operator for not equals

 

Hope that helps.

 

This was selected as the best answer
RedSalesRedSales

Thanks everyone for your help. My errors were because I was not using <> previously.  I was using !=

 

Thanks!