Announcement

Collapse
No announcement yet.

How can I use Workflow to send an HTTP request?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How can I use Workflow to send an HTTP request?

    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.

  • #2
    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.

    Comment


    • abidoss
      abidoss commented
      Editing a comment
      thank you for your response. However, I would prefer if there is already an example. I already have the Advanced pack. My idea is to send the PDF file to the API, receive a result, and then store it in a text field.

  • #3
    How would you assign the whole pdf content?

    just by using the field name? like
    Code:
     $pdf = file;
    or is it something like
    Code:
    $pdf = fileContent;
    ?

    I am not able to find a way to do it
    Last edited by Kharg; 07-01-2023, 10:46 AM.

    Comment


    • yuri
      yuri commented
      Editing a comment
      There's no way to read file contents from the attachment entity at the moment. One would need to create a simple formula function for this. I planned to add such a function.

    • Kharg
      Kharg commented
      Editing a comment
      Would be really useful for situations like this

  • #4
    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


    • Kharg
      Kharg commented
      Editing a comment
      Doesn’t seem that the formula exists.

      I created an entrypoint to do this, but I can convert it into a formula and release an extension maybe.

  • #5
    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"
        
    }
    }
    ​  ​ 
    and this how you can use it see attached screen shot.

    I hope this helps

    Click image for larger version

Name:	get-file-formula-function.png
Views:	100
Size:	283.6 KB
ID:	110816
    Rabii
    Web Dev

    Comment


    • Kharg
      Kharg commented
      Editing a comment
      Since you already did all the job, do you mind if I package this into an extension? I would also include my entrypoint probably.

    • rabii
      rabii commented
      Editing a comment
      Of course feel free to do it
Working...
X