Announcement

Collapse
No announcement yet.

default value for call name using entity fields

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

  • default value for call name using entity fields

    I see there is the ability to use javascript in metadata for calculating default text of a field. How could I set a default name for Call like "{{parent.name}} - {{dateStart}}"?
    Is it mandatory to extend the field in code? Is this the best way or can Dynamic Logic do it ? https://docs.espocrm.com/development/dynamic-handler/

  • #2
    Last time I try to javascript a default field and it break the system (found a bug, it was patch officially and this no longer occurs). Haven't dare to touch this function again.

    The video tutorial show that you can javascript the "today date" into it, not sure how much of helpful it is for what you trying to do.

    Comment


    • #3
      Try this:

      Code:
      this.model.set('name', this.model.get('parentName') + ' - ' + this.model.get('dateStart'))
      CEO & Founder of Eblasoft.
      Professional EspoCRM development & extensions.

      Comment


      • #4
        I corrected that to javascript: this.set('name', this.get('parentName') + ' - ' + this.get('dateStart')); It seems that 'this' is the model in that context.

        I also tried this: javascript:return (function(model){let r=(model.attributes.parentType==='Lead'?(model.att ributes.parentName+' - '+model.attributes.dateStart):''); return r;})(this);

        In both cases the dateStart value would be undefined. I think it does not wait for the model to be ready.

        What is the difference between using attributes.dateStart and using get('dateStart') ?

        Comment


        • #5
          I suggest to use dynamic handler to accomplish what you want, it works like a "front end hook" and you can use regular javascript to define your functions and references in a clear way instead of having to "guess" how the metadata "statement" is going to be interpreted.

          What is the difference between using attributes.dateStart and using get('dateStart') ?
          none for practical uses as far as I understand, but I will be happy to be corrected if not
          Last edited by telecastg; 06-13-2020, 06:53 AM.

          Comment


          • tothewine
            tothewine commented
            Editing a comment
            Yeah... If I also stick an event listener in there it becomes hilariously long

          • telecastg
            telecastg commented
            Editing a comment
            In my opinion, using metadata to implement business logic is a brilliant method used by Espo to allow users to do some "pseudo-programming" from the Administration panel (like dynamic logic and formula), but in order to be used by developers it requires them to master the custom "language" or syntax that translates these statements into actual code, and that knowledge is not documented and hard to acquire.

            For that I believe that is much better to implement the logic directly in your scripts and skip the "middleware".

          • tothewine
            tothewine commented
            Editing a comment
            you are right indeed: that's a sore point for documentation. often i think in terms of how many files needs to be edited to add one line of code somewhere and it is always too high... for real the espocrm needs a big document describing the entire architecture and all of the logic behind it.

        • #6
          Is there a way to set the default name field to its parents name? I would love to be able to set this automatically. Please let me know. And thank you for your consideration.

          Comment


          • #7
            Hi,

            Yes you can do that with formula which is a GUI facility that allows you to create "beforeSave" hooks without PHP coding.

            Go to Admin > Entity Manager > {choose your entity} > Click the down chevron icon on the right to display the drop down menu, and select "Formula"

            The next step depends on how is your entity related to the "parent":

            If its linked as a child-to-parent relationship then enter
            Code:
             name=parentName
            and click "Save"

            If its linked as a many-to-one relationship then enter
            Code:
             name={'one' side entity name}.{field name}
            and click "Save"

            As example of the latter we have a "Tenancy" entity which is linked to a "Property" entity in a one-to-many relationship, and I want to fill the field "name" in "Tenancy" with the value of the field "propertyAddressStreet" in "Property":
            Code:
            name=property.propertyAddressStreet
            To learn more about formula check this link: https://github.com/espocrm/documenta...ion/formula.md
            Last edited by telecastg; 07-13-2020, 12:45 AM.

            Comment


            • Redwood
              Redwood commented
              Editing a comment
              Thank you so much, that worked perfectly. I had to change the default name that I had in as a placeholder. I really appreciate this!!!

            • telecastg
              telecastg commented
              Editing a comment
              You're welcome :-)

          • #8
            Anyone has ideas on how to detect entity creation mode in dynamic handler?

            Update: I will answer myself. The most practical way I found is to check if the model id is empty.
            Last edited by tothewine; 09-21-2020, 11:32 PM.

            Comment

            Working...
            X