Formula not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ctheuring
    Senior Member
    • Oct 2022
    • 138

    Formula not working

    Hello,
    I have the following formulas in my entity "Kontakte":

    name = string\concatenate(kontaktdatum, ' ', kontaktart, ': ', firmen.name1); // -> running fine
    mitarbeiterId= env\userAttribute('name'); // -> running fine
    herstellerId = Maschinen.herstellerId; // -> didn't run -> should be update the field herstellerId with the content of the field herstellerId in the linked entity Maschinen
    baureihenId = Maschinen.baureihenId;// -> didn't run -> same with other linked fields as obove
    typenId = Maschinen.typenId;// -> didn't run-> same with other linked fields as obove
    This is my main problem.
    After solving this, I've some 2nd problems about this situation ... discuss later ...

    Christoph
  • shalmaxb
    Senior Member
    • Mar 2015
    • 1605

    #2
    1. click on the circle symbol in the formula editor to see if the syntax is correct (although this is not 100%)
    2. check every field name, Id is sometimes ID, capital letters, sometimes singular plural for entities
    3. check log file, if it gives a hint

    uncomment formula lines, starting with the last one, after that one after another, to get nearer to the problem

    Comment

    • yuri
      Member
      • Mar 2014
      • 8452

      #3
      Maschinen.herstellerId is incorrect. If it's a field of a related record you want to fetch, it should start with a lower case letter.
      If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

      Comment

      • ctheuring
        Senior Member
        • Oct 2022
        • 138

        #4
        Originally posted by yuri
        Maschinen.herstellerId is incorrect. If it's a field of a related record you want to fetch, it should start with a lower case letter.
        this tipp do the work :-)
        But ...
        after saving the linked field herstellerId it shows the (correct) ID but not the (field) name of the hersteller. After reload (or scroll forward than backwords) the filed show the true name of the hersteller.
        Is there a command I can append to reload the saved row after saving?

        Comment

        • rabii
          Active Community Member
          • Jun 2016
          • 1250

          #5
          you can try to add the field name as well meaning, add this to your code:

          PHP Code:
          herstellerName = maschinen.herstellerName 
          
          This should load the field with the name as well as the id.
          Rabii
          Web Dev

          Comment

          • ctheuring
            Senior Member
            • Oct 2022
            • 138

            #6
            Originally posted by rabii
            you can try to add the field name as well meaning, add this to your code:
            PHP Code:
            herstellerName = maschinen.herstellerName 
            
            This should load the field with the name as well as the id.
            exactly ! Thanks

            Now next questions in my context of formula:

            mitarbeiterId = env\userAttribute('id'),
            mitarbeiter = mitarbeiter.name​
            ... is working fine

            ​if (mitarbeiter == '') {
            mitarbeiterId = env\userAttribute('id'),
            mitarbeiter = mitarbeiter.name
            };
            ​is not working - also with mitarbeiterId== '' or mitarbeiterName == ''
            How can I test of mitarbeiter/Id/Name is empty?
            Database showed field as <null>

            Christoph

            Comment

            • rabii
              Active Community Member
              • Jun 2016
              • 1250

              #7
              you can try this instead, i assume mitarbeiter is a link (if yes then you need to check if id is null)

              PHP Code:
              ​if (mitarbeiterId == null) {
              mitarbeiterId = env\userAttribute('id'),
              mitarbeiter = mitarbeiter.name
              };
              Or This (you have to use double quote)

              PHP Code:
              ​if (!mitarbeiterId) {
              mitarbeiterId = env\userAttribute('id'),
              mitarbeiter = mitarbeiter.name
              };
              Last edited by rabii; 08-18-2023, 01:11 PM.
              Rabii
              Web Dev

              Comment

              • ctheuring
                Senior Member
                • Oct 2022
                • 138

                #8
                Originally posted by rabii
                you can try this instead, i assume mitarbeiter is a link (if yes then you need to check if id is null)

                PHP Code:
                ​if (mitarbeiterId == null) {
                mitarbeiterId = env\userAttribute('id'),
                mitarbeiter = mitarbeiter.name
                };
                Or This (you have to use double quote)

                PHP Code:
                ​if (!mitarbeiterId) {
                mitarbeiterId = env\userAttribute('id'),
                mitarbeiter = mitarbeiter.name
                };
                Thank you rabii ...
                ... good idea ... but dosn't run :-(
                May be a cache is the problem ...
                For me it's enough for today - I continue tomorrow

                Christoph

                Comment

                • rabii
                  Active Community Member
                  • Jun 2016
                  • 1250

                  #9
                  Please note that there is a problem with the function it should be like below:

                  PHP Code:
                  ​if (mitarbeiterId == null) {
                  mitarbeiterId = env\userAttribute('id');
                  // mitarbeiterName = mitarbeiter.name; // this will not work as the save of the above is not done so it is better to use env to get user name
                  mitarbeiterName = env\userAttribute('name');
                  };​  ​ 
                  
                  Try this instead, although make sure that the attributes are correct
                  Rabii
                  Web Dev

                  Comment

                  • ctheuring
                    Senior Member
                    • Oct 2022
                    • 138

                    #10
                    I thought I found the goal

                    if (mitarbeiterId == null) {
                    mitarbeiterId = env\userAttribute('id')
                    };
                    if ementity\isAttributeChanged('maschinenId') {
                    herstellerId = maschinen.herstellerId;
                    baureihenId = maschinen.baureihenId;
                    typenId = maschinen.typenId
                    };
                    // if there are more than one allocation in one condition, than EVERY allocations needs a ";" at the end, exept of the last - it's ";" is at the end of the if-condition.
                    mitarbeiterName = mitarbeiter.name;
                    herstellerName = maschinen.herstellerName;
                    baureihenName = baureihen.name;
                    typenName = typen.name;​
                    // after saving the change of links from id to field-name in output must be OUTSIDE the if-loop (so it is executed every save)
                    Christoph
                    P.S.
                    what ist the different in use inside formulas of the control structures with if (CONDITION) {CODE}​ and the general function ifThen(CONDITION, CONSEQUENT) ?
                    Last edited by ctheuring; 08-20-2023, 07:46 PM.

                    Comment


                    • rabii
                      rabii commented
                      Editing a comment
                      i don't understand your question, only different between if ( CONDITION ) {CODE} (works only in version 7.4 and higher - ifThen(CONDITION, CONSEQUENT) works in all versions including old versions. no difference it is just improvement on how it could be write but both do the same.
                  • ctheuring
                    Senior Member
                    • Oct 2022
                    • 138

                    #11
                    That's what I want to know (from version 7.4 on it's the same) - thanks rabii
                    Christoph

                    Comment

                    • macistda
                      Member
                      • Jul 2022
                      • 76

                      #12
                      If some other data is needed (not the one directly available via maschinen.xxx) it is useful to be exercised with the formula functions, e.g. https://docs.espocrm.com/administrat...ecordattribute. Of course to be a linked table is necessary then.

                      Comment

                      • ctheuring
                        Senior Member
                        • Oct 2022
                        • 138

                        #13
                        Originally posted by macistda
                        If some other data is needed (not the one directly available via maschinen.xxx) it is useful to be exercised with the formula functions, e.g. https://docs.espocrm.com/administrat...ecordattribute. Of course to be a linked table is necessary then.
                        thanks for the usefull tip macista
                        Christoph

                        Comment

                        Working...