3 Replies Latest reply on Dec 19, 2017 11:17 AM by Laurent Matheo

    Associate Records via JavaScript Api

    Richard Rattmann

      Hi all,



      how do I associate records with the JavaScript API?

      Or to be more specific. I'm trying to associate Records that I have just created with rxRecordInstanceResource.


      @ Laurent Matheo i have a feeling that you might be able to help me with this.




        • 1. Re: Associate Records via JavaScript Api
          Laurent Matheo

          Hi Richard

          Sorry I saw your question and forgot about it.


          So the answer to your question is no, and yes and well...


          I°) No:

          Javascript framework do not provide methods to associate records.



          II°) Yes (?):

          You could ask a rule or process to do this for you and this would be the preferred method.

          The idea would be to:

          • Create a "dummy" record definition, for example "my action", with two text fields "record definition ID Parent", "record definition ID Child".
          • You would have a rule triggered on "Create" for record definition "my action" that would:
            • Have an "Associate Record" activity using both "record definition ID Parent" and "record definition ID Child",
            • Delete the "Current Record" of "my action" to "clean" your temporary record,


          So the main idea is that you create your record instances to associate (parent / child), create a record instance in the "dummy" record definition passing the parent / child instance Ids and the rule would then associate them and delete your "dummy" record,


          From Javascript, you would:

          • Create a record instance for the "Parent" and get its record Instance ID,
          • Create a record instance for the "Child" and get its record Instance ID,
          • Create a record instance in "my action" and pass both record instance Ids in "record definition ID Parent" and "record definition ID Child" respectively,


          The rule on "Create" for "my action" would trigger and do the association and then delete the "dummy" record from "my action".



          III°) Wellll:

          There is a solution I found a while ago I only list for fun and that is not officially supported and so we do not recommend it.


          This solution can do it using Javascript code.


          What the code does is:

          Loading the parent record instance (from its record instanceID), creating the necessary object that list association operations, Saving the record instances.

          As here I want to add a new association, I put an object within "pending" with the instance Id of the child record.

          When you save the parent record instance with the association object, the association will be created.

          As you can see, you can, the same way, delete association.

          You should be careful with the "existing" array that, I guess, should list the existing ones so you should get it


          // Create a record instance resource for the parent.
          var parentResource=rxRecordInstanceResource.withName('com.example.taskmanager:Task');
          // Loading parent record Instance Id (AGG1234Parent)
              var objToAdd={};
               // Creating a new association with a child record (its record Instance ID is "AGGChild")
               // As it is a creation, the association needs to be defined in "Pending" array.
               // Adding the association object to the Parent record Instance.
               // Saving the Parent record instance, with the new association object.
          • 2. Re: Associate Records via JavaScript Api
            Richard Rattmann

            HI Laurent,


            thanks for the detailed Answer.

            Point III looks interesting. I might try to implement this.

            But i'll probably end up using II. We have already used "dummy records" to trigger rules and processes before so im confident with that solution.

            • 3. Re: Associate Records via JavaScript Api
              Laurent Matheo

              Yes this is the best option for now