Calculated sum field with multiple conditions

I am looking to create a calculated field that will find the sum of fields on related records if they meet 2 separate conditions. I'm able to use the rollupConditionalSum to account for one of the conditions, but I can't figure out how to include an additional condition. 

For my specific scenario we have a module for all our orders. Each of those order records have related individual transaction records that house specific components of the sale (i.e. a discount, fee, payment, etc.). I am looking to create a single field that will sum up all the same transaction types that have a specific status. We currently have this formula that isn't quite doing what we want:

ifElse(
equal(related($sp_transactions_ord_orders,"transaction_status"),"Applied"),rollupConditionalSum($sp_transactions_ord_orders,"transaction_amount","transaction_account_types","Applied_Payment"),0)

The problem with the above formula is that it is just looking for the presence of any related transaction with status of "applied" and then summing all transactions of type "applied_payment". What I need is for it to only sum all the transaction amounts for records that have the type of "applied_payment" AND transaction status of "Applied". 

Any guidance you can offer would be greatly appreciated!

Thank you,

Abby

  • I'm afraid you need to create a custom sugarLogic function based on the core rollupConditionalSum and inject a second condition in order to accomplish your needs. The core file is located at include/Expressions/Expression/Numeric/SumConditionalRelatedExpression.php.

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Hi Abby, 

    To add to what  shared, you can check here how to Extend Sugar Logic to create your own functions. 

  • Thank you for your help with this! I will take a look into this further.

  • Hi Abby

    May I ask you why are you looking for the calculated field specifically?
    Why not configure a logic hook to the Transactions record that will update the parent's record field value, do you consider this option?
    How many Transactions are created per minute?

    Best Regards,
    Dmytro Chupylka

    integroscrm.com
    We make work in Sugar CRM system faster, more convenient and efficient

  • Great question! To answer your specific question, right now we would expect less than 100 transactions created or updated a minute. We are trying to keep an eye towards the future though to structure our database for scaling if we do grow.

    We are fairly new to Sugar (been in Sugar for about a year), so are still learning the capabilities and best practices. From your perspective, what situations would you utilize a logic hook vs. a calculated field vs. a business process in Sugar? 

  • Logic Hook is the fastest solution in an execution perspective, but it fully requires development techniques.

    I like to say Logic Hook is pretty welcome for accomplishing procedures not supported by GUI configurations.

    Calculated field is not as faster as Logic Hook but it is very easy to implement, once you can do that through Studio. It is indicated for a solely calculation.

    Business process is indicated for calculations in a context of a well defined and implemented business process at SugarBPM, that means, if a field should be calculated somehow due some trigger which may be an ellapsed time or previously modified field or message either received or sent. In short words you will not configure an entire SugarBPM Process to update a single field.

    Kind regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Hello Abby,

    AFAIK, the calculated field has a similar trigger-based nature as a generic logic hook.

    However, I would highlight the following specifics of calc fields: 

    a) for the calc field you write a text formula - it works fine for relatively simple calculations, but the formula quickly goes crazy-looking if you try to achieve more complex calculations.
    That is probably due to the specific unary operations-style spelling, which hasn't changed in Sugar for, maybe, the last 10 years, if I'm not wrong

    b) calc field can recalculate in the user interface on the fly - before the record is saved
    this is truly a great advantage - if you like to get calc field value recalculated while you are in the process of entering data to the fields on which the calc field depends from

    c) the calc field is always read-only -
    sorry, you have no chance to adjust it if you don't like the value that was calculated with the formula
     
    Logic Hooks provides the level of flexibility that pure Sugar code is capable to deliver - actually, it is hardly comparable to any formulas which rely on a predefined limited set of available operands and functions.

    Speaking figuratively, these two are from different weight classes in terms of logic implementation capabilities  - heavyweight Logic Hook against lightweight Calc Field.

    Actually, Logic Hooks were something that only experienced Sugar developers can deliver.

    I said "were" instead of "have been" because today Logic Hooks creation is available via configuring - for admins with no PHP development skills.

    This possibility is provided by the Logic Builder no-code tool, which performs like a designer environment, which puts it in a row with a built-in no-code Sugar Studio tool - you just draw what you need and deploy it to your Sugar - no code involved.

    Logic Builder receives fairly good feedbacks but unfortunately is not an out-of-the-box Sugar component.

    For highly intensive updates of transactions it is recommended to use time-based calculations - e.g. to calculate once in 10 min with a Schedule Job or with external service - we have the experience of establishing calculations of the transaction aggregates for Forex brokers that import more than a thousand transactions per minute into theirs Sugar.

    But since 100 changes per min still looks fairly good  for using trigger-based calculations, which could be  performed by both CalcFields and Logic Hooks, and having in mind that out-of-the-box CalcFields cannot help with your task,
    I would suggest trying to solve the task by designing a logic hook with Logic Builder that I have access to - just for your genuine feedback here, in this forum thread, if logic hook actually solves your challenge (like it was done for Juliana's inquiry)

    If this offer looks fair to you, please drop me a line at dch@integroscrm.com so that I could help


    Best Regards,
    Dmytro Chupylka

    integroscrm.com
    We make work in Sugar CRM system faster, more convenient and efficient