Add ANOTHER connection using the API and jQuery

I have a connection field that already has a connection in it and I need to add another connection with ajax PUT and for the life of me cannot figure out the syntax. It’s a many-to-many table.

I can successfully PUT one connection into the non-raw field and it works fine. If am replacing whatever is in the connection field, I only have to send the connection and not the identifier for it to work.
var UpdateActivitiesObject = {
“field_381”: NewBatchConn
};

But I need to add a connection to what is already there. So I am first reading what the existing connection is, then trying to combine with the new connection and then put both together.

A simpler way of stating my question is this:

The existing connection field_381 has this object in it:

 [
 {"id":"636c257824dc630021195225","identifier":228},
 {"id":"636c20ea52a340002567277b","identifier":223},
]

I want to add another value to the list. How do I format it? I imagine something like this but it doesn’t work.

"field_381": '{"636c257824dc630021195225"}, {"636c20ea52a340002567277b "}, {"636c1957fdda4100211083bb "}'

DETAIL

If I try using the non-raw field, the existing data looks like this
<span class="636c1734fdda410021106e71">215</span><br /><span class="636bfe9c13cf0800233d2327">204</span>

So I have tried to replace it with this
<span class="636c1734fdda410021106e71">215</span><br /><span class="636bfe9c13cf0800233d2327">204</span><br /><span class="636c179b48ef7a002178b661">216</span>

And yet that doesn’t work. The field gets updated with nothing.

I also tried putting the raw version of it into an array, pushing the new values in, then PUTting the new array but it does not seem to want to accept an array at all.

                   //look FOR existing batch value and APPEND new one to it.
                 
                    DoSomeAjax(ActivitiesUpdateURL, 'GET', '', function(ActivityBatch) {      // custom function, it works     
                        
                        // get current Batch value
                        ExistingBatches = ActivityBatch.field_381;
                        
                        if (ExistingBatches.length > 0) {      // if Batches is not null, append
                            // if there are any, array will look like this
                            // 0:{id: '636c0120f72fd500210e43a5', identifier: 205}
                            // 1:{id: '636bfc554c688a0021f3f731', identifier: 203}              

                           // for purposes of this forum,
                           NewBatchID = '216';
                           NewBatchConn = '636c179b48ef7a002178b661';

                            BatchArray = ActivityBatch.field_381_raw;       // get what is already in the array
                            BatchArray.push({id: NewBatchConn, identifier: NewBatchID});
                            
                           // set up the data variable here
                            var UpdateActivitiesObject = {
                                "field_381_raw": BatchArray 
                            };  

                            DoSomeAjax(ActivitiesUpdateURL, 'PUT', UpdateActivitiesObject, function(object10update) {
                                console.log(object10update);         
                                // do other stuff
                            }); 
                        }

Is there something else I can try?? Thank you.

I finally figured it out so am posting for anyone else who needs to ADD CONNECTIONS to a field that already has some connections in it:

It was very confusing because what would come OUT OF the existing array was not the same way that I needed to put it back in! I had to

  • Read the existing connections from the raw connection field
  • Extract ONLY the id from each record
  • Put each existing ID into a temporary array
  • Add the new ID to the temporary array
  • Send the temporary array in my JSON object and put into the NON-RAW field
// get what is already in the RAW array
CurrentArray = MyData.field_381_raw;    
// existing raw array looks like: [{"id":"6377f36b0f0d26002179f1e2","identifier":273}]

// extract connection (ONLY) from existing array and put into new simpler array
NewArray = [];  
for(var a =0; a <CurrentArray.length; a++){
     NewArray.push(CurrentArray[a]['id']);        // take only the id, not the identifier
}

// add the newest value
NewArray.push(NewConn);                  
// looks something like ["4fea069e9e8246001d000698", "4fea069e9e8246001d000699"]

// send them all together into the NON-RAW field
var UpdateActivitiesObject = {
    "field_381":NewArray
};
1 Like