No announcement yet.

formula replace query?

  • Filter
  • Time
  • Show
Clear All
new posts

  • formula replace query?

    Is it possible to search a string in a field and replace using a formula?
    I can do this easily in MySQL. I have searched the Forum with 0 results/hits.

    what I am attempting to do it clean up upon entry for example field "website" is user copies and paste it includes "https://" or "http://"
    I find it pointless to enter this info and it takes up space based on the limit of the field.
    Basically I was to remove "https://" or "http://" from the string upon saving.

  • #2
    You can do it using a "beforeSave" hook instead of formula like this:

    file: custom/Espo/Custom/Hooks/{{yourEntity}}.php

    namespace Espo\Custom\Hooks\{{yourEntity}};
    use Espo\ORM\Entity;

    class {{yourEntity}}Hooks extends \Espo\Core\Hooks\Base


    public function beforeSave(Entity $entity, array $options=array())

    $haystack = $entity->get("website");
    $needle1 = "https://";
    $needle2 = "http://";
    // test for "https://"
    if (strpos($haystack, $needle1) !== false) {
    str_replace ( $needle1, "", $haystack);
    // test for "http://"
    if (strpos($haystack, $needle2) !== false) {
    str_replace ( $needle2, "", $haystack);
    // save changes to field


    Looks a lot more work than a formula command but I think it's a lot easier to understand and debug if necessary.
    Last edited by telecastg; 06-10-2019, 05:27 PM.


    • rodrigocoelho
      rodrigocoelho commented
      Editing a comment
      I'm asking them to create a Field with MASKS.
      I hope it would be the best solution...

    • khopper
      khopper commented
      Editing a comment
      Why can't they just create a replace() function just like "MySQL"? That is my question!

    • rodrigocoelho
      rodrigocoelho commented
      Editing a comment
      Just suggest it.