Before Save Custom Script Repeated Every Time (After Save)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jacksii
    Junior Member
    • Mar 2025
    • 6

    Before Save Custom Script Repeated Every Time (After Save)

    Hey,
    I am creating a CODE for Before Save Custom Script, where COMPANY NAME LEGAL FROM IS EQUAL TO INC, than COMPANY NAME MUST BE ABC INC,

    But when i am update any fields of that company every time it is ADDED INC at the end of the company like ABC INC INC INC

    here is the code;

    name = ifThenElse(
    lEGALFORM == 'INC',
    string\concatenate(name, ' ', "INC");,
    string\concatenate(name);
    );

    how to stop the INC REPEATED AT Before Save Custom Script.

    Thank in Advance




  • rabii
    Active Community Member
    • Jun 2016
    • 1286

    #2
    Try this

    PHP Code:
    if(entity\isAttributeChanged('lEGALFORM') && !string\contains(name'INC')){
        
    name string\concatenate(name' '"INC");

    Last edited by rabii; 04-14-2025, 10:14 PM.
    Rabii
    Here to help :)

    Comment

    • Jacksii
      Junior Member
      • Mar 2025
      • 6

      #3
      Originally posted by rabii
      Try this

      PHP Code:
      if(entity\isAttributeChanged('lEGALFORM') && !string\contains(lEGALFORM'INC')){
      string\concatenate(name' '"INC");,
      string\concatenate(name);



      HEY,
      This Code is NOT working for me on before save custom script formula section.

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1286

        #4
        which version of espocrm you have ?

        Just to confirm to avoid having the code executed repeatedly you can add some condition which triggers the code. in the code i have shared above, the result will be executed when condition is met. the legalform field changed and name doesn't contain INC.

        It should work fine in custom formula script.
        Rabii
        Here to help :)

        Comment

        • ChrisSka83
          Senior Member
          • Apr 2023
          • 232

          #5
          Hy rabii ,

          in case I'm wrong, but isn't “name=” missing ?
          ->
          if(entity\isAttributeChanged('lEGALFORM') && !string\contains(lEGALFORM, 'INC')){
          name=string\concatenate(name, ' ', “INC”);,
          name=string\concatenate(name);
          }​

          Comment


          • rabii
            rabii commented
            Editing a comment
            Correct the string/contains should check the name instead. i have fixed the code above.

            Thanks

          • ChrisSka83
            ChrisSka83 commented
            Editing a comment
            I actually meant the conclusion

            name=string\concatenate(name, ' ', “INC”);,
            name=string\concatenate(name);

            So that the formula knows in which field the string\concatenate is executed.
            Or does this also work without “name=” ?
            I haven't tried it yet, but I always build my formulas this way.

          • rabii
            rabii commented
            Editing a comment
            you are right, it should be name = string function.
        Working...