record\create and record\update

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brotherlouie
    Junior Member
    • May 2023
    • 17

    #1

    record\create and record\update

    I have an entity named "Bilet" in a base plus property. I have a flight date (ucusTarihi) field in this entity. I also have another entity named Ucustarihi in the event property. With the record\create command, I trigger some fields in the ticket entity with a formula to create a new record in the flight date entity. My formula is below and it really works.

    record\create(
    'Ucustarihi',
    'name', name,
    'dateStart', ucusTarihi,
    'dateEnd', ucusTarihi,
    'biletId', id,
    'assignedUserId', assignedUserId,

    );

    When I update the record in my main entity 'Bilet', it creates a copy in the 'Ucustarihi' entity. I tried to solve this with create\update but I couldn't. Is there a way to do this?

    What I want to do is basically this; after creating a record in the main entity, a record is automatically created in the other entity. But I want to prevent it from creating a copy again unless I make a change in the main entity.

    If you can help me I would be very happy. Thank you in advance.
  • lazovic
    Super Moderator
    • Jan 2022
    • 972

    #2
    Hi brotherlouie,

    You can use an if condition using the entity\isNew() function. Example:
    Code:
    if (entity\isNew()) {
       record\create(
       'Ucustarihi',
       'name', name,
       'dateStart', ucusTarihi,
       'dateEnd', ucusTarihi,
       'biletId', id,
       'assignedUserId', assignedUserId
      );
    }

    Comment

    • brotherlouie
      Junior Member
      • May 2023
      • 17

      #3
      lazovic Thank you very much. It really worked. Apart from that, is it possible to update the flight date of the relevant record when I update the flight date?

      Comment

      • lazovic
        Super Moderator
        • Jan 2022
        • 972

        #4
        brotherlouie,

        You can try using something like this formula script:
        Code:
        if (entity\isAttributeChanged('flightDate') {
            record\update('Record', recordId, 'flightDate', flightDate)
        }

        Comment

        • brotherlouie
          Junior Member
          • May 2023
          • 17

          #5
          If I know the id of the entity I created, this formula works. But since I automatically create a record for another entity from the main entity, the id is created in my other entity and I need to query it from the main entity to update it. I think I won't be able to do this. So I will create a single record. And if an update is needed, I will do it manually from the other entity. Thank you for your support.

          Comment

          • lazovic
            Super Moderator
            • Jan 2022
            • 972

            #6
            brotherlouie,

            You can access the ID of any record in the system, regardless of where the formula script is called from.

            If you tell me specifically which record you want to access from the main entity and which entity can connect them, I will try to help you. It will also be useful to look at their relationships (One-to-Many, Many-to-Many, etc.).

            Comment

            • brotherlouie
              Junior Member
              • May 2023
              • 17

              #7
              The entity I want to reach is 'Ucustarihi'.
              My main entity is 'ticket'.
              There is a many to one relationship between my main entity 'ticket' and 'Ucustarihi'.
              When I make a record in the main entity with the formula you gave, a record is created in my 'Ucustarihi' entity. What I want to do is update the relevant record in the 'Ucustarihi' entity when I make a change in the record in the main entity. The only record fields to be updated here are;

              'dateStart', uçusTarihi,

              I am grateful for your help with my problem. Thank you for your time.

              Comment

              • lazovic
                Super Moderator
                • Jan 2022
                • 972

                #8
                brotherlouie,

                Can you please try the following formula script in the Bilet (Ticket) entity?
                Code:
                if (entity\isAttributeChanged('ucusTarihi') {
                record\update('Ucustarihi', ucustarihiId, 'ucusTarihi', ucusTarihi)
                }
                It works as follows: if the value of field ucusTarihi in the Bilet (Ticket) entity changes, then the value of field ucusTarihi in the related entity Ucustarihi will change to the corresponding one.

                There may be a nuance with the relationships: please tell me if you can have several Ucustarihi records for entity Bilet? If so, then this formula script needs to be changed a little.
                Last edited by lazovic; Today, 11:18 AM.

                Comment

                • yuri
                  Member
                  • Mar 2014
                  • 8909

                  #9
                  The ID of the created entity is returned by the record\create function.
                  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

                  • brotherlouie
                    Junior Member
                    • May 2023
                    • 17

                    #10
                    I added it visually to better express what I want to do. In short, I have a single date field in my main entity (ucusTarihi). The name of the entity I created a new record for is also Ucustarihi. When I update the ucusTarihi in my ticket main entity, I want my two date fields in the Ucustarihi entity to be updated with the ucusTarihi in my main entity.(dateStart and dateEnd)

                    I hope I was able to explain it. Thank you for your support.
                    Attached Files

                    Comment

                    Working...