Api send request trigger when new record has come into DB

Need to implement sending a new request when trigger has found a new record in DB
Could anyone help me please with this?

Hi,
Suite has an event-based hook layer that can faciliate such a logic:

you e.g. create an after-save-hook for the respective module, and when triggered, you create and send your request to a third party.

Great
Thank you so much

Could you tell me please is there any way to fire a request on trigger “after_save”? Because I need to intercept some how this data and send it to another API
?

Or maybe we could add some additional plugins to have this “send request to API” feature?

Hi,
did you successfully create any hook yet? The page above should show you the necessary steps (registering the hook and the file that holds the additional code)?

The following example deals with new accounts:

Hook registration (\custom\modules\Accounts\logic_hooks.php):

$hook_array['after_save'][] = Array(80, 'myAftersave', 'modules/Accounts/MyHooks.php','MyHooks', 'myAftersave');

and this is my pseudo code. I’ve addes some useful comments that will help you in the beginning:

<?php
class MyHooks
{
    /**
     * @param $bean
     * @param $event
     * @param $arguments
     * @return void
     */
    function myAftersave($bean, $event, $arguments)
    {
        // start coding here!

        //read e.g. bean data
        $companyName = $bean->name; //current account->Name
        $url = $bean->website; //current account->Website

        //validation: newly created bean or just an updated one? this "if" tests for the create-event.
        if ($bean->fetched_row == false) {
            //call third party api
            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => 'https://myserver.com/odata/Account',
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => '{
                        "name": "' . $companyName . '",
                        "url": "' . $url . '"
                }',
                CURLOPT_HTTPHEADER => array(
                    'Accept: application/json',
                    'Content-Type: application/json',
                    'ForceUseSession: true',
                    'BPMCSRF: mysecuretoken :)'
                ),
            ));
            $response = curl_exec($curl);
            curl_close($curl);

            //do something with response
            $bean->somefield_c = $response->someValue;

            //careful! this save would trigger the next after-save-event, so be sure to not create loops here.
            $bean->save();
        }
    }
}

hint: set up your IDE/local environment so that debugging works. It will help you a lot, especially in the beginning.

Thanks for helping me
I have another question. Is there any way to manage fields that should be sent in request from admin → workflow manage (or other page, etc)?
I mean, how can we add the ability to pick some field with data and put it into request which will be send
?

Hi,
you won’t be able to call an api from the workflow module, it can only manage data in suite’s own database. the content of what you send to the external API is shown in the little script above:

CURLOPT_POSTFIELDS => 
           '{
                        "name": "' . $companyName . '",
                        "url": "' . $url . '"
            }',

My request would contain two attributes: “name” and “url”, the respective values were retrieved first with $companyName = $bean->name; and $url = $bean->website;
($bean is the record that threw the event, it contains the data that you entered while creating the record).

E: my example is meant to be easily readable, it would make sense to gather your request data first in an array and then convert it with e.g. json_encode, but that is step 2 :slight_smile: