Knowing individual ids in a one to many relationships

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • esendino
    Senior Member
    • May 2019
    • 158

    Knowing individual ids in a one to many relationships

    Hi.

    When a One to Many relationship is defined a Link-Multiple field is included in the ONE Entity with the name of the MANY Entity ended in s. In this example should be called 'mANYs'.

    That field has two attributes: fieldIds, fieldNames. In this case mANYsIds and mANYsNames

    The question is how I can know the individual Ids included in mANYsIds.

    I have tried:
    mANYsIds($counter)
    mANYsIds[$counter]
    array\at(mANYsIds, $counter).

    But all of them unsuccessful.

    How can I get the individual ids?

    Thanks in advance for your help
  • yuri
    Member
    • Mar 2014
    • 8501

    #2
    Hi,

    You can use findRelatedOne to fetch the first related record.https://github.com/espocrm/documenta...findrelatedone
    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

    • esendino
      Senior Member
      • May 2019
      • 158

      #3
      Thanks @yurikuzn.

      I thought about that, but once got the firt one, How I can get the rest of them?

      I,m looking for retrieving a data from all the child registers. Thats why I look for the ids of all the children

      Comment

      • yuri
        Member
        • Mar 2014
        • 8501

        #4
        You want to iterate through an array. Espo-formula doesn't support iteration. It was intended for more simple operations. You can define your custom function for more complex logic.

        You can also try something like this:
        Code:
        $ids = fieldNameIds;
        
        $item0 = null;
        $item1 = null;
        $item2 = null;
        
        ifThen(
           array\length($ids) > 0,
           $item0 = array\at(0)
        );
        ifThen(
           array\length($ids) > 1,
           $item1 = array\at(1)
        );
        ifThen(
           array\length($ids) > 2,
           $item2 = array\at(2)
        );
        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

        • esendino
          Senior Member
          • May 2019
          • 158

          #5
          Thanks @yurikuzn.

          What you proposed worked perfectly. Thanks a lot.

          I solved the iteration issue with gateways in the BPM flow.

          Best regards

          Comment

          • esendino
            Senior Member
            • May 2019
            • 158

            #6
            Sorry @yurikuzn I assume that what tou prposed works equally for Ids and for Names.

            But it is not working for me. I suppose I made a mistake somewhere but can´t find where.

            As you see if I count the number of children thru Ids I get 7 (which is correct) but if I do it thru Names I get 0.

            Attached Files

            Comment

            • esendino
              Senior Member
              • May 2019
              • 158

              #7
              I just reproduced that in demo and ontained the same result

              Counted the number of children thru Ids I get 7 (which is correct) but if I do it thru Names I get 0.

              Comment

              • esendino
                Senior Member
                • May 2019
                • 158

                #8
                Hi @yurikuzn can you confirm if fieldNameIds should work similarly as fieldNameNames?

                I expected they should, but as mentioned in post of 04-05-2020, 06:46 PM they didn't for me. May you confirm?

                Comment

                • yuri
                  Member
                  • Mar 2014
                  • 8501

                  #9
                  fieldNames is not an array. It's an object. key=>value pairs.
                  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

                  • esendino
                    Senior Member
                    • May 2019
                    • 158

                    #10
                    Thank you @yurikuzn

                    Comment

                    Working...