Best practice to send data to an external site and displaying the return in sugar as an alert

Hi all,

I have a custom module of printers, when certain fields are updated in the record I want to send that record data to an external laravel site to update its database.

I currently use logic hooks to do this

    $hook_array['after_save'] = Array(); 
    $hook_array['after_save'][] = Array(
        1,
        'Update Serial Cache',
        'custom/modules/S_SerialNumber/update_serial_cache.php',
        'update_serial_cache',
        'update_to_cache',
    );

    $hook_array['before_save'] = Array(); 
    $hook_array['before_save'][] = Array(
        1,
        'Update Serial Cache',
        'custom/modules/S_SerialNumber/update_serial_cache.php',
        'update_serial_cache',
        'get_record',
    );

 the update_to_cache function checks if the fields I want to check have changed and if they have pushes that data to our laravel site over curl to a custom endpoint. The problem is Id like to inform the user with an alert box that the cache was updated or display the error returned from laravel

is it possible to do this in sidecar and have a custome script  running an ajax reguest to the laravel server then using the alerts  to display the return?

I cant use the built in web_logic_hooks as I only want this to happen if certain fields are updated. 

this is my logic hooks custom class

   class update_serial_cache
    {
        public function get_record($bean, $event, $arguments)
        {
            $bean->stored_fetched_row_c = $bean->fetched_row;
            // sugar_die('just a test sugar_die.');
        }

        public function update_to_cache($bean, $event, $arguments)
        {
            if (!$bean->cached_c) {
                $GLOBALS['log']->warn('Not cached: exiting');
                return false;
            }

            $update = false;            
            if (isset($bean->stored_fetched_row_c)) {
                $GLOBALS['log']->warn('OLD: '.json_encode($bean->stored_fetched_row_c));
                // Warranty change 
                if ($bean->stored_fetched_row_c['warrantystart_c'] != $bean->warrantystart_c) { 
                    $GLOBALS['log']->warn('New: '.$bean->warrantystart_c);
                    $update = true;
                }
                if ($bean->stored_fetched_row_c['warrantyend_c'] != $bean->warrantyend_c) {
                    $GLOBALS['log']->warn('New: '.$bean->warrantyend_c);
                    $update = true;
                }
                if ($bean->stored_fetched_row_c['warrantystatus_c'] != $bean->warrantystatus_c) {
                    $GLOBALS['log']->warn('New: '.$bean->warrantystatus_c);
                    $update = true;
                }                
                if ($bean->stored_fetched_row_c['status'] != $bean->status) {
                    $GLOBALS['log']->warn('New: '.$bean->status);
                    $update = true;
                }
                // Dealer change
                if ($bean->stored_fetched_row_c['dealer_id_c'] != $bean->dealer_id_c) {
                    $GLOBALS['log']->warn('New: '.$bean->dealer_id_c);
                    $update = true;
                }
                if ($bean->stored_fetched_row_c['dealersoldto'] != $bean->dealersoldto) {
                    $GLOBALS['log']->warn('New: '.$bean->dealersoldto);
                    $update = true;
                }
                // Product change
                if ($bean->stored_fetched_row_c['product_code_related_c'] != $bean->product_code_related_c) {
                    $GLOBALS['log']->warn('New: '.$bean->p_product_s_serialnumber_1_name);
                    $update = true;
                }

                // Update Cache
                if ($update) {
                    $args = array(
                        'Serial_Number' => MC_ContactsApi::obfuscate('encrypt', $bean->name),
                    );
                    $printer    = MC_SerialNumberApi::serial_number_check(null, $args);
                    $GLOBALS['log']->warn('NEW: '.json_encode($printer));

                    // CURL
                    $curl       = curl_init("https://services.magicard.com/hs/public/serial-cache");
                    curl_setopt($curl, CURLOPT_HEADER, false);
                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($curl, CURLOPT_POST, true);
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $printer['records']);
            
                    $response   = curl_exec($curl);
                    $err        = curl_error($curl);
                    curl_close($curl);
            
                    if ($err) {
                        $GLOBALS['log']->warn("cURL Error #:" . $err);
                        return false;
                    }
            
                    $GLOBALS['log']->warn('RESP: '.$response);
                    return true;
                }
                return false;
            }
        }

i used the log  just to check the hook was firing

  • Hello John,

    Two points

    1.
    The integration could be initiated not from record view either - in general, the field value could be changed in different ways in Sugar -  with list view mass update, SugarBPM, ia API, another logic hook.
    It means there could be no record view context for the alert box, moreover the user to notify may not be logged in.

    Therefore, we recommend arranging a special record field that will show the results of the recent external update along with response details.

    Let me show an example
    My example gets Currency rate from external service -  on field value is changed, while the currency codes are values of Name field.





    2.
    I would say that the best practice to keep integration implementation manageable is to configure it rather than develop it with code.

    This case is totally configurable with the Logic Builder no-code tool (https://logicbuilder.integroscrm.com)

    The implementation drawing of the example fits a single monitor screen (zoom in, then follow the while line to read):


    For a case, here is the loadable zip file, which has been generated with a click from the example drawing (pls  use Module Loader to install zip)
    ExternalRESTCall.zip

    I hope this helps and I would be happy to help with configuring API integration to your website.

    BTW, in case there are restrictions for system interactions due to security reasons, the LB DataBridge could help to arrange middleware-based integration via configuring - zero spends on integration data traffic.

    Best Regards,
    Dmytro Chupylka

    integroscrm.com
    We make work in Sugar CRM system faster, more convenient and efficient

  • Hello friend, I understand that your use case is when a user makes the modifications or all the times they make modifications.