Announcement

Collapse
No announcement yet.

Having troubles relating 2 records when their relationship is n-n using formula

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Having troubles relating 2 records when their relationship is n-n using formula

    Hi everyone,

    I'm struggling to relate using a flowchart 2 records that are related in a many-to-many relationship. This works perfect when the relationship is unique on one side, let me show it to you.

    In this case, we have an entity called Student, that relates to many Courses, at the same time those courses relate to many students so that's a many-to-many relationship.
    We do have too an entity called Participation that has all the information of the student relative to the course (grades, attendance, etc), and that's related to Student as 1 Student / N Participations, and N Participations / 1 Course.

    When I try to relate the records using a flowchart and the formula from the advanced pack, I get that the 1-N relations are done properly using the following:
    record\relate('Student', id, 'participations', $pcID);
    record\relate('Course', $cId, 'participations', $pcID);


    But it's not working, and not logging an error when using the same formula for N-N relations:
    record\relate('Student', id, 'courses', $cId);

    Am I doing something wrong? is there another way of doing this in this case? I can't use the system relation as I am checking a list I get from elsewhere and I need to do it by scripting and an interator...

    Thanks for the help!
    ​​

  • #2
    use entity\addLinkMultipleId(LINK, ID) instead like below:

    PHP Code:
    entity\addLinkMultipleId('courses​'$cId​
    I am assuming the workflow is for student as a target entity.
    Rabii
    Web Dev

    Comment


    • abisbe
      abisbe commented
      Editing a comment
      Great thank you!

  • #3
    Remember that record\relate works only for 1 - N relationship.

    You need to use entity\addLinkMultipleId that works for N-N relationship.

    Rabii
    Web Dev

    Comment


    • #4
      Sorry rabii I'm a bit confused about how to use this, you see on the previous formula, you'd state the entity you're working with, and it's id, then the field that contains the relation, and then the data you want to introduce to that relation.
      Here it just seems that you relate 2 fields, and it looks like it's missing something, is this working only for the record that made the flowchart start? meaning that I can't add n-n relationships between 2 different records when none are the target?

      Thank you.

      Comment


      • #5
        i think you could still that as well. look i am happy to help, but i need some details can you share your code now and how your flowcharts is designed?
        Rabii
        Web Dev

        Comment


        • #6
          > Here it just seems that you relate 2 fields, and it looks like it's missing something, is this working only for the record that made the flowchart start? meaning that I can't add n-n relationships between 2 different records when none are the target?

          I am relating two entities that have a many-to-many relationship. Yes entity\ (anything that start with entity\ ) is meant to be applied to the current entity (target entity) in workflow / formula script / bpmn (works the same on all of them). but remember that there are otherwise to update data even the data of the entities that are not linked in any way to the target entity. depends on what you are trying to achieve but all is doable.
          Rabii
          Web Dev

          Comment


          • #7
            It's a bit complex, we have an entity called Application that contains the application of a student, then this has selections of courses, each selection controls the application of this student to a concrete course. Then upon confirmation, the student is created, and this student has participations, an entity that controls grades and specific data from the student regarding a course.

            The following code is a bpmn that we start manually from a Participant to check if there is any discrepancy between the student courses and the courses present at the selections related to the Application.

            //Check all App Selections present in Student
            $i = 0;
            $AppSelectionsID = record\attribute('Application ', applicationId, 'selectionsIds');
            while($i < array\length($AppSelectionsID),

            $selId = array\at($AppSelectionsID, $i);​

            $cId = record\attribute('Selection', $selId, 'courseId');

            //Check Relation with Course
            ifThen(!array\includes(coursesIds,$cId),
            //Relate C
            record\relate('Student', id, 'courses', $cId); // This is the part that is not working
            );
            );


            This is a simple part of the whole script, but it would be enough to explain a bit of my difficulties. Thanks for the help! Let me know if you need further clarification.​

            Comment


            • #8
              One question the flowchart target entity is Application ? if yes then this formula code is executed in a script formula or (Update Related Entity) Action ?
              Rabii
              Web Dev

              Comment


              • abisbe
                abisbe commented
                Editing a comment
                The flowchart target is Student, and this will be triggered by the change of an enum field to "update student" so that we can do this to groups of students using the mass update feature.

            • #9
              I have assumed that you have enabled Link Mulitple Field on the relationship so that you could access the courses on the students, if not done then this might be an issue and you will need to enable that so that you can access the courses field, based on information you have provided this code below should work for you. Please note that you had a missed bracket on the while lop condition:

              PHP Code:
              //Check all App Selections present in Student
              $i 0;

              $appSelectionIds record\attribute('Application'applicationId'selectionsIds');

              while(
              $i array\length($appSelectionIds)),
                  
                  
              $courseId record\attribute('Selection'array\at($appSelectionIds$i), 'courseId');
                  
                  
              //Check Relation with Course
                  
              ifThen(!array\includes(coursesIds$courseId),
                      
                      
              //Relate Course
                      
              entity\setAttribute('coursesIds'array\push(coursesIds$courseId)) // In order for this to work Link Multiple Field should be enabled on the relationship
                  
              );
                  
                  
              $i $i 1;
              );
              ​ 
              This should work fine.

              Let me know if you face any issue, oh sorry have changed names of variables, i am just used to the convention as it reads better.
              Rabii
              Web Dev

              Comment


              • #10
                It worked! Thanks for your patience!!

                Comment


                • #11
                  you are welcome
                  Rabii
                  Web Dev

                  Comment

                  Working...
                  X