Save Data from live website using Hook

Hello Experts

I want to save registrations from live website using hook in my Accounts module.
Can anyone help me in code regarding this hook ? Please help

Thanks

Hi,
why donā€™t you just add a web request on top of your current form actions? You could do a simple API request that uses the form information as attributes for e.g. new accounts.

E: v4 API intoduction:

Hey @crmspace thanks for reply. Hope you good. Actually its my custom form and that data is also saving in DB so i want that also store in CRM Can we create a simple hook ? how we will create hook ?

Hi,
how is your web form currently integrated? Is it using direct mysql queries to insert stuff to the CRM (like insert into accounts [...];)?
This would be a quite bad design, as this approach totally skips the whole framework and there is basically no way of triggering hooks (you could do db triggers, but this wouldnā€™t be recommendable either).

Hi,
No data is right now not storing in CRM only storing in MySQL database using Laravel, but now i want that data also store in CRM.

and web form is not yet integrating with suitecrm. Thats the point what should i do so that i get data also in my CRM ?

Hi,
When submitting the form, execute additional code, e.g. a php request like shown in the tutorial above. The API will then trigger logic hooks too.

2nd approach:
create a new suite-scheduler job that grabs these newly added records from the DB that laravel is using and save them as beans. This would also trigger hooks.

Ok if i want to store data in Accounts module then in custom/modules/Accounts/LogicHook i should create hook right ? before_save or after_save ?

Hi,
no, logic hooks wonā€™t work for you here. Those methods are only triggered if you do something in the CRM (like creating/updating a record).

If I understand correctly, you have an external application with an external db. This application has a form and each time its being submitted, a new account should be added to the CRM.
Therefore:
a) use SuiteCRMs API for that
b) use a scheduler job to retrieve all needed rows from the external db and process them within Suiteā€™s bean framework.

I personally would go for option a) as it is the most easy way in my mind.

yes exactly the same scenario as you explained. Please help me how to use an API for that as i have only worked with hooks not with an API. please help

Iā€™ll try to summarize the requests you need to do:

  1. login and retrieve a session id
  2. create the account with given attributes (using set_entry or set_entries)
  3. (optional) after-care tasks (e.g. linking related records)

A general introduction has already been linked above. Additionally, you might want to have a look at service/v4_1/rest.php or respectively service/v4_1/soap.php to get an overview of all methods available.

Note: I personally recommend to test requests first with additional tools (like postman). If I get the expected results, I continue doing the actual implementation.
Note 2: there is a newer API version available too, v8. You can try using it, but afaik it is still a bit too buggy to use it in a productive environment.

1 Like

Here is the rest api example as suggested by @crmspace

ok Thank you @crmspace

Ok Thank you @sagarjaydeep

Hi,
with the help of your above reply i have created this API now i have to text this on postman how can we do that. I have created one file inside CRM folder and write this code now i have to test it postman ? please help ?

<?php

// specify the REST web service to interact with

$url = ā€˜http://example.com/suitecrm/service/v4_1/rest.phpā€™;

// Open a curl session for making the call
$curl = curl_init($url);

// Tell curl to use HTTP POST

curl_setopt($curl, CURLOPT_POST, true);

// Tell curl not to return headers, but do return the response

curl_setopt($curl, CURLOPT_HEADER, false);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Set the POST arguments to pass to the Sugar server

$parameters = array(

'user_auth' => array(

    'user_name' => 'user_name',

    'password' => md5('password'),

    ),

);

$json = json_encode($parameters);
$postArgs = array(
ā€˜methodā€™ => ā€˜loginā€™,
ā€˜input_typeā€™ => ā€˜JSONā€™,
ā€˜response_typeā€™ => ā€˜JSONā€™,
ā€˜rest_dataā€™ => $json,
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);

// Make the REST call, returning the result
$response = curl_exec($curl);
// Convert the result from JSON format to a PHP array
$result = json_decode($response);
if ( !is_object($result) ) {
die(ā€œError handling result.\nā€);
}
if ( !isset($result->id) ) {
die(ā€œError: {$result->name} - {$result->description}\n.ā€);
}
// Echo out the session id
echo $result->id."
";

$session = $result->id;

$parameters = array(
ā€˜sessionā€™ => $session, //Session ID
ā€˜moduleā€™ => ā€˜Leadsā€™, //Module name
ā€˜name_value_listā€™ => array (
array(ā€˜nameā€™ => ā€˜first_nameā€™, ā€˜valueā€™ => ā€˜Davidā€™),
array(ā€˜nameā€™ => ā€˜last_nameā€™, ā€˜valueā€™ => ā€˜Borisā€™),
array(ā€˜nameā€™ => ā€˜statusā€™, ā€˜valueā€™ => ā€˜Newā€™),
array(ā€˜nameā€™ => ā€˜lead_sourceā€™, ā€˜valueā€™ => ā€˜Web Siteā€™)
),
);
$json = json_encode($parameters);
$postArgs = ā€˜method=set_entry&input_type=JSON&response_type=JSON&rest_data=ā€™ . $json;

curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);

// Make the REST call, returning the result
$response = curl_exec($curl);

// Convert the result from JSON format to a PHP array
$result = json_decode($response,true);

// Get the newly created record id
$recordId = $result[ā€˜idā€™];

print "New Record Created with ID ".$recordId;

Note: When i am passing parameters in postman then also its creating ā€œDavidā€ entry in Leads Module.

great, so you managed to log in via postman and you created a lead. From there, you can either generate code to be copied to your project (postman feature), directly executing your example, or you can rewrite the request like shown above.

You do not have to place code within the CRM, you have to attach this code to your web formā€™s post action.

Postman is btw just a recommendation, it is not necessary to do this extra step.

Hi sir thank you so much for supporting.
When i am hitting this url => http://example.com/suitecrm/service/v4_1/rest.php

then its giving me this response

{
"name": "Invalid Login",
"number": 10,
"description": "Login attempt failed please check the username and password"

}

my username and password are correct.

did you provide the password as plain text? You have to use its md5-hash instead (in php md5("your-password");).

yes i have used md5 also

your screenshot shows that you donā€™t submit the rest_data, therefore all mandatory fields are missing.