Learning Sugar Logic: Calculated Date Due

sugarcrmdevelopers —  June 7, 2011 — 10 Comments

Starting in SugarCRM 6.2, we can use dates in SugarLogic formulas. This means we can now calculate fields like Due Date without writing custom code.

For example, we can open the “date_due” field of tasks in studio and check the “Calculated Value” checkbox. If we want the date due to be two weeks from the start date, we can use the formula:

addDays($date_start, 14)

Because Date Due is now a calculated field, we can no longer set the value manually in the Edit View. To get around this, we can create a Vardef Extension that set the “enforced” property of the field to false.  This will cause Sugar to leave the field editable on Edit Views, but still recalculate the value when other fields are changed. For example, when we modify the Start Date of Tasks, the Date Due will automatically be set to 2 weeks from the Start Date. However, we can now still edit Date Due and set it to an arbitrary value.

10 responses to Learning Sugar Logic: Calculated Date Due

  1. 

    I’m using 6.2.2 CE and I don’t see a checkbox called “Calculated Value” ? Is it only for PRO version ?

  2. 

    Just want some clarification on something you mention that I’ve been meaning to test for some time:

    “This will cause Sugar to leave the field editable on Edit Views, but still recalculate the value when other fields are changed.”

    Does that mean that if someone, in your example, overwrites the due date for a task to, say, 3 weeks from now, but then their partner goes in and changes the start date to next Tuesday (trying to be nice, realizing it’s a holiday), that the due date is recalculated to 2 weeks from the start date?

    The reason I’ve been wondering about this is because I’ve been in a tight spot with the enforced property in both directions.  I’ve always assumed once a value is overwritten, it stays that way, but a client asked “So, I like that it calculates but I need to be able to override anything in the system. Can I do that.”  Well, I was about to tell him enforced would do that, but balked.  So instead, I ended up creating a “Copy Calculated Field to Permanent Field” option for him.  Meanwhile, I have another client who also wanted to calculated field and the ability to change it, but somehow Sugar should also know when he wants it to recalculate (I considered a “recalculate” button, but he decided to let things calculate).

    Long story short, will someone’s “final answer” on a field be lost to a recalculation?

    On a similar note, I think a lot of this fuss might be solved by offering a calculated Default Value.  Most people just don’t want to do the math but don’t really want a field that’s out of their control (even if it’s what’s best for them).

  3. 

    I have this calculated value on a Date field, where I add 30 days to now(). I need the Calendar button to appear so that this date can be edited later on and I tried your solution with the “enforced” property set to false (in custom/Extension/modules/[MyModule]/Ext/Vardefssugarfield_[my_field]_enforced.php) bun adding this breaks my formula – any chance I’m misunderstanding something in your instructions?

    • 

      What do you mean by breaks? Does it just stop working entirely?

      • 

        yes, the field has an empty value, as if there is no formula at all.

        • 

          Hmm, not sure. Could you post the vardef snippet that shows the change you made?

          • 

            Having the Task Start date, I edit it in Studio and change it to be a calculated value, with formula: now(). This gives me a non-editable datetime field, correctly filled up. I then edit customExtensionmodulesTasksExtVardefssugarfield_date_start.php and change only the last line, the ‘enforced’ value from true to false. This gives me the following piece of code:

            $dictionary[‘Task’][‘fields’][‘date_start’][‘importable’]=’false’;
            $dictionary[‘Task’][‘fields’][‘date_start’][‘merge_filter’]=’disabled’;
            $dictionary[‘Task’][‘fields’][‘date_start’][‘calculated’]=’true’;
            $dictionary[‘Task’][‘fields’][‘date_start’][‘formula’]=’now()’;
            $dictionary[‘Task’][‘fields’][‘date_start’][‘enforced’]=false;

            I then run a quick repair and my Start Date is editable, but has no value whatsoever, even though in Studio I still see it as a calculated value, with the right formula.

            Thanks for taking the time to look at my issue here!

            • 

              Because the formula does not refer to another field, it has nothing to trigger off of and will never fire. When enforced is true, the field is calculated during page load so this isn’t a problem.

Trackbacks and Pingbacks:

  1. Case Due Date + 3 days from create date - May 22, 2012

    […] […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s