How to implement JS EventListener on specific fields with Sugar10?

I´ve a problem to enable onChange Events within Create/Edit a Opportunity.

I have these 3 fields:

- invoicestartdate1_c (a date with datepicker)
- runtime_c (a integer field which hold the value for "how much month")
- invoiceenddate1_c (a date with datepicker)

What i would achive:

If the user change the field "invoicestartdate1_c" or the field "runtime_c" the field "invoiceenddate1_c" should be new calculated and set.

All should happen in the frontend.

Current solution seems to work fine so far - but the datepicker of the target field shows the new date only on the second click. Is there a way to "trigger refresh" for the datepicker field?

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
initialize: function (options) {
this._super('initialize', [options]);
this.model.on('change:runtime1_c',this._updateInvoiceEndDate);
this.model.on('change:invoicestartdate1_c',this._updateInvoiceEndDate);
},
_updateInvoiceEndDate: function(model)
{
if ($('[name="save_button"]').is(":visible")) {
runtime = model.get("runtime1_c");
startdate = model.get("invoicestartdate1_c");
if (parseInt(runtime) > 0 && startdate !== undefined) {
console.log(runtime, startdate)
model.set('invoiceenddate1_c', startdate);
}
}
},
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Hi Michael

    Your code works fine over sugar 10 and 11.

    I don't identify the problem you are presenting.

    Kind regards

  • If it is a simple set value on an date why don't you use sugarlogic?

  • it looks like the idea is that the two fields reflect the same thing. So if you set the runtime directly, it will auto-adjust the starttime, if you set the starttime directly, it will update the runtime. So SugarLogic would at the very least require "enforced" to be set to false so that they can be directly interacted with (and I can't remember if that's actually the right property or if that would even work).

  • hmmm, sorry, on second reading, I see that updating startdate field does not update runtime, it just updates startdate. But only if runtime is set. So maybe the idea is allow the user to set startdate directly if runtime is empty, but if runtime has a non-zero value, startdate should always be based on that runtime, even if directly changed.

    If that's the case, then maybe the solution would be a Sugar Logic dependency for ReadOnly for the startdate field so that if runtime has a value, startdate is readonly, if it does not have a value, it is editable.

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_11.1/Architecture/Sugar_Logic/Dependency_Actions/ReadOnly/

    Like 

    'triggerFields' => array('runtime','startdate'),
       'actions' => array(
            array(
                'name' => 'ReadOnly',
                'params' => array(
                    'target' => 'startdate',
                    'value' => 'greaterThan($runtime,0)',
                ),
            ),