How To Add Days Based On Another Date Field?

Hello, I'm trying to create a duration panel for a subscription related data fields.

There are three fields

  1. Subscription Duration (Dropdown Field)
  2. Subscription Start Date (Date Field)
  3. Subscription End Date (Date Field)

Wherein:

  • Subscription Duration has the following dropdown options:
    • 1 Month
    • 2 Months
    • 3 Months
    • etc

What I'm trying to setup:(Sample Scenario)

  • Subscription Start Date = June 15, 2021
    • IF Subscription Duration = 1 Month, 2 Months, etc
    • THEN add days to Subscription End Date

Expected Result:

Sample A

  • Subscription Duration: 1 Month
  • Subscription Start Date: June 15, 2021
  • Subscription End Date: July 15, 2021

Sample B

  • Subscription Duration: 3 Months
  • Subscription Start Date: June 15, 2021
  • Subscription End Date: September 15, 2021

Does anyone know the Sugar Logic Formula to do this?

Parents Reply
  • Just got a request today that reminded me this topic of 2021. 

    The logic for creating a custom SugarLogic expression that adds months is actually pretty straightforward, and in this case, I believe it’s the best approach.

    Here’s what I built:

    custom/include/Expressions/Expression/Date/AddMonthsExpression.php

    <?php
    
    /**
     * <b>addMonths($date, $months)</b><br>
     * Returns a date object moved forward or backwards by <i>$months</i> calendar months.<br/>
     * ex: <i>addMonths(date("2024-01-15"), 2)</i> = "2024-03-15"
     */
    class AddMonthsExpression extends DateExpression
    {
        public function evaluate()
        {
            $params = $this->getParameters();
    
            $date = DateExpression::parse($params[0]->evaluate());
            if (!$date) {
                return false;
            }
            $months = (int)$params[1]->evaluate();
    
            $modifier = ($months < 0) ? "$months month" : "+$months month";
    
            return $date->modify($modifier);
        }
    
        public static function getJSEvaluate()
        {
            return <<<EOQ
            var params = this.getParameters();
            var fromDate = params[0].evaluate();
            if (!fromDate) {
                return '';
            }
    
            var months = parseInt(params[1].evaluate(), 10);
            if (_.isNaN(months)) {
                return '';
            }
    
            var date = SUGAR.util.DateUtils.parse(fromDate, 'user');
            var d = new Date(date);
    
            var targetMonth = d.getMonth() + months;
            d.setMonth(targetMonth);
    
            if (this.context.view) {
                d = App.date.format(d, 'Y-m-d');
            }
    
            return d;
    EOQ;
        }
    
        public static function getOperationName()
        {
            return 'addMonths';
        }
    
        public static function getParameterTypes()
        {
            return ['date', 'number'];
        }
    
        public static function getParamCount()
        {
            return 2;
        }
    
        public function toString()
        {
        }
    }



    After adding this file and Rebuild Sugar Logic Functions the new formula addMonths() it's exposed on the SugarLogic Builder. 




    With this formula, we can accurately add days, months, and years to a specific date using custom fields.

    If the number of days, months, and years are stored in the fields number_of_days_c, number_of_months_c, and num_years_c, respectively, we can perform a precise date calculation with the following SugarLogic formula:

    addDays(addMonths(addMonths($start_date_c,$number_of_months_c),multiply($num_years_c,12)),$number_of_days_c)



    The end result with be something like this: 



    I hope this helps. 

    Cheers, 

    André 




Children
No Data