Insert to reference and multi reference field

Hi, I am creating a project where I need to automatically link the current member to a from when submitting it (using fully custom form and dataset for this). I have been trying to get a grasp of the insert Reference documentation but haven’t been able to figure it our. according to the documentation the link gets linked to a specific item but not a specific cell, how do I specify this?

you are also supposed to specify the ID of the item that it needs to be inserted to but as this is an item that still has to be created by clicking the submit button how do you know the ID in advance?
Below you can find my messy code :slight_smile: , thanks for the help.

export function button 1_click(event) {
    
    let options = {fieldsets: [ 'FULL' ]}
    currentMember.getMember(options)
        .then((member) => {
    
        const id = member._id;


        const loggedIn = member ? true : false;

 
        const loginEmail = member.loginEmail;
        const contactEmails = member.contactDetails.emails;


        wixData.query("InvestmentReadinessreport")
          .find()
          .then( (results) => {
            if(results.items.length > 0) {
              let firstItem = results.items[0];


                    wixData.insertReference("Members/PrivateMembersData",     "InvestmentReadinessreport", firstItem._id , member._id )
                      .then( () => {
                        console.log("Reference inserted");
                      } )
                      .catch( (error) => {
                        console.log(error);
                      } );
       

            } else {
   
            }
            } )
            .catch( (err) => {
              let errorMsg = err;
            } );

Depending on how you set up the collection, the owner of each row should be tied to the current user logged in, their id gets added on creation of the row in the Owner system field. However this would not work with a logged out /unregistered user as you would get a blank cell for that row.
You can then just pull the data from the member table using this id when retrieving a row from your investment collection.

in your code, If the form hasn’t been submitted when this runs, it will find the first row in the collection and add the current member to it. This row could be another members data that gets overwritten or switched.

Hi Jay, thank you for the answer, after messing around a bit I have stumbled upon a problem with your approach.

In my case I would like to add the person that is submitting the form into a multi reference field since all people that work at the company will be linked to the company in this way (this is submitted in the companies dataset). it would not work with the owner field since you can only link 1 person. a company can also be created by an admin and afterwards a user can link themselves to that company. in that case the admin should also not be the owner of the item. Would you have any idea how to solve this issue?

Many thanks!