I have a custom module and I am uploading information through the suitecrm REST API v4.1, in this module there is a field for an image but I cannot find how to upload it correctly.
Here is the example of my code:
$set_entry_parameters_mascota = array(
//session id
"session" => $session_id,
//The name of the module from which to retrieve records.
"module_name" => " <Module custom>",
//Record attributes
"name_value_list" => array(
array('name' => 'name', 'value' => $data),
array('name' => 'description', 'value' => $data),
array('name' => 'especie', 'value' => $data),
array('name' => 'raza', 'value' => $data),
array('name' => 'color', 'value' => $data),
array('name' => 'color2', 'value' => $data),
array('name' => 'genero', 'value' => $data),
array('name' => 'caracter', 'value' => $data),
array('name' => 'segmento_c', 'value' => $data),
array('name' => 'foto', 'value' => ) //this field in the database appears as a varchar so I
don't understand exactly how to upload it
),
);
There is no API for that but you can extend the API with this function:
function set_image($session,$module_name,$id,$field,$contents,$deleted=0) {
global $sugar_config;
$error = new SoapError();
if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'write', 'no_access', $error)) {
$GLOBALS['log']->info('End: SugarWebServiceImpl->set_image');
return;
} // if
$filename = $sugar_config['upload_dir'].$id.'_'.$field;
if ($deleted == 0) {
$contents = base64_decode($contents);
file_put_contents($filename,$contents);
}
else {
if (file_exists($filename)) {
unlink($filename);
}
}
$GLOBALS['log']->info('End: SugarWebServiceImpl->set_image');
return array('id'=>$id, 'entry_list' => array('filename'=>$filename));
}
Then, after calling the set_entry API, you can call that set_image API.
The parameters should be:
“session” => $session_id,
“module_name” => " Module custom",
“id” => “The id of the record as returned by set_entry”,
“field” => " foto",
“contents” => “The file contents (base64 encoded)”,
Hello, thanks for answering, reviewing the feedback you gave me and implementing, but I got an error that I could not solve the structure of what I did this way:
directori created:
//////////////////////////////////////////////////////////////////////
SugarWebServiceImplv4_1_custom.php
SugarWebServiceUtilv4_1_custom.php
just copy the existing files and add the custom.
//////////////////////////////////////////////////////////////////////
Add the function also to the SugarWebServiceImpl.php file
but in console I get this error:
Fatal PHP error: ArgumentCountError not caught: Too few arguments to work SugarWebServiceImpl :: set_image (), 3 passed in /<crm> / service / core / REST / SugarRestJSON.php on line 94 and at least 5 expected in /<crm>/service/core/SugarWebServiceImpl.php:1203
Stack Tracking:
# 0 /<crm>/service/core/REST/SugarRestJSON.php(94): SugarWebServiceImpl-> set_image ('80e29ec3455b22e ...', 'GRAL_Mascota', Array)
# 1 /<crm>/service/core/SugarRestService.php(136): SugarRestJSON-> serve ()
# 2 /<crm>/service/core/webservice.php(70): SugarRestService-> serve ()
# 3 /<crm>/service/v4_1/rest.php(56): require_once ('')
# 4 {main}
launched at /<crm>/service/core/SugarWebServiceImpl.php on line 1203