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
sfdc-admin-smsfdc-admin-sm 

Anyway to optimize this workflow formula?

Hello All,

 

I currently have a working workflow formula as follows:

 

IF(Product2.Name = "13-Credits" && Quantity = 1, 13, 
IF(Product2.Name = "13-Credits" && Quantity = 2, 13 * 2, 
IF(Product2.Name = "13-Credits" && Quantity = 3, 13 * 3, 

)))

 

I know I can either keep this formula as is or use a Case function. However, if I need to 100 different Quantity values, this will require me to have 100 different lines on the formula for each Quantity value. Even worst if I have 10 different Product2.Name requiring 100 different Quantity variations, the number of lines in this formula can easily = 10 * 100 = 1000 lines!

 

Is there any way I can reduce the number of lines in the this formula? 

 

Please respond with specific working formula examples.

 

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
Kiran  KurellaKiran Kurella

Try this:

 

IF(Product2.Name = "13-Credits", 13, 0) * Quantity

You should consider moving the rates to a custom object and use Apex to handle the logic for several products.

 

All Answers

Kiran  KurellaKiran Kurella

Try this:

 

IF(Product2.Name = "13-Credits", 13, 0) * Quantity

You should consider moving the rates to a custom object and use Apex to handle the logic for several products.

 

This was selected as the best answer
Shannon HaleShannon Hale

If Product2.Name always starts with the number of credits followed by a hyphen, you could potentially simplify this further:

 

VALUE( LEFT( Product2.Name, FIND( "-", Product2.Name ) - 1 ) ) * Quantity

This strips out the digits from the beginning of the name and turns them into a number, and then multiplies that number by the quantity to get the total number of credits. So the same line works for any product as long as the name follows the format "N-xxx".

 

You would probably also want to write a validation rule on Product2 to ensure that the product names are entered in the correct format; you could do that like this:

 

NOT( ISNUMBER( LEFT( Product2.Name, FIND( "-", Product2.Name ) - 1 ) ) )