Announcement

Collapse
No announcement yet.

Execute a formula with custom button

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Execute a formula with custom button

    How can I execute a formula (in this case ext/email/send) with a custom button in detail view. If not possible, could anybody provide the necessary action to achieve this. Thanks in advance.

  • #2
    not sure what is your use case, but the ext/email/send require first to create an email and then execute sending it. if you want to implement that on detail view who/where the email will be build ?

    Comment


    • #3
      A Workflow with the manual trigger will do the job.

      Comment


      • #4
        My solution has always been to create a hidden boolean field (default is "false"), then create a button that changes the field's boolean state to "true", the "true" state triggers the formula process at the end of which add a command to change the field's boolean back to "false".

        Example:

        PHP Code:
        ifThen(entity\isAttributeChanged('hiddenBooleanField')
            && 
        hiddenBooleanField == true,
            
        $sendEmailButton record\create(
                
        'Email',
                
        'from''email@address.mail',
                
        'to'emailAddress,
                
        'status''Draft',
                
        'isHtml'true,
                
        'parentId'id,
                
        'parentType''ParentType',
        );
            
        ext\email\applyTemplate($sendEmailButton'templateIdNumber');
            
        ext\email\send($sendEmailButton);
            
        hiddenBooleanField false;
        );
        ​ 
        Last edited by heligonaut; 03-18-2024, 02:19 PM.

        Comment


        • shalmaxb
          shalmaxb commented
          Editing a comment
          heligonaut, thank you very much. This is, what I had been looking for.

      • #5
        I tried, but I do not find the right code for the button action. Could you help again?

        Comment


      • #6
        Simple example

        PHP Code:
        actionMyActionForButton: function (datae) {
            
        this.view.model.set('hiddenBooleanField'true);
            
        this.view.model.save();
            
        Espo.Ui.notify("Action OK!"'success'5000);

        Comment


        • shalmaxb
          shalmaxb commented
          Editing a comment
          heligonaut, thank you very much again. I guess I do not have enough skills in javascript.....
          I will try with this one, thanks again.

      • #7
        I copied the code exactly from the espoCRM doc but I do not even get the button to appear. I always get the error "JSON syntax error". Here is my code for the button:

        PHP Code:
        "menu": {
        "detail": {
        "buttons": [
        "__APPEND__",
        {
        "label""Fragebogen absenden",
        "name""fragebogenAbsendenButton",
        "action""fragebogenAbsendenAction",
        "style""default",
        "acl""edit",
        "aclScope""Software",
        "data": {
        "handler""custom:fragebogen-absenden-action-handler"
        },
        "initFunction""initManageFragebogenAbsendenAction"
        }
        ]
        }
        }, 


        I do not see anything wrong. Anybody does?

        Comment


        • #8
          As the error message says, JSON syntax is invalid. There should not be the trailing comma, and your entire code should be wrapped into { ... }. https://docs.espocrm.com/development...p-right-corner

          Comment


          • shalmaxb
            shalmaxb commented
            Editing a comment
            The comma ist there, because there follows other code in the file Software.json. Because of that also no wrapping {....}, as there are already these wrapping (main) braces. Or do I understand something wrong?

        • #9
          For few days I am trying (with the above help) to implement this in my custom entity, but do not get it to work. I acnnot see, if the button would be displayed, as the detal view does not open.
          As I do not get it to work, I want to ask for pointing me to the direction, where I did something wrong.
          So I will post all files involved.
          My custom entity is named "Software". The action I want to execute is "fragebogen absenden" (fragebogen means questionnaire), which will be the attachment to send by mail.
          The file custom/Espo/Custom/metadata/Resources/clientDefs/Software.json is already present, as there is some of the configuration of fields etc..
          I want to display a button, which should execute a formula to send an email with an attachment.

          My files:

          custom/Espo/Custom/metadata/Resources/clientDefs/Software.json: I added this code

          Code:
          "menu": {
                               "detail": {
                                       "buttons": [
                       {
                          "label": "Fragebogen absenden",
                          "name": "fragebogenAbsendenButton",
                          "action": "fragebogenAbsenden",
                          "style": "default",
                          "acl": "edit",
                          "aclScope": "Software",
                          "data": {
                                 "handler": "custom:fragebogen-absenden-action"
                            },
                            "initFunction": "fragebogenAbsenden"
                     }
                 ]
            }
          }
          Handler: client/custom/src/software/fragebogen-absenden-action.js

          Code:
          Espo.define('custom:views/software/fragebogen-absenden-action', 'views/detail', function (Dep) {
          
                              return Dep.extend({
                                       setup: function() {
                                             Dep.prototype.setup.call(this);
          
                                        this.addMenuItem('buttons', {
                                             name: 'fragebogenAbsenden',
                                             label: 'Fragebogen absenden',
                                             style: 'primary',
                                             acl: 'edit',
                                             action: 'fragebogenAbsenden'
                                        }, true);
                                  },
          
                                      actionFragebogenAbsenden: function (data, e) {
                                      this.view.model.set('hiddenBooleanField', true);
                                      this.view.model.save();
                                      Espo.Ui.notify("Fragebogen gesendet", 'success', 5000);
                                      }
          });
          Formula in entity Software:

          ifThen(entity\isAttributeChanged('hiddenBooleanFie ld')
          && hiddenBooleanField == true,
          $sendEmailButton = record\create(
          'Email',
          'from', 'email@address.mail',
          'to', emailAddress,
          'status', 'Draft',
          'isHtml', true,
          'parentId', id,
          'parentType', 'ParentType',
          );
          ext\email\applyTemplate($sendEmailButton, 'templateIdNumber');
          ext\email\send($sendEmailButton);
          hiddenBooleanField = false;
          );​

          Thank you in advance.


          Comment

          Working...
          X