How can I use the rollupCurrencySum and only obtain certain records?

Here is our use case:

We have a custom field with a year field.

Accounts has a 1 to Many relationship with the custom module

I want this

rollupCurrencySum($mymodule_accounts, "amount") 

where

equal(related($mymodule_acounts, "year"), "2014")?

how can I limit my rollupCurrencySum to only this year?
Parents
  • Hi Jeff,

    Conditional rollUp functions are not currently possible in Sugar. This is requested in enhancement request #59450. The request can be reviewed apart from this case here.

    However, this can be done with a simple logic hook. For an introduction to creating a simple logic hook, please review our knowledge base article on the subject at:

    Creating a Simple Logic Hook

    To build upon the information provided in that article with an example pertaining to the logic in your question, I will discuss how to make a simple logic hook that, when an Opportunity is changed to Status: Closed Won, will automatically assess all the Opportunities related to the parent account, looking for Closed Won sales status and then including only those that meet the condition in a roll up in an integer field on the account.

    This example hook would be defined in the file system at:
    custom/modules/Opportunities/logic_hooks.php

    with the following line:
    $hook_array['before_save'][] = Array(1, 'rollupsum_won_oppts', 'custom/modules/Opportunities/custom_hooks/rollupsum_won_oppts.php','rollupsum_won_oppts_class', 'rollupsum_won_oppts_method');

    The content of custom/modules/Opportunities/custom_hooks/rollupsum_won_oppts.php would then be:

    <?php
    class rollupsum_won_oppts_class {
    function rollupsum_won_oppts_method($bean, $event, $arguments) {
    if ($bean->sales_status == 'Closed Won') {
    $account = BeanFactory::getBean('Accounts',$bean->account_id);
    $account->closed_opportunities_c = 0;
    $account->load_relationship('opportunities');
    foreach ($account->opportunities->getBeans() as $opportunity) {
    if ($opportunity->sales_status == 'Closed Won') {
    $account->closed_opportunities_c = $account->closed_opportunities_c + 1;
    }
    }
    $account->save();
    }
    }
    }
    ?>

    This example can hopefully be reworked to apply to your custom module and the condition field of your choosing.

    Regards,
    Patrick McQueen
  • Hello Patrick:

    How are you? Do you know if this is fixed? There isn't any update on the bug...

    Thanks.

    Kind regards.

    Nikol.

  • Hi Nikol,

    Conditional rollup is possible in the product using the following function:

    Function: rollupConditionalSum
    Description: Returns the sum of the values of $field in records related by $link where $conditionField contains something from $conditionalValues.
    Parameters: Relate $link, String $field, String $conditionField, List $conditionalValues
    Return Type: Number

    I hope this helps!

Reply Children