Announcement

Collapse
No announcement yet.

Automatic link to relationship

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

  • Automatic link to relationship

    Hello everyone,

    I have 3 entities: LEAD (parent), APPUNTAMENTO, and CONTRATTO (children).
    In the "Lead" section, I can see the relationships "Appuntamenti" and "Contratti" by selecting the Lead's name from the respective field (Attachment 1). However, I am struggling to automate the relationship between Appuntamenti and Contratti. Let me explain it better: I would like that when I create a new "Contratto" record or update it, the "Appuntamento LeadName" is automatically selected from the dropdown list (if it exists) (Attachment 2), and vice versa. I have tried this formula, which works for creating the name but not for selecting it from the dropdown list (relationship):

    $nameLead = entity\attribute("parent.name");
    $nameAppointment = string\concatenate("Appuntamento ", $nameLead);
    Appuntamento = $nameAppointment;

    How do I select from a dropdown list/relationship?

    As a solution, I would also be fine with a relationship that shows the Appuntamenti in the lower panels (similar to how it works for leads, but changing the type of relationship). The important thing is that it shows/selects automatically after choosing the Lead.

    I hope I have been clear. Thank you in advance!​
    Attached Files
    Last edited by MyloStark; 07-25-2023, 11:34 AM.

  • #2
    screenshots not attached
    Rabii
    Web Dev

    Comment


    • MyloStark
      MyloStark commented
      Editing a comment
      uploaded, I noticed it while it was being moderated! 😅

  • #3
    if i understand this correct, you want when editing or creating a new contract, you want to automatically relate an existing appoinmtent from the parent lead to the contract ? is this what you want ?
    Rabii
    Web Dev

    Comment


    • #4
      Exact. When I create a new Contract I would like to insert only the Lead and then automatically correlate the Appointment, perhaps with a formula, but that would be fine also a different relationship that shows the correlated Appointment in the lower panels as happens in Lead>Appointments/Contracts

      Comment


      • #5
        only thing you should noticed is that the lead -> has Many appointments, which means you will have to select only one of the appointments. question Appointment is this a custom entity or is it (Meeting) ?
        Rabii
        Web Dev

        Comment


        • #6
          In reality they would prefer to change a single appointment so, in theory, it would be 1:1 but for customers it doesn't matter, that's why I was saying that the relationship could also be changed, the important thing is that the automatic association works.
          Appointments and Contracts are Custom entities​.

          Comment


          • #7
            Here is a formula that could do the job, this formula should be used in Contract formula entity, also please change the name of the fields and entities based on the correct ones in your instance:

            PHP Code:
            ifThen(entity\isAttributeChanged('parentLeadId') && parentLeadId,

            $appointmentId record\findOne('Appointment'nullnull'leadId='parentLeadId);

                
            ifThen($appointmentIdentity\setAttribute('appointmentId'$appointmentId))
            )
            ​ 
            Rabii
            Web Dev

            Comment


            • #8
              as soon as I can try and update, in the meantime, thanks!

              Comment


              • rabii
                rabii commented
                Editing a comment
                you are welcome

            • #9
              ​hello, IT WORKS but the update only takes place or is visible only when I reload the page. In Formula, I currently have this:

              //Create name
              $nameContract= entity\attribute("parent.name");
              $newName = string\concatenate("Contratto ", $nameContract);
              name = $newName;
              //Update Vendor Appointment
              ifThen(entity\isAttributeChanged('parentId') && parentId,
              $appointmentId = record\findOne('VendorAppointment', null, null, 'parentId=', parentId);
              ifThen($appointmentId, entity\setAttribute('appuntamentoVId', $appointmentId));
              )​

              if it can be useful I noticed that if I try to reverse the order of the 2 formulas both don't work.
              - Cache emptied​
              - I tried in both Before Save Script and API
              Last edited by MyloStark; 07-26-2023, 11:00 AM.

              Comment


              • #10
                what do you mean by reverse the order of the 2 formulas ?

                Yes the result will appear only after the page is refreshed. if you want instant changes formula is not the way, you will have to create a custom view for your link and send a request to an api endpoint to recover data and change it.
                Rabii
                Web Dev

                Comment


                • #11
                  I meant the formulas //Create name and //Update Vendor Appointment.
                  "to create a custom view for your link and send a request to an api endpoint to recover data and change it." I see it a complicated thing? Is it feasible for someone like me who couldn't even do the previous formula? 😅​

                  Comment


                  • #12
                    Ok fir the first question why the formulas don't work when you change their order is simply you have to add a semi colon to the end of the last formula, like below

                    PHP Code:

                    //Update Vendor Appointment
                    ifThen(entity\isAttributeChanged('parentId') && parentId,
                       
                    $appointmentId record\findOne('VendorAppointment'nullnull'parentId='parentId);
                       
                    ifThen($appointmentIdentity\setAttribute('appuntamentoVId'$appointmentId));
                    )
                    ​​;

                    //Create name ( you can simplify this code into one line like below)
                    name string\concatenate("Contratto "entity\attribute('parent.name'));​ 
                    This should work fine. If you know how to code you can definitely do it but you need to be aware of how espocrm works and how you can add custom code.
                    Rabii
                    Web Dev

                    Comment


                    • #13
                      work this too, you're the top!

                      Comment


                      • #14
                        Can I still take advantage of your availability? the question is very similar, I have a text field in the Lead (parent), I would like to modify it when I update the "faseAcquisto" field of Contract. This is the Formula (in Contract):

                        ifThen(faseAcquisto=="Consegnato",
                        entity\setAttribute("parent.test","prova")
                        );
                        output\print(parent.test);​

                        Because it does not work? But above all because the print is able to get to the parent field while the setAttribute is not?
                        Last edited by MyloStark; 07-26-2023, 01:47 PM.

                        Comment


                        • #15
                          Hey,

                          sure here is how you can do it, change the test to the name of the field on the lead and also prova to the value you want to set for that field:

                          PHP Code:
                          //Update test field of the Lead (Parent) with the value prova
                          ifThen(faseAcquisto == "Consegnato" && parentId,

                             
                          record\update('Lead'parentId'test''prova');
                          );
                          ​ 
                          Hope this helps.
                          Rabii
                          Web Dev

                          Comment

                          Working...
                          X