Issue with entity\isNew() method failing to retrieve ID

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lj4353
    Senior Member
    • Nov 2023
    • 114

    Issue with entity\isNew() method failing to retrieve ID


    When I create a user, I want to create a piece of data, but the newly created user.id cannot be obtained.Click image for larger version

Name:	image.png
Views:	121
Size:	28.8 KB
ID:	100927
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    Make sure that the name of the entity is correct (EmployeeInfo) -> you can check the name under administration > Entity Manager > Employee, also you should use AssignedUserId so it should be as below:

    PHP Code:
    if(entity\isNew()) {
        record\create('EmployeeInfo', 'assignedUserId', id, 'joiningDate', datetime\today(), 'baseSalary', 150000);
    }
    Rabii
    Web Dev

    Comment

    • a.slyzhko
      Member
      • Oct 2023
      • 90

      #3
      Id might not have been initialized yet by the time of execution of this formula script. Do you use before save script or after save workflow?

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #4
        You are right, id will not be available because all espocrm formula is a before save script which means it is applied before the record is created and saved in db. I think in this case you might want to reach to a afterSave hook which will be perfect for this scenario https://docs.espocrm.com/development/hooks/
        Rabii
        Web Dev

        Comment

        Working...