record\create and record\update

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

    #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
    • 979

    #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
      • 21

      #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
        • 979

        #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
          • 21

          #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
            • 979

            #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
              • 21

              #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
                • 979

                #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; Yesterday, 11:18 AM.

                Comment

                • yuri
                  Member
                  • Mar 2014
                  • 8914

                  #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
                    • 21

                    #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

                    • lazovic
                      Super Moderator
                      • Jan 2022
                      • 979

                      #11
                      brotherlouie,

                      Can you please try to use the following formula script in the Bilet (Ticket) entity?
                      Code:
                      if (entity\isAttributeChanged('ucusTarihi') {
                        record\update('Ucustarihi', ucustarihiId, 'dateStart', ucusTarihi, 'dateEnd', ucusTarihi)
                      }

                      Comment

                      • brotherlouie
                        Junior Member
                        • May 2023
                        • 21

                        #12
                        I tested this formula but it didn't work. I tried a few times. I discovered an interesting situation. Maybe this will help us to know how to proceed. When I create a record in the main entity, the ID information of the record created in the other entity is 681da2e5ae2e5b040.

                        if (entity\isAttributeChanged('ucusTarihi') {
                        record\update('Ucustarihi', UcustarihiId, 'dateStart', ucusTarihi, 'dateEnd', ucusTarihi)
                        }

                        if (entity\isAttributeChanged('ucusTarihi') {
                        record\update('Ucustarihi', 681da2e5ae2e5b040, 'dateStart', ucusTarihi, 'dateEnd', ucusTarihi)
                        }

                        When I add this ID information 681da2e5ae2e5b040 to the ID section of this formula and try to run it, I cannot get a result.​​

                        However, when I use the formula like this,
                        record\update('Ucustarihi', '681da2e5ae2e5b040', 'dateEnd', ucusTarihi, 'dateStart', ucusTarihi);
                        I can get results.

                        of course the result only works for this 681da2e5ae2e5b040 Id.

                        I'm taking your time, but can you give me a suggestion based on this? Thank you for your help.​

                        Comment

                        • lazovic
                          Super Moderator
                          • Jan 2022
                          • 979

                          #13
                          brotherlouie,

                          Firstly, the attribute name should be written with a lowercase letter, that is, it should be ucustarihiId, not UcustarihiId.

                          Secondly, when we specify the record ID in the formula script, it should be in brackets (as in your last formula script version).

                          Please tell me, do I understand correctly that the Bilet (Ticket) entity and the Ucustarihi entity are connected by One-to-Many relationships? That is, one ticket can have several ucustarihi records? This is very important, as it affects the correctness of the formula script.

                          Comment

                          • brotherlouie
                            Junior Member
                            • May 2023
                            • 21

                            #14
                            lazovic
                            There is a many-to-one relationship between my ticket existence and my Ucustarihi existence.

                            Ticket -> many-to-one -> Ucustarihi

                            Comment

                            • lazovic
                              Super Moderator
                              • Jan 2022
                              • 979

                              #15
                              brotherlouie,

                              In this case, this formula script should working:
                              Code:
                              if (entity\isAttributeChanged('ucusTarihi') {
                                record\update('Ucustarihi', ucustarihiId, 'dateStart', ucusTarihi, 'dateEnd', ucusTarihi)
                              }

                              Comment

                              Working...