Doc Merge Today's Date Field

Hello,

I need help finding the correct string to return today's date in a Doc Merge Template.  In PDF Manager I used a smarty modifier, but I'm having trouble 

moment().calendar(referenceDay)
Parents
  • I am not familiar with Doc Merge but if you check the Template Assistant in the help at the bottom of the page:

    it suggests functions like today and now:

    and you can copy to clipboard from there.

    FrancescaS

  • I have tried the template assistant functions for today {!formula value='today('} comes up as undefined on the document.

  • instead of {!formula value='today('} you need {!formula value='today()'}   

    i.e. a closing parenthesis. 

    I just tested with a fresh doc the following two:

    and they generated the following:

    I'll raise an Issue with the product group too to ensure this experience is improved - thank you for flagging!

  • although this is a 2 year old thread, the Template Assistant is still showing the second formula which gives the "undefined" result......

  • Hi Sarah!

    Thanks for your reply! Unfortunately, issue 91358 remains unresolved. You will have to add the ) manually to make this field work as expected.

    You may learn more about the bug here:

    https://portal.sugarondemand.com/index.php#supp_Bugs/91358

    If you'd like, you can contact support to add you to this bug. The more information we have regarding a bug, the more likely we are to get it fixed. If you would like me to do that for you, let me know, and I can! 

    -raye

  • Hi Sarah,

    I looked into this in more detail in the default behavior of the stock formula editor, functions and field names are inserted at the end of the line. However, it would be more user-friendly if these could be inserted at the current cursor position, allowing users to continue editing fluidly. The same formula editor component is also used within Studio when configuring action buttons, where additional validation ensures parentheses are balanced and function parameters are correctly supplied.

    This behavior originates from the _addFunction method, which currently does not insert a closing parenthesis ) when adding functions.

    While the editor logic remains in  ./clients/base/fields/formula-builder/formula-builder.js, I’ve implemented a custom adjustment specifically for the DocMerge Template Builder by updating the view at custom/modules/DocumentMerges/clients/base/views/tag-builder-formulas/tag-builder-formulas.js .

    The replaced view has to update the formula-editor object directly, which might not be ideal but it has fewer lines of code to add. If you decide to use it,  please make sure to test it thoroughly in staging environment to avoid unintended side effects.

    /* Original class with formulaChanged modified to add ')' on each new formula
     * @class View.Views.Base.DocumentMerges.TagBuilderFormulasView
     * @alias SUGAR.App.view.views.BaseDocumentMergesTagBuilderFormulasView
     */
    ({
        preview: '',
    
        initialize: function(options) {
            this._super('initialize', arguments);
            this.initTag();
            this.listenTo(this.context, 'change:currentModule', this._render, this);
        },
    
        initTag: function() {
            let tagBuilderFactory = new App.utils.DocumentMerge.TagBuilderFactory();
            let tagBuilder = tagBuilderFactory.getTagBuilder('directive');
            let tag = tagBuilder.newTag().setName('formula').get();
            this.tag = tag;
        },
    
        _render: function(view, selectedModule) {
            this._super('_render');
    
            this._buildFormulaBuilderField(selectedModule);
        },
    
        _buildFormulaBuilderField: function(module) {
            this.formulaField = app.view.createField({
                view: this,
                viewName: 'edit',
                targetModule: module || this.module,
                callback: _.bind(this.formulaChanged, this),
                formula: '',
                def: {
                    type: 'formula-builder',
                    name: 'formula-builder',
                },
            });
            this.formulaField.render();
    
            this.$('.row-fluid.builder').append(this.formulaField.$el);
        },
    
        formulaChanged: function (formula) {
    
            // Replace "('}" with "()'}" if present
            if (formula && formula.endsWith("(")) {
                 
                 const fe = this.el.querySelector('textarea.formula-editor');
                 if (fe && fe.value.endsWith('(') && this.formulaField &&
                        this.formulaField._formula && this.formulaField._formula.endsWith('(') ) {
    
                      formula += ")";
                      fe.value += ')';
                      // Note this could be nicer implemented through that view override or extension, but extension will also affect other places where the view is used
                      this.formulaField._formula += ")"; 
                 }
            } 
    
            this.tag.setAttribute({'value': formula});
            let tagValue = this.tag.compile().getTagValue();
    
            this.$('.preview').html(tagValue);
            this.$('.preview').attr('value', tagValue);
        }
    });

    If the view is extended, the code could potentially be simplified even further reusing the base class and updating only one function formulaChanged().

    However, the more robust solution would still be to update the logic in  ./clients/base/fields/formula-builder/formula-builder.js to automatically append the closing parenthesis when inserting functions and add it to the current cursor line of the formula-editor textarea html element. This would centralize the behavior and ensure consistency across all views that rely on the formula editor.

    WBR Yuri

