Extend Advanced Workflow configurable actions to trigger custom PHP code

Here it is a ready-to-use example on how to extend Advanced Workflows leveraging UI configurable custom PHP actions. It is based on the original example from SugarCON last year.

I've made it so that it is module aware (the developer can define module specific methods) and the method to call can be configured from the UI (only module specific methods will appear on the workflow).

The code is here: https://github.com/esimonetti/SugarAdvancedWorkflowCustomPHPMethods 

It will be extremely beneficial in situations where complex actions are required (eg: multiple lookups and records save at once), and UI action configuration/selection at the workflow level is required. The admin users with this type of customisation can have control on when and how the complex actions executed on the PHP side are triggered and have the flexibility to change the conditions that trigger them through the workflow UI.

The custom actions can be sent to the background and executed from the PHP side asynchronously, leveraging further our product's job queue to improve UI responsiveness for end users.

This should not be used as the silver bullet for everything, but it is a solid example to start with.

Feel free to have a look at the repository for more info!

Cheers

Parents
  • Hey Enrico,  

    Thanks for the post!  I am doing a more simplified custom advanced workflow on a vagrant localhost SugarCRM instance to test proof of concept. I have been following Robert Gonzalez's example: UnCon 2016 - Advanced Workflow Deep Dive Presentation .  Nothing seems to happen when I attempt to trigger the advanced workflow on an Account Record.  In the process management module it reports that my process definition is 'in progress' but never resolves.  Any thoughts on how I could debug or attempt to troubleshoot what I have done wrong?

    Thanks!

    Matt

  • Brian Hearn,

    Are you using the code examples as-is without any changes, to start with? How did it go?

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

  • Enrico,  I added some loggin to the PHP side:

    $GLOBALS['log']->test('This is my test log message');

    This does not write to the log.  I used the code example as is without any changes.  Once again I have a localhost version 8.0 on SugarCRM on a Vagrant box.  

    Thanks for your time.

  • Brian Hearn,

    Do you mean: $GLOBALS['log']->fatal('here'); ?

    How about checking the apache error logs and php error logs (if separate), to see if you have any errors there?

    Do you see any api calls failure on the network tab?

    When you mention that you used the code example without any changes, which example are you referring to? The example on this thread, or which one of the other two that I have mentioned, so that I can attempt to see if it is a compatibility issue of the customisation?

    There are so many details that will need to be provided, to be able to get closer at debugging this.

    Some that come to mind are:

    1. Has the system's quick repair been executed correctly?
    2. Do you see the additional customised UI element on the workflows UI?
    3. Do you have a screenshot of your process design?
    4. Do they have a beginning and an end on the process?
    5. Do you have a screenshot of your "stuck in progress" processes, to see if we can understand anything from there, on where they get stuck? (follow Administration -> Process Management -> Preview Icon on the stuck process)
    6. What are the messages on the logs (PHP, Apache, sugarcrm.log, advanced workflows logs)?
    7. If you remove the custom action from the process UI, and use an out of the box action (eg: update a field on the module), does the process complete successfully or not?

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

  • Enrico- 

    Thank you very much for your help.  As I walked through your checklist I was able to Robert Gonzalez's example: uncon/awf_deep_dive/src/custom at 2016 · sugarcrm/uncon · GitHub 

    I was able to get it to work. I had misspelled a php file.

    I am a SugarCRM newbie and I am wondering what would be the significance of creating a custom pmse object in the project manager library over creating a custom action by extending an already existing out of the box pmse object? 

    I have been reading over the documentation for Advanced workflow and the Process Manager in the documentation but was wondering what would be a good use case for creating a custom PMSE object.

    Two more questions if you have time:

    - What is the main difference of logic hooks vs advanced workflow besides the UI editor in the advanced workflow?

    - If I push a bunch of new records through the REST API to a module that has a Advanced Workflow or a logic hook registered on a new record event, will I run into blocking issues?

    Thanks!

  • Hi Brian Hearn,

    I am glad to hear you got it working, great!

    1) I don't think you create a custom PMSE object, you would always extend something that is already existing and it will be loaded by the getPMSEObject(). See more here: http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_8.0/Architecture/Advanced_Workflow/Exten… 

    The website lists what can be customised to date, depending on the specific need and requirement.

    2) Well, the UI editor first of all makes the actions configurable and flexible for the users. Say tomorrow the business changes a field requirement, or needs to change values of fields based on some conditions, it can be done through some clicks of mouse, instead of a software release.

    Aside from the UI, you have things like routings (that would take a considerable amount of time to implement through hooks), email notifications, time elapse (that can help with escalations and follow up), approval processes, setting conditionally of fields read only while the records is in certain state etc. I would suggest to try its functionalities out, there are also some examples on our website.

    All in all, going a little deeper, the workflows is executed either by an after save logic hook or if timer based, by a background queue, or by an action on the approval process dashlet.

    3) It depends. Usually you would not build a workflow that starts when every record of module X is saved. You would narrow down its start conditions based on the exact scenarios you want to cover (eg: when field one is A, field two is B, and field 3 changes from C to D). If you only have actions, yes those are synchronous, so I highly recommend to first define and design all the business processes required to meet the requirements, and then implement them in Sugar in the most optimal way, and eventually tweak them along the way.

    You have also the option of leveraging timers that can be added to help "push to the background" actions that are not required to be executed synchronously and/or by the current user.

    I did build the sample customisation listed at the beginning of this thread, to give some flexibility to the UI administrators, to select specific custom actions provided, and at the same time to help developers build custom actions that can be leveraged by the UI administrators to achieve quickly their goals.

    For example with the custom PHP method customisation example above, you could leverage it to build a "Synchronise with ERP" and "Synchronise with Marketing Automation" functionality. The functionality could asynchronously communicate with the ERP/Marketing Automation, by adding a job queue via PHP synchronously, and then executing the sync process later on. You could build multiple actions to do multiple things specific to your business, and then the UI Administrators can leverage those actions when appropriate to do so, and have the flexibility of changing the trigger points via a point and click UI. The same way you could build widgets to get the latest exchange rate, or go to google translate and translate your string, or geolocate an address, validate a phone number etc etc.

    Hope this makes sense

    Cheers

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

  • Enrico- 

    Thank you very much.  Your whole write up makes perfect sense to me.  I appreciate all your insight and advice.  I think the last step in my journey is understanding the job queue and learn how to push custom php actions to the queue to execute asynchronously.  In your post you said:

    You have also the option of leveraging timers that can be added to help "push to the background" actions that are not required to be executed synchronously and/or by the current user

     

    In your example on github: GitHub - esimonetti/SugarAdvancedWorkflowCustomPHPMethods: SugarCRM's Advanced Workflow custom PHP actions  you specify in the readme that you are leveraging timers to push actions to the job queue.

    In my situation I would like to be able exactly that- to push my custom php activity in my Advanced Workflow to the background. I read through the job queue in the documentation:  http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_8.0/Architecture/Job_Queue/  and looked through the example of pushing a logic hook to the job queue (although I was not about to get it to work in a proof of concept). 

    My question is --

    1) where in your example of github are you pushing the custom action to the background?  Is that happening out of the box when you set up a wait event in the Advanced Workflow UI?  Or is there some logic that I am not recognizing in your code.

     

    Thanks in advance.

  • Hi Brian Hearn,

    No problem!

    Yes, you are correct, it is the Advanced Workflows timer that will make the system push the action in the background through the workflow engine (see screenshot at the bottom of the readme).

    A possible alternative is to use Advanced Workflows with a custom action that interacts directly with the job queue (and then it has the processing side of the queue to pick the action back up). In this case there would be no need of using timers, but then you would be forking your functionality even further from the actual process engine.

    Cheers

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

  • Enrico- 

    It's me again.  If I wanted to create a popup and some custom definitions for my custom action I would follow your example for the SearchableCombobox in 

    SugarAdvancedWorkflowCustomPHPMethods/activity.js at master · esimonetti/SugarAdvancedWorkflowCustomPHPMethods · GitHub -

    Here is your code I am referring to:

    var comboApi = new SearchableCombobox({
    label: translate('LBL_CUSTOM_LOGIC_TO_CALL'),
    name: 'act_service_method',
    placeholder: translate('LBL_PMSE_FORM_OPTION_SELECT'), etc....

    In my custom definition popup if I wanted to do something slightly different, like instead of an option set, create a textfield that the user types in.  Then that textfield would be passed to my pmse custom action.  How would I go about doing this?

    Would I create an instance of ComboboxField? 

    How would I go about implementing the functionality in this class? Any more info I could read to understand the different properties and how to use these objects that are located in the SugarCRM core pmse\activity.js file. 

    I appreciate it!

  • Brian Hearn,

    That's a good question. Currently I do not have a customisation example that implements something similar to what you are looking for. I will make sure to share it here, if it becomes available.

    Perhaps after a quick glance, it looks like what you might want to look further into, might be TextField?

    It does feel like you are looking at the right places though, that can lead you on how to implement what you are looking for.

     

    Cheers

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

  • Hey Enrico Simonetti -

    Hope you are doing well!  I am seeing this issue with cron jobs and didn't know if you have any thoughts... Thanks!

    Every so often I am seeing this error in my SugarCRM logs when pushing a custom workflow to the Job queue.  Having a hard time tracking it down- not a lot of consistency.  It sometime happens when I push a bunch of jobs in the queue in a short period of time.  This error brings down the cron and I have to restart it to get it functional again.  Any help would be much appreciated!  Thanks!

    Fri Jul 13 19:47:24 2018 [4317][1][FATAL] Job fa289d96-86d3-11e8-8cd5-0800270fcb6c (Advanced Workflow Scheduled Job) failed in CRON run Fri Jul 13 19:47:24 2018 [4317][1][FATAL] An exception occurred while executing 'UPDATE job_queue SET name = ?, date_modified = ?, scheduler_id = ?, execute_time = ?, status = ?, resolution = ?, message = ?, target = ?, data = ?, requeue = ?, retry_count = ?, failure_count = ?, job_delay = ?, client = ?, percent_complete = ?, job_group = ?, module = ?, fallible = ?, rerun = ?, interface = ?, assigned_user_id = ? WHERE (id = ?) AND (deleted = ?)' with params ["Advanced Workflow Scheduled Job", "2018-07-13 19:36:02", "0285cb5c-86d0-11e8-9b0b-0800270fcb6c", "2018-07-13 19:36:02", "done", "failure", "Warning [2]: Creating default object from empty value in \/vagrant\/custom\/modules\/pmse_Inbox\/engine\/PMSEElements\/PMSEMyCustomAction.php on line 56

    Here is my code in PMSEMyCustomAction.php starting on line 56.  Nothing out of the ordinary.

    $bean->assigned_user_name = $variable1['userFullName'];
    $bean->assigned_user_id = $variable1['userId'];
    $bean->assigned_user_link->full_name = $variable1['userFullName'];
    $bean->assigned_user_link->id = $variable1['userId'];

    $bean->save();

  • Hi Brian Hearn,

    As pointed by the error, it would be good to understand what is wrong with line 56 on the custom code.

    I would highly recommend to have a look how to interact with beans (and related beans) from code, before proceeding further with customising the system in such complex scenarios like Advanced Workflows, as the code pasted does not look right. Link fields are used to load and/or add related records, not to store data within them directly.

    Some material that can help:

    Start maybe with simpler examples like a standalone custom api, and see if you can get the functionality working there first, by passing the input with a tool such as Postman. A great starting point would be Lauren Schaefer's tutorial here: Tutorial: Intro to the Sugar REST v10 API and all the ProfessorM scenarios as well.

    Hope it helps you move forward

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

  • Thank you.  

    "Link fields are used to load and/or add related records, not to store data within them directly."

    Was what I needed. 

    I will also look into your other recommendations.

    I appreciate it!

Reply Children
No Data