Announcement

Collapse
No announcement yet.

Retrieving information from a related record

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

  • Retrieving information from a related record

    I have a process that is triggered by an update in a register of entity A.

    This entity has a defined 1 to N relationship with entity B. Although, in practice, it is a one-to-one relationship.

    When the process is launched I need to retrieve information that is in entity B. To do this I have executed a task "update related record" as shown in image 1.

    I have used this technique several times and it has always worked for me, but this time it did not. The process variables are all empty

    I am not able to find what I have not done well.


    Trying to see what the error is, I have generated simplified versions of the process.

    In the image 2.1 the process is launched and I do some update in the entity B. What I see is that it updates the entity B but the information of the process variable is not carried to the next task.

    The image 2.2 is the same with a small modification in the code of the update task V2. It gives me the same result: it updates the entity B but the process variable does not carry the value to the next task.

    In the image 2.3 I execute the task as a Script task. In this case I get the process variable to carry the value to the next task. But, logically, this doesn't allow me to read the information from entity B.

    How can I solve this? What I need is to read the entity B and carry in the process variables the values I need (when there are many attributes to be retrieved and not just one).

    thank you very much
    Attached Files

  • #2
    Hi,

    As I understand, you need to retrieve values from the record that is linked through one-to-many.

    Update Related Record should not store process variables or target record. You need to utilize Script Task.


    In script task:

    Code:
    $idOfRelatedRecord = record\findRelated('TargetEntityType', entity\attribute('id'), 'linkName', 'createdAt', 'desc');
    
    ifThen($idOfRelatedRecord,
        $shippingAddressCity = record\attribute('EntityTypeOfRelatedRecord, $idOfRelatedRecord, 'shippingAddressCity');
        entity\setAttribute('shippingAddressCity', $shippingAddressCity);
    
        // the same 2 lines for other fields
    );
    Substitute TargetEntityType, linkName, EntityTypeOfRelatedRecord with actual values.



    See: https://docs.espocrm.com/administrat...findrelatedone
    Last edited by yuri; 04-30-2020, 01:02 PM.

    Comment


    • #3
      Thanks

      Comment


      • #4
        Hi Yuri,
        could you please explain the parameters used in record\findRelatedOne:

        From your example, I understand:
        TargetEntityType: contains the related records I'm seraching. This is the "many" side.
        entity\attribute('id'): I don't get, which ID I need to put here!
        linkName: I've got a link between many ("transferfaelle") and one ("lead"). Should LinkName be the name of the many side ("transferfaelle")?

        If I use ...[... KEY1, VALUE1, KEY2, VALUE2 ...], as in the docs, is this like SQL "where KEY1 = Value1" etc.?

        My case: I need to import a lead (one side), that contains a field "transferCaseId". My many side "Transferfall" contains the same field, one record will have the same value as the field in lead. I just want to add the corresponding Transferfall to the lead I just imported. Unfortunately, my script does not work, obviously record\findRelatedOne does not give any result. I cannot figure out, what goes wrong:

        Code:
        $myEID = entity\attribute('id');
        $myTCI = entity\attribute('transferCaseId');
        
        $myId = record\findRelatedOne(
        'Transferfall',
        $myEID,
        'transferfaelle',
        'createdAt',
        'desc',
        'transferCaseId=',
        $myTCI
        );
        
        entity\addLinkMultipleId('transferfaelle', $myId);
        Any hints very welcome!
        Kind regards

        Comment


        • #5
          Solved the problem by using record\findOne:
          $myEID = entity\attribute('id');
          $myTCI = entity\attribute('transferCaseId');

          $myId = record\findOne(
          'Transferfall',
          'number',
          'desc',
          'transferCaseId=',
          $myTCI);


          entity\addLinkMultipleId('transferfaelle', $myId);

          Comment


          • #6
            I have 2 Entities
            Profile (1:N) Academic Details

            percentageScored data is available on Academic Details entity.
            Need to show percentageScored on Profile page (latestacademicpercentage)

            I am trying to use this formula but its now working

            Expected Result - Need to show the academic percentage from latest academic record on profile that has percentage data in record

            $idOfRelatedRecord = record\findRelatedOne('AcademicDetail', entity\attribute('id'), 'academicDetails', 'createdAt', 'desc', 'percentageScored>', '50');
            output\printLine($idOfRelatedRecord);

            Reference Image
            Click image for larger version

Name:	image.png
Views:	303
Size:	38.9 KB
ID:	93246

            Click image for larger version

Name:	image.png
Views:	291
Size:	54.8 KB
ID:	93247

            Comment


            • #7
              Still unable to get the correct formula
              Plesae help agri yuri

              Draft Formula

              record\findRelatedOne('Contact', academicDetailsId, 'academicDetails', 'createdAt', 'desc', 'percentageScored>', '50');

              Comment


              • #8
                rmf did you manage to solve your problem? I'm struggling at how to use findrelatedmany and findrelatedone. Could you help me?

                Comment


                • #9
                  I'm just trying to filter record from the related link. After filtering, I'm gonna use "while loop" and delete those filtered records.

                  Comment


                  • #10
                    Hi klausen12,

                    Please clarify the details of your case directly. Tell us about the entities relationships, show screenshots of the records and what you want to achieve. This way we can help you faster.

                    Comment


                    • #11
                      lazovic, well. I have 2 entities. "Score" ManytoOne "CampagneDistribution".
                      I want to filter the record in Score by the value of "CampagneDistribution".


                      I've already created 2 records in "CampagneDistribution" entity.
                      Record 1: Name= CampagnePPPSUD
                      Record 2: Name= Distribution Vivre Ambatoabo​

                      In "CampagneDistribution" there is a boolean field "generateTableofScore".


                      For Record1: When this "generateTableofScore" field is changed to "TRUE", it triggers a workflow which create multiple records in the "Score" entity and linked them with this Record1.
                      I did the same for Record2.

                      Now, when the Record1 "generateTableOfScore" is changed to "FALSE", i want to delete the records in "Score" entity that are only linked with this Record1.

                      The algorithm, i'm thinking to implement is:

                      Step1: use record\findrelatedmany to filter the "score" entity and get le list of IDs
                      Steps2: use record\delete to delete all the records of these list of IDs


                      I'm using a "FlowChart". Its target entity type is "campagneDistribution".


                      The code for deletion:
                      $id=record\findMany('Score', 10, 'createdAt', 'desc');
                      $idscore= record\findRelatedMany('Score', scoresNames , 'CampagneDistribution', 10, 'createdAt', 'desc','campagneDistribution=', name);
                      $i=0;
                      while(
                      $i < array\length($idscore),
                      (
                      $deleteids = array\at($idscore, $i);
                      record\delete("Score",$deleteids);
                      $i = $i+1;
                      )
                      );​
                      Last edited by klausen12; 04-08-2024, 02:02 PM.

                      Comment


                      • #12
                        klausen12,

                        If you are using a formula script in a flowchart with a Campagne Distribution target entity, try using the following option:
                        Code:
                        $campagneDistributionId = bpm\targetEntity\attribute('id');
                        $scoresIds = record\findRelatedMany('CampagneDistribution', $campagneDistributionId, 'scores', 10, 'createdAt', 'desc');
                        
                        $i = 0;
                        
                        while ($i < array\length($scoresIds)) {
                        
                           $scoreId = array\at($scoresIds, $i);
                           record\delete('Score', $scoreId);
                        
                           $i = $i + 1;
                        }​

                        Comment

                        Working...
                        X