Hello, please, has anyone been able to send a PDF file via the workflow? I'm unable to send my file to the external API, and there are no errors in the log file. However, I can send it using JavaScript.
Announcement
Collapse
No announcement yet.
How can I use Workflow to send an HTTP request?
Collapse
X
-
Hi, In newer versions of Advanced Pack, there is a field "Payload from variable". You can assign the whole PDF file content to a variable (in an Execute Formula Script action before) and send it via workflow.
You can also compose any payload using formula.Last edited by yuri; 06-24-2023, 08:47 AM.
- Likes 2
-
How would you assign the whole pdf content?
just by using the field name? likeCode:$pdf = file;
Code:$pdf = fileContent;
I am not able to find a way to do itLast edited by Kharg; 07-01-2023, 10:46 AM.
Comment
-
Hi everyone,
I'm really interested in this thread because I need to implement an integration with an external system that requires, among other things, sending files from Espo via an HTTP REQUEST. The external API expects the file content in JSON format, encoded in Base64. The encoding function is available through a free plugin (https://devcrm.it/product/custom-formulas-for-espocrm/), but I can't figure out how to pass the file content (from the Attachment entity) into a variable within a Workflow.
I found a similar thread on the forum (https://forum.espocrm.com/forum/deve...ttachments-api), but it uses the external ext\file\contents() function, which isn’t available in my environment, and I can't find any information about it.
Thanks in advance for sharing your experience.
Comment
-
Hey jacao
You can create a custom formula function to get the file content and send it over through. Here is the code for a function formula i have used in the past.
1 - Create the php class under Espo\Custom\Core\Functions\ExtGroup\FileGroup\Cont entsType.php
PHP Code:<?php
namespace Espo\Custom\Core\Formula\Functions\ExtGroup\FileGroup;
use Espo\Entities\Attachment;
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Exceptions\Error;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Tools\Attachment\Service;
use Espo\Core\Di;
use Exception;
class ContentsType extends BaseFunction implements
Di\EntityManagerAware,
Di\InjectableFactoryAware
{
use Di\EntityManagerSetter;
use Di\InjectableFactorySetter;
public function process(ArgumentList $args)
{
if (count($args) < 3) {
$this->throwTooFewArguments(3);
}
$args = $this->evaluate($args);
$entityType = $args[0];
$id = $args[1];
$fileId = $args[2];
if (!$entityType || !is_string($entityType)) {
$this->throwBadArgumentType(1, 'string');
}
if (!$id || !is_string($id)) {
$this->throwBadArgumentType(2, 'string');
}
if ($fileId && !is_string($fileId)) {
$this->throwBadArgumentType(3, 'string');
}
$em = $this->entityManager;
try {
$entity = $em->getEntity($entityType, $id);
}
catch (Exception $e) {
$this->log("Message: " . $e->getMessage() . ".");
throw new Error();
}
if (!$entity) {
$this->log("Record {$entityType} {$id} does not exist.");
throw new Error();
}
try {
/** @var Attachment $attachment */
$attachment = $em->getEntity(Attachment::ENTITY_TYPE, $fileId);
}
catch (Exception $e) {
$this->log("Message: " . $e->getMessage() . ".");
throw new Error();
}
if (!$attachment) {
$this->log("Record Attachment {$id} does not exist.");
throw new Error();
}
$service = $this->injectableFactory->create(Service::class);
try {
$fileData = $service->getFileData($attachment->getId());
}
catch (Exception $e) {
$message = $e->getMessage();
$this->log("Error while getting. Content: {$message}." , 'error');
return false;
}
return (object) [
'content' => base64_encode($fileData->getStream()),
'name' => $attachment->getName(),
'type' => $attachment->getType(),
];
}
}
2 - Then create the mapping class for the formula function under Espo\Custom\Resources\metadata\app\formula.json
PHP Code:{
"functionList": [
"__APPEND__",
{
"name": "ext\\file\\contents",
"insertText": "ext\\file\\contents(ENTITY_TYPE, ENTITY_ID, FILEID)",
"returnType": "object"
}
],
"functionClassNameMap": {
"ext\\file\\contents": "Espo\\Custom\\Core\\Formula\\Functions\\ExtGroup\\FileGroup\\ContentsType"
}
}
I hope this helps
Rabii
Web Dev
- Likes 1
Comment
Comment