1

Hi I am a beginner at apex coding and salesforce. Here is my code that I would like to use the NOT() function.

CASE(Lead_Source__c,
    "Self Generated",
        IF(Markup__c >= 0 && Markup__c <0.35, 0,
        IF(Markup__c >=0.35 && Markup__c <0.40, 2/100,
        IF(Markup__c >=0.40 && Markup__c <0.45, 3.5/100,
        IF(Markup__c >=0.45 && Markup__c <0.50, 3.5/100,
        IF(Markup__c >=0.50 && Markup__c <0.60, 5.5/100,
        IF(Markup__c >=0.60 && Markup__c <0.70, 6.50/100,
        IF(Markup__c >=0.70 && Markup__c <0.80, 7.0/100,
        IF(Markup__c >=0.80 && Markup__c <1, 7.5/100, 0)))))))),
    "Self Generated",
        IF(Markup__c >= 0 && Markup__c <0.35, 0,
        IF(Markup__c >=0.35 && Markup__c <0.40, 5/100,
        IF(Markup__c >=0.40 && Markup__c <0.45, 8/100,
        IF(Markup__c >=0.45 && Markup__c <0.50, 8/100,
        IF(Markup__c >=0.50 && Markup__c <0.60, 11/100,
        IF(Markup__c >=0.60 && Markup__c <0.70, 12/100,
        IF(Markup__c >=0.70 && Markup__c <0.80, 13/100,
        IF(Markup__c >=0.80 && Markup__c <1, 13/100, 0)))))))),
    0
)

So pretty much I would like the first "Self Generated" to use the NOT() function. Because when the picklist value is not "Self Generated" , then it should perform those series of "IF" statements for all the other picklist values I have. The second "Self Generated" is perfect, because when the picklist value is "Self Generated" ,then I would like to perform those series of "IF" statements.

NEW CODE:

IF(ISPICKVAL(Lead_Source__c, "Self Generated"), 
        IF(Markup__c >= 0 && Markup__c <0.35, 0,
        IF(Markup__c >=0.35 && Markup__c <0.40, 5/100,
        IF(Markup__c >=0.40 && Markup__c <0.45, 8/100,
        IF(Markup__c >=0.45 && Markup__c <0.50, 8/100,
        IF(Markup__c >=0.50 && Markup__c <0.60, 11/100,
        IF(Markup__c >=0.60 && Markup__c <0.70, 12/100,
        IF(Markup__c >=0.70 && Markup__c <0.80, 13/100,
        IF(Markup__c >=0.80 && Markup__c <1, 13/100, 
        0))))))))),
IF(NOT(ISPICKVAL(Lead_Source__c, "Self Generated")),
        IF(Markup__c >= 0 && Markup__c <0.35,
        IF(Markup__c >=0.35 && Markup__c <0.40, 2/100,
        IF(Markup__c >=0.40 && Markup__c <0.45, 3.5/100,
        IF(Markup__c >=0.45 && Markup__c <0.50, 3.5/100,
        IF(Markup__c >=0.50 && Markup__c <0.60, 5.5/100,
        IF(Markup__c >=0.60 && Markup__c <0.70, 6.50/100,
        IF(Markup__c >=0.70 && Markup__c <0.80, 7.0/100,
        IF(Markup__c >=0.80 && Markup__c <1, 7.5/100,
        0)))))))))

ERROR: Syntax error. Extra ','

8
  • FYI, you can reduce your function by probably 75% if you take out the redundant conditions and move the /100 outside of the case statement. This is important if you plan on using more formulas on the same object.
    – sfdcfox
    Jun 24 '16 at 1:55
  • But how would I have conditions for other lead sources, because the commission percentage is different for each lead source and its different depending on how much the markup % is. Also where would I put the "/100", honestly I dont know I am new to all this so I am learning on how to reduce formula's and maniplute them.
    – Alias311
    Jun 24 '16 at 1:59
  • I've written an answer for you to get started. If you get too much more complicated, you won't even be able to save this function, so you might need to resort to alternative means.
    – sfdcfox
    Jun 24 '16 at 2:03
  • Yes I see the answer that you have posted. But the only thing I want to add is that if the Picklist value is not Self Generated then do this. Because the commission percentage that is calculated is different for "Self Generated" , but the same for all the other PickList values.
    – Alias311
    Jun 24 '16 at 2:05
  • That's what my answer does: it provides one value for "self generated", and one value for "every other value". Try it out and see how it works.
    – sfdcfox
    Jun 24 '16 at 2:06
5

The last value in the case is the "default value", in other words, for any value not already in the case statement. Therefore, it should look like this:

CASE(Lead_Source__c,
    "Self Generated",
        IF(Markup__c >= 0.35 && Markup__c <0.40, 5,
        IF(Markup__c <0.50, 8,
        IF(Markup__c <0.60, 11,
        IF(Markup__c <0.70, 12,
        IF(Markup__c <1, 13, 0))))),
        IF(Markup__c >= 0.35 && Markup__c <0.40, 2,
        IF(Markup__c <0.50, 3.5,
        IF(Markup__c <0.60, 5.5,
        IF(Markup__c <0.70, 6.50,
        IF(Markup__c <0.80, 7.0,
        IF(Markup__c <1, 7.5, 0))))))
)/100

Notice how I took out the redundant logic: formulas have only a limited amount of "space" per object, so make sure you're writing your code as efficiently as possible.

3
  • OMG yes I see the redundant logic. Thanks a lot for pointing that out. I will make sure next time to make everything more efficient
    – Alias311
    Jun 24 '16 at 2:03
  • @Alias311 Yes, you can add more values if you need to, just make sure you leave the last value for the default.
    – sfdcfox
    Jun 24 '16 at 2:05
  • WOW you literally decreased the compile size of this formula. It went from 4500------>1849!!! Your corrections did make a huge difference and that is what I want to learn moving forward in salesforce.
    – Alias311
    Jun 24 '16 at 2:07
0

Inistead of CASE you can use ISPICKVAL(picklist_field, text_literal).

So you'll end up with something like this:

IF(ISPICKVAL(Lead_Source__c, "Self Generated"), TrueBlock, FalseBlock)

Or vice versa, if you want to use the NOT function, you do:

IF(NOT(ISPICKVAL(Lead_Source__c, "Self Generated")), FalseBlock, TrueBlock)

In addition to this, you can also compare a result from a function against TRUE or FALSE values:

IF(ISPICKVAL(Lead_Source__c, "Self Generated") = FALSE, ..., ...) is equivalent to the previous statement.

Replace the TrueBlock and FalseBlock placeholders with your blocks of IF statements.

1
  • okay so I did what you said and I get an error. I have posted the new code above. Please correct me if I have made mistakes. I'm not use to programming on the salesforce platform.
    – Alias311
    Jun 24 '16 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.