Workflow Button for Linking Record with current user- Workflow Formula Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harrytruman
    Member
    • Jun 2023
    • 98

    Workflow Button for Linking Record with current user- Workflow Formula Question

    Greetings everyone,

    I want to create a manual (button) workflow trigger that does the following:

    -Button is displayed on record details page

    -When button is pressed, a link between the (target) record and the current logged in user is created

    -The user ID will be displayed in a field called 'users' and added to a current list. This field is a link multiple field.



    Here is what I've tried (please see attached screen shot).



    I've tried everything but no luck. Any help would be greatly appreciated. Thanks!

  • esforim
    Active Community Member
    • Jan 2020
    • 2204

    #2
    Interesting, I never knew you can create button with Workflow.

    Sorry I can't help but look like you are working with "array" type of field (relationship/multiple link), getting these to work can be difficult.

    I recommend you test your button with a simpler field first to see if the button work at all, after that then you would move to do the real thing. You might always want to use Sandbox Formula to test to see if you can create link before using it in your workflow.

    Personally I'm still trying to warp my head around these type of field.

    Comment

    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #3
      Hey @harrytruman

      First of all to make this to work you need to create a relationship between the user and your entity (n:n => many to many) see this https://docs.espocrm.com/administrat...rtain%20record.
      Once you have create a relationship and add the field assignedusers to your layout then you will be able to achieve this easily with the workflow. i have attached two screenshots (first is the relationship i have defined between user and account entities as a many to many) - second screenshot show how the workflow would work with the correct formula code needed to save the current logged in user to the assignedusers.

      let me know if that work for you, i have tested it and it working fine.
      Attached Files
      Rabii
      Web Dev

      Comment

      • harrytruman
        Member
        • Jun 2023
        • 98

        #4
        Hi rabii Thank you so much for your kind help here! I struggled a bit but when trying to tweak it for my specifics, but the struggle really helped me learn. I needed to change this to allow a user to add hospitals to their list. After a bit of trial and error here is what I ended up with (I also added automatic follow each time this workflow is triggered). See screenshot.

        By the way, ChatGPT was helpful here by asking it to explain the formula you provided. Here is the AI answer (ChatGPT was wrong about the syntax errors though, your formula was perfect):



        "The formula you provided seems to be attempting to add the user ID to an array called assignedUsersIds. However, there are some syntax errors in the formula you provided. Here's a corrected version of the formula:
        lessCopy code
        assignedUsersIds = array_push(assignedUsersIds, env_userAttribute('id'))
        In this formula, the array_push function is used to add the user ID (env_userAttribute('id')) to the assignedUsersIds array. The env_userAttribute('id') is used to retrieve the user ID of the current user."

        Comment

        • rabii
          Active Community Member
          • Jun 2016
          • 1250

          #5
          Chatpgt wouldn't get the correct formula used in espocrm. i formula i give is the correct one all you need to do is to changed assignedUsersIds to the correct field you want the current user to be added in.

          Did you manage to get to work ?
          Rabii
          Web Dev

          Comment

          • yuri
            Member
            • Mar 2014
            • 8440

            #6
            This should work either:

            Code:
            entity\addLinkMultipledId('users', env\userAttribute('id'));

            Or (if you don't have link-multiple):

            Code:
            record\relate('YourEntityType', id, 'users', env\userAttribute('id'));
            But the latter should be used only in Execute Formula Script action, not in Update Target Record. Otherwise, side effects occur.
            Last edited by yuri; 06-29-2023, 07:24 PM.
            If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

            Comment

            • harrytruman
              Member
              • Jun 2023
              • 98

              #7
              Originally posted by rabii
              Chatpgt wouldn't get the correct formula used in espocrm. i formula i give is the correct one all you need to do is to changed assignedUsersIds to the correct field you want the current user to be added in.

              Did you manage to get to work ?
              Got it to work perfectly. Thanks again for helping me here.

              Comment

              • macistda
                Member
                • Jul 2022
                • 76

                #8
                Here is the code how to add buttons (example for documents) before the new possibilities via "Workflow" existed. Advantage of that kind of Button, it is at the same Place and is able to change it's letters and colours etc and send an "Update" to e.g. Status-Fields.

                1) Edit the file /var/www/crm/custom/Espo/Custom/Resources/metadata/clientDefs/Document.json and insert the following lines appropriately:

                (Warning) Make sure that the file is saved in UTF-8 if it contains German umlauts, for example.

                (Warning) Pay attention to the correct JSON syntax. There is no comma at the end of an object/array.​

                Code:
                {
                    "menu": {
                        "detail": {
                            "buttons": [
                                "__APPEND__",
                                {
                                    "html": "<span class=\"fas fa-exclamation fa-sm\"></span> AR abrechnen",
                                    "name": "finishAr",
                                    "action": "finishAr",
                                    "style": "warning",
                                    "acl": "edit",
                                    "aclScope": "Document",
                                    "data": {
                                        "handler": "custom:change-ar"
                                    },
                                    "initFunction": "initChangeAr",
                                    "hidden": true
                                },
                                "__APPEND__",
                                {
                                    "html": "<span class=\"fas fa-check fa-sm\"></span> AR öffnen",
                                    "name": "openAr",
                                    "action": "openAr",
                                    "style": "success",
                                    "acl": "edit",
                                    "aclScope": "Document",
                                    "data": {
                                        "handler": "custom:change-ar"
                                    },
                                    "initFunction": "initChangeAr",
                                    "hidden": true
                                }
                            ]
                        }
                    }
                }​
                2) Create the /var/www/crm/client/custom/src/change-ar.js file

                Code:
                define('custom:change-ar', ['action-handler'], function (Dep) {
                    return Dep.extend({
                        actionFinishAr: function (data, e) {
                            this.view.model.save({"type": "AR abgerechnet"}, {patch: true})
                        },
                        actionOpenAr: function (data, e) {
                            this.view.model.save({"type": "AR offen"}, {patch: true})
                        },
                        initChangeAr: function () {
                            this.controlButtonVisibility();
                            this.view.listenTo(
                                this.view.model,
                                'change:type',
                                this.controlButtonVisibility.bind(this)
                            );
                        },
                        controlButtonVisibility: function () {
                            switch (this.view.model.get('type')) {
                                case 'AR abgerechnet':
                                    this.view.hideHeaderActionItem('finishAr');
                                    this.view.showHeaderActionItem('openAr');
                                    break;
                                case 'AR offen':
                                    this.view.hideHeaderActionItem('openAr');
                                    this.view.showHeaderActionItem('finishAr');
                                    break;
                                default:
                                    this.view.hideHeaderActionItem('finishAr');
                                    this.view.hideHeaderActionItem('openAr');
                            }
                        },
                    });
                });​
                Useful Links:
                Source Code for funktion this.view.model: https://github.com/espocrm/espocrm/b...t/src/model.js. Used Library: http://backbonejs.org/#Model-save

                Explanations:
                Save data method:
                The following method saves all fields.
                this.view.model.save({"status": "Active"})

                If you don't want this behavior, you should add the {patch: true} option to the method:
                this.view.model.save({"status": "Active"}, {patch: true})

                Espo converts the PATCH request to a PUT request because the API does not support PATCH. Excerpt from the Espo source code:​

                PHP Code:
                /**
                 * @param {string} [method] HTTP method.
                 * @param {module:model.Class} [model]
                 * @param {Object} [options]
                 * @returns {Promise}
                 */
                sync: function (method, model, options) {
                    if (method === 'patch') {
                        options.type = 'PUT';
                    }
                 
                    return Dep.prototype.sync.call(this, method, model, options);
                },
                Hope that helps if you don't want to use the standard Workflow Buttons and if you want to see / set state / text / colour for that Button.

                Clear Cache to make working. If done something wrong, correct it and clear cache again to get Espo working again
                Last edited by macistda; 06-30-2023, 11:26 AM.

                Comment

                • harrytruman
                  Member
                  • Jun 2023
                  • 98

                  #9
                  Thanks, macistda I was already getting ready to ask about custom buttons and you beat me to it.

                  I tried your code above to see if I could do it. I'm running into some problems.

                  -The file pathway you provided is different than what I see. Is it common to have different file pathways in different installations? I'm using elest.io with AWS.

                  -Got a 404 error when I tried to open a document. No button displayed.


                  I'd love to learn how to build custom buttons. I agree with you. Custom coding offers more flexibility than workflow buttons.

                  -Do you have any suggestions on a good tutorial or coding examples (maybe here or on github)? Any other suggestions you can point me to?

                  -Once I purchase the advanced pack and have access to the backend of the extension, would I be able to customize the Button colors and fonts in the files?


                  Thanks again for your kind help here. This form is amazing. I can't wait for the day when I have the expertise to contribute more.

                  Comment

                  • macistda
                    Member
                    • Jul 2022
                    • 76

                    #10
                    Hi,

                    the path depends where you have installed EspoCRM of course. "crm/custom/Espo/Custom/Resources/metadata/clientDefs/Document.json" is an example. With Apache Webserver the standard path would be /var/www/crm or /var/www/html/crm.

                    You can find the path from Frontend, Administration --> Systemrequirements (direct: https://<your-espo-host>/#Admin/systemRequirements)
                    You need ssh or SFTP or FTP(s) access to be able to customize that files. Our Espo runs at a VM Debian, not as a SaaS like "elest.io"

                    For the colors I attach a screenshot.

                    I work at a software company, so I do not code all things on my own, we have different developer employees. This code was made by a collegue first and adepted for other entities by me. We developed different things for Espo, as well Espo-internal as external (Python) scripts and programs outside of espo.

                    Examples:
                    - API connected automated workflow between Nextcloud (documents) with Webhooks automaticaly transfered to Espo and assign the Case (For working reports PDF) via n8n (n8n is a open source software to automate processes).
                    - Integration of Office365 contacts, calendars, different multiple common planning calendars for about 25 people, with "real" O365 invitations, serial meetings and more (the official Outlook extension from Espo is necessary because of Oauth) (https://forum.espocrm.com/forum/exte...ation-body-etc).
                    - API connected automated workflow between EspoCRM and Atlassian Jira for development tickets (both directions) via n8n.
                    - A Desktop Client Software (for Windows) for better handling of documents, it downloads and uploads the files automaticaly via https, and after editing with the desktop program (e.g. Word, Excel) it uploads the edited files to espo via https. We integrated two buttons for that (file view and file edit).
                    There is a URL handler defined (Windows Registry) for "espo", so every document-extension is possible to be handled by this Desktop Client for "<https://..espo..>".

                    ​I will take a look at https://elest.io/ , we do that on our own so I did not try elest.io since now.

                    Click image for larger version

Name:	docs.png
Views:	387
Size:	44.3 KB
ID:	95072
                    Click image for larger version

Name:	edc.png
Views:	390
Size:	8.1 KB
ID:	95073
                    Attached Files

                    Comment


                    • esforim
                      esforim commented
                      Editing a comment
                      That desktop thing sound cool.

                      Hope to see more of your work, coding is outside of my skill level.
                  Working...