Error "Can't use an entity w/o ID" in API Before Save Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alohatech
    Junior Member
    • May 2024
    • 17

    Error "Can't use an entity w/o ID" in API Before Save Script

    Hello,

    I am working on an API Before Save Script where I check the value of an attribute to determine whether the entity can be saved or not. The script works perfectly when the entity is updated, but I encounter the following error when trying to save a new entity:

    "Can't use an entity w/o ID."

    It seems like the script is trying to use the ID of the entity, which doesn't exist for new records. How can I modify the script to handle new entities (without IDs) and still enforce the logic based on the attribute value?

    Thank you in advance for your help!
  • yuri
    Member
    • Mar 2014
    • 8453

    #2
    HI,

    It can take time to try to figure out what your failing script is doing. A provided non-working snippet would make it easier.​
    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


    • alohatech
      alohatech commented
      Editing a comment
      Hello yuri , This my code:
      if (attributeA!="A"){

      if (programme.name=="P0001"){
      $message = "modification is disabled.";



      recordService\throwBadRequest($message);

      }
      }
  • alohatech
    Junior Member
    • May 2024
    • 17

    #3
    Hello This the used code :
    PHP Code:
    if (attributeA != "A") {
    
        if (programme.name == "P0001") {
            $message = "modification is disabled.";
    
    
    
            recordService\ throwBadRequest($message);
    
        }
    }

    Comment

    • yuri
      Member
      • Mar 2014
      • 8453

      #4
      Hi,

      Related attributes are not available in API before-save script when the record is new.

      Click image for larger version  Name:	image.png Views:	0 Size:	14.8 KB ID:	111683
      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

      Working...