No announcement yet.

Read current URL to set in a field

  • Filter
  • Time
  • Show
Clear All
new posts

  • Read current URL to set in a field

    Hello, I use the QR-Code field to print it on labels. The QR-Code contains the URL of the current record (means it is a QR-Code of its own record). I use it to print it on labels, by which you may access the record, when scanning the Code.
    The URL I have to copy from the browser`s address field and out manually into the QR-Code field.

    Is there a way to automatically read the current URL and fill in the respected field?

  • #2
    I think you could create hook which will fill this field after you create record. In hook you have to get config of EspoCRM and copy domain. It's not didifficult.

    Here you have adjusted hook from our extension. You need to adjust this hook, but after change of namespace, name of hook etc it should work.


    • #3
      Hi, I tried to create a hook, following the documentation and the script of emillod helped to understand hooks. It seems not very difficult, though I did not succeed with my code. This is what I wrote:

      namespace Espo\Custom\Hooks\MyNameSpace;
      use Espo\ORM\Entity;
      class qrCodeUrl extends \Espo\Core\Hooks\Base
      public function beforeSave(Entity $entity, array $options = [])
      if ($entity->isNew() && !$entity->get('qrCodeUrl')) {
      $entity->set('qrCodeUrl', 'https://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
      It does not work, nothing happens.
      What is my intention:

      I want to output the current site URL of a record in a custom entity. The site URL should fill in the field qrCodeUrl.
      The entity is equal to myNameSpace.
      The field should be filled in either it is a new record or it is not present in a record yet.
      I set the entity Attribute of qrCodeUrl to concatenated URL from prepend protocol (https;//), domain and rest of the address after the domain.
      The script I saved at custom/Espo/Custom/Hooks/MyNameSpace/qrCodeUrl.php

      I rebuilt, cleared cache, but it does not do anything.

      Is this approach correct or do I miss something?


      • #4
        shalmaxb have you tried afterSave instead beforeSave?
        Anyway i think you shouldn't use $_SERVER. My way is pretty simple.


        • #5
          emillod , you mean this line of your code:

          $domain = $config->get('siteUrl');
          In this part I did not understand $config. I think I would not need that. Right? But how would I set the ->get('siteUrL')?

          I have never done this before, so it takes time for me to understand.I will try afterSave.

          Thank you for your help.


          • #6
            shalmaxb you need everything which i put there, because first you have to construct config to fetch all parameters from data/config.php file to $config, and if you'll check config.php, then you'll see that you have there siteUrl parameter where you have domain.
            And thanks to $config->get('siteUrl'); you can take address of you EspoCRM to variable $domain


            • #7
              I tried everything, but did not get it to work. So I made another effort with formula in the following way:

              To create a complete link of the record I need:
              1. The base address of the record (which is equal for any record), without the record ID
              2. The record ID

              I created a varchar field recordID, which I populate by formula

              Then I created another varchar field to put together the whole link this way:

              The output is the complete address like in the browser address bar.

              I see, that this is not the most elegant solution, but for whom does not succed with coding (even trying hours to solve), this might be an acceptable approach.


              • #8
                It's great if it's working It's important that's working


                • shalmaxb
                  shalmaxb commented
                  Editing a comment
                  nontheless, I learned to understand a lot mor because of your help. I only did not succeed in finding out, why it did not work. Maybe later I will understand enough to solve even this. We will see. Thank you again for your help.

              • #9
                Meanwhile I succeded in solving the mentioned question in a more elegant way by creating a custom function to read the current hostname and concatenate with URL.
                You can find the solution here: