findRelatedOne understanding issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DashingUno
    Member
    • Apr 2022
    • 45

    findRelatedOne understanding issue

    Hi everybody, was hoping somebody can help me with findRelatedOne issue I'm facing.


    I have 3 entities:
    Account, Rentalobject, Transactions

    I'm writing a formula for the Transaction entity, that has both Account and Rentalobjects linked to it.
    My end goal is when within Transaction records, I select an Account that it relates to, it then executes:
    Code:
    record\relate('Rentalobject', '$rentalid', 'transactions', $accid);
    I'm having issues with "findRelatedOne" to get the $rentalid
    I can't seem to understand how exactly this formula works.
    In non-formula language what I want is:

    When I create a Transaction record, and within it specify that this transaction is made by Account lets say "Alex"
    I want the formula to assign a Rentalobject field to the Rentalobject that Alex has assigned in his Account entity.

    What my current formula looks like:

    Code:
    $accname = accountName;
    $relid = record\findRelatedOne('Rentalobject', RentalobjectId, 'Account', 'name=', $accname);
    $accid = id;
    record\relate('Rentalobject', '$relid', 'transactions', $accid);

    I know for a fact that $accname tells me the right Name, so lets say "Alex".
    I also know that the record\relate also works (I replaced $relid with the actual Id - and it did what I'm trying to achieve)

    So my issue is with the findRelatedOne, I don't understand its structure despite looking and the Formula FAQ and other forum posts.
    Can't understand which part of it am I getting wrong, will appreciate any help. My current version of findRelatedOne does not work​
  • esforim
    Active Community Member
    • Jan 2020
    • 2204

    #2
    Hi Dashing, this is outside of my skill level and I don't have any working example in my system to copy/paste for your reference. There was one thread I saved long time ago which talk about this:

    Hi, I'm trying to get the latest contact history entries and convert them to a list field, in order to export them. I saw that I could use record\findRelatedMany function. I'm trying to make it works with findRelatedOne (and after use findRelatedMany), but I'm getting My actual code is histId = record\findRelatedOne('Contact


    My only recommendation for you is: start small. Try to get 1 working test to work, then slowly add more code/complexity to your "findOne". Maybe you already did that and is stuck at this final hurdle.

    For example; perhaps try this code first, please note that I'm just demonstrating and this code below is not a Working Example:

    record\findRelatedOne('Rentalobject', RentalobjectId);

    If it work, you would try the next bit:
    record\findRelatedOne('Account', 'name=', $accname);

    If it work, then you would try to combination, and you would be able to see where the code start saying "error".

    Comment

    • item
      Active Community Member
      • Mar 2017
      • 1476

      #3
      same as :

      Code:
      $factureIds=record\findRelatedMany('Periode', $periodeId, 'factures', 300, 'createdAt', 'desc', 'type=', 'Institution');
      '$relid' => no '
      RentalobjectId => certainly/maybe rentalObjectId

      sandbox : depend on how you have relation and where entity is formula
      print if you have => accountId
      print if you have => rentalObjetId
      If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

      Comment


      • DashingUno
        DashingUno commented
        Editing a comment
        Still does not work for me, I guess I'm doing something wrong on the basic level.

        Perhaps you could help with with a simpler solution:

        "account.rentalobjectsIds" - this also give's me the Id I need, but it gives it in a Array, so the output of the attribute above is:["623b75f3d1233eb69"]

        How do I convert to into a string so I can use it here - "record\relate('Rentalobject', '!!HERE!!', 'transactions', $accid);" ?

      • item
        item commented
        Editing a comment
        record\relate('Rentalobject', $relid, 'transactions', $accid);

        Not use ‘

        And too depand : formula is beforeSave, so you don’t have Id for new entity
        Last edited by item; 12-05-2022, 11:37 AM.
    Working...