API Before-Save script vs Formula Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    API Before-Save script vs Formula Script

    I have come across a situation where i noticed that in Formula script you can access nested relationships but in API Before-Save script it doesn't work, see code example below for Case entity:

    Formula script code

    PHP Code:
    if (!account.assignedUserId){
        description = "Account does not have assigned user.";
    }
    The code is executed and description is filled with the message because we could check if the assignedUserId on the linked account is empty/null.

    Api Before-save script code

    PHP Code:
    if (!account.assignedUserId){
        $message = "Account does not have assigned user.";
        
        recordService\throwConflict($message);
    }
    This code is not working and throw an error 500, below the log message (seems that we can't check nested relationships here)

    PHP Code:
    [2023-09-01 09:30:30] ERROR: (0) Can't use an entity w/o ID.; POST /Case; line: 73, file: \application\Espo\ORM\Repository\RDBRelation.php 
    
    Is this by design or is there an issue ? Not sure if this is a bug or not?
    Rabii
    Web Dev
  • yuri
    Member
    • Mar 2014
    • 8452

    #2
    Not a bug.

    Last edited by yuri; 09-01-2023, 12:29 PM.
    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


    • rabii
      rabii commented
      Editing a comment
      sorry about that Yuri, is that mean if the record is new it won't work ? we need this to be applied to new created record but looks we can't access the nested relationship as explained above.

    • yuri
      yuri commented
      Editing a comment
      You still can access with record\attribute function.

    • rabii
      rabii commented
      Editing a comment
      Thanks record\attribute sorted the issue, i have tried before using findOne but didn't work. thanks all is fine now
Working...