Announcement

Collapse
No announcement yet.

Create Multiple Related Records From One Relationship Field

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

  • Create Multiple Related Records From One Relationship Field

    I have a point tracker system for training. For example I will select lets say 10 employees in one training record. I then (when saved) want to run either via a formula or by BPM a flow to create one "point tracker" record for each employee selected.

    My goal is to be able to run a report totaling all points and group by employee name.

    So my question is how can I create multiple records from one relationship field?

  • #2
    Hey dodom2

    It is easy to do it, you can use a bpm or you can use just before save script formula like below, please make sure you build a proper condition to make sure that the system creates those records only when certain condition happened:

    PHP Code:
    if (entity\isAttributeChanged('employeesIds') && employeesIds) {
        
    $e 0;
        
        while (
    $e array\length(employeesIds)) {
            
            if (
    record\exists('TrackingPoint''employeeId='array\at(employeesIds$e))) {
                continue;
            }
            
            
    record\create('TrackingPoint''name''some-name''employeeId'array\at(employeesIds$e));
            
            
    $e $e 1;
        }
    }
    ​ 
    Rabii
    Web Dev

    Comment


    • #3
      Thank you rabii however I dont really understand your formula. Maybe you can help with my fields?

      In my Training Module I have this field called "membersOnSceneIds" which is the relationship field I use to select each employee that was at the training. I also have "pointsIssued" in this same module that I enter in how many points we are issuing to the employee for attending the training. Having this said once I save a record for the first time and "membersOnSceneIds" field is not empty && "pointsIssued" is greater than 0 then I would like to create a point tracker record (which I understand is the record\create in this case).

      After I save this record if I update the "membersOnSceneIds" or "pointsIssued" fields and save then it should update the related (already created) point tracker record which we will say is "pointTrackerRecord".

      Any help is appreciated

      Comment


      • #4
        Hey, i am happy to help, i need to know the exact name of the models (entities) as far as i understand you 3 entities (Training - Employee - PointTrackerRecord) is that correct ?

        What is the relationship between the PointTrackerRecord and the Employee entities ? what is the field on PointTrackerRecord that represent the employee?

        It would be easier if you could share some screenshots, pointsIssued is it part of training ?
        Rabii
        Web Dev

        Comment


        • #5
          For the sake of this forumla lets say

          PointTrackerModule (which is where all point trackers records are stored, and also what were trying to create. This is the module that I will pull reports from)
          EmployeeModule
          TrainingModule

          Thank You

          Comment


          • #6
            I am still not sure how it works between your entities, but you can try this code below:

            PHP Code:
            // This code will be executed when a new training is created. It will check the conditions below and execute the code
            if (entity\isNew() && membersOnSceneIds && pointsIssued 0) {
                    
                
            $e 0;
                
            // Here we will loop through the existing/selected members (employees).
                
            while ($e array\length(membersOnSceneIds)) {
                    
                    
            // we check if a tracking point already exists. If yes we go to next itertaion. I assume employee is the name field of the relationship between the trackingpoint
                    
            if (record\exists('PointTrackerRecord''employeeId='array\at(membersOnSceneIds$e))) {
                        continue;
                    }
                    
                    
            // otherwise if record does not exist we create the record.
                    
            record\create('TrackingPoint''name''some-name''employeeId'array\at(membersOnSceneIds$e));
                    
                    
            $e $e 1;
                }
            }

            // This code will be executed when an existing training record is updated. It will check the conditions below and execute the code
            if (!entity\isNew() &&
                (
                    
            entity\isAttributeChanged('membersOnSceneIds') && membersOnSceneIds ||
                    
            entity\isAttributeChanged('pointsIssued') && pointsIssued 0
                
            )
            ) {
                
                
            $prefetchedMembersIds entity\attributeFetched('membersOnSceneIds');
                
                
            $p 0;
                
            // Here we will loop through the existing/selected members (employees).
                
            while ($p array\length($prefetchedMembersIds)) {
                    
                    
            // we check if a tracking point already exists. If yes we update the record with new data.
                    
            if (
                        
            array\includes(membersOnSceneIdsarray\at($prefetchedMembersIds$p)) &&
                        
            record\exists('PointTrackerRecord''employeeId='array\at($prefetchedMembersIds$p))
                    ) {
                        
            record\update('TrackingPoint''pointsIssued'pointsIssued'employeeId'array\at($prefetchedMembersIds$p));    
                    }
                    
                    
            // This code will delete a PointTrackerRecord if a member has ben removed from the list. Plase note that record/delete() is available only on version 7.4.0.
                    
            if (
                        !
            array\includes(membersOnSceneIdsarray\at($prefetchedMembersIds$p)) &&
                        
            record\exists('PointTrackerRecord''employeeId='array\at($prefetchedMembersIds$p))
                    ) {
                        
            $pointTrackerRecordId record\findOne('PointTrackerRecord''createdAt''desc''employeeId=', ($prefetchedMembersIds$p));
                        
                        
            record\delete('PointTrackerRecord'$pointTrackerRecordId);
                    }
                    
                    
            $p $p 1;
                }
            }
            ​ 
            Rabii
            Web Dev

            Comment


            • #7
              Thank you for taking the time to put this together. Give me a few days to work on this and I will report back the final results

              Comment

              Working...
              X