Reply
  • Hi Sarah,

    I looked into this in more detail in the default behavior of the stock formula editor, functions and field names are inserted at the end of the line. However, it would be more user-friendly if these could be inserted at the current cursor position, allowing users to continue editing fluidly. The same formula editor component is also used within Studio when configuring action buttons, where additional validation ensures parentheses are balanced and function parameters are correctly supplied.

    This behavior originates from the _addFunction method, which currently does not insert a closing parenthesis ) when adding functions.

    While the editor logic remains in  ./clients/base/fields/formula-builder/formula-builder.js, I’ve implemented a custom adjustment specifically for the DocMerge Template Builder by updating the view at custom/modules/DocumentMerges/clients/base/views/tag-builder-formulas/tag-builder-formulas.js .

    The replaced view has to update the formula-editor object directly, which might not be ideal but it has fewer lines of code to add. If you decide to use it,  please make sure to test it thoroughly in staging environment to avoid unintended side effects.

    /* Original class with formulaChanged modified to add ')' on each new formula
     * @class View.Views.Base.DocumentMerges.TagBuilderFormulasView
     * @alias SUGAR.App.view.views.BaseDocumentMergesTagBuilderFormulasView
     */
    ({
        preview: '',
    
        initialize: function(options) {
            this._super('initialize', arguments);
            this.initTag();
            this.listenTo(this.context, 'change:currentModule', this._render, this);
        },
    
        initTag: function() {
            let tagBuilderFactory = new App.utils.DocumentMerge.TagBuilderFactory();
            let tagBuilder = tagBuilderFactory.getTagBuilder('directive');
            let tag = tagBuilder.newTag().setName('formula').get();
            this.tag = tag;
        },
    
        _render: function(view, selectedModule) {
            this._super('_render');
    
            this._buildFormulaBuilderField(selectedModule);
        },
    
        _buildFormulaBuilderField: function(module) {
            this.formulaField = app.view.createField({
                view: this,
                viewName: 'edit',
                targetModule: module || this.module,
                callback: _.bind(this.formulaChanged, this),
                formula: '',
                def: {
                    type: 'formula-builder',
                    name: 'formula-builder',
                },
            });
            this.formulaField.render();
    
            this.$('.row-fluid.builder').append(this.formulaField.$el);
        },
    
        formulaChanged: function (formula) {
    
            // Replace "('}" with "()'}" if present
            if (formula && formula.endsWith("(")) {
                 
                 const fe = this.el.querySelector('textarea.formula-editor');
                 if (fe && fe.value.endsWith('(') && this.formulaField &&
                        this.formulaField._formula && this.formulaField._formula.endsWith('(') ) {
    
                      formula += ")";
                      fe.value += ')';
                      // Note this could be nicer implemented through that view override or extension, but extension will also affect other places where the view is used
                      this.formulaField._formula += ")"; 
                 }
            } 
    
            this.tag.setAttribute({'value': formula});
            let tagValue = this.tag.compile().getTagValue();
    
            this.$('.preview').html(tagValue);
            this.$('.preview').attr('value', tagValue);
        }
    });

    If the view is extended, the code could potentially be simplified even further reusing the base class and updating only one function formulaChanged().

    However, the more robust solution would still be to update the logic in  ./clients/base/fields/formula-builder/formula-builder.js to automatically append the closing parenthesis when inserting functions and add it to the current cursor line of the formula-editor textarea html element. This would centralize the behavior and ensure consistency across all views that rely on the formula editor.

    WBR Yuri

Children
No Data