API V8 Create Account to Contact Relationship

Hi,

Just after an example of creating a Relationship between an Account and Contact(s) using the latest version of the API

I have this, but its not working

this is my url:
/api/v8/modules/Accounts/23fa0f9d-fbdc-406b-0a48-5ab326dda5db/relationships/contacts



{  
   "data":{  
      "id":"",
      "type":"Accounts",
      "attributes":{  
         "id":"23fa0f9d-fbdc-406b-0a48-5ab326dda5db"
      },
      "relationships":{  
         "contacts":{  
            "data":[  
               {  
                  "id":"1ff95909-2f1c-bc37-de48-5ab327409652",
	          "type":"Contact",
               }
            ]
         }
      }
   }
}

Any tips appreciated

Regards
Brendan

I also tried the following:


{  
   "data":{  
      "type":"accounts",
      "id":"",
      "relationships":{  
         "contacts":{  
            "data":[
            {  
               "type":"contact",
               "id":"819af9c4-44a2-ccad-55bf-5abc710c17e0"
            },
            {  
               "type":"contact",
               "id":"1ff95909-2f1c-bc37-de48-5ab327409652"
            }            
            ]
         }
      }
   }
}

But this returned the following error:

[SuiteCRM] [API] [Conflict] [ModuleController] [Unable to add relationships (to many)] false

Found the way this works

using postman to test. My url looks like:

/api/v8/modules/Accounts/23fa0f9d-fbdc-406b-0a48-5ab326dda5db/relationships/contacts

my json looks like:


{  
   "data":[  
      {  
         "type":"contacts",
         "id":"819af9c4-44a2-ccad-55bf-5abc710c17e0"
      }
   ]
}

Bump.

This problem still persists. It appears that the relationship creation API entry point does not work at all. Any pointers?

I’ve just been doing this, and it seems to work for me, but my url and body looks a bit different.
I am on 7.11.0 currently.
Try

/Api/V8/modules/Account/23fa0f9d-fbdc-406b-0a48-5ab326dda5db/relationships

And

{  
   "data":{  
         "type":"Contacts",
         "id":"819af9c4-44a2-ccad-55bf-5abc710c17e0"
      }
}
2 Likes

I appreciate the helpful reply.

When referring to the API documentation it appears that the code you specified is the same as what the documentation states is used for deleting a relationship. Is there a distinction between these two that I’m missing?

I had to check the actual code I am using, and it does a POST to “module/Accounts/{$accountid}/relationships”
with

$relation = array(
“data” => array(
“type” => “Contacts”,
“id” => $contactid
)
);

which seems to work as intended. (relate a contact to/below an account)

I see what you mean about the doc and delete using the same code, and can only guess it is incorrect, and it should be a DELETE not a POST. I have not tested this.
Please test this thoroughly before you put it in production!

1 Like

I see that now and in my tests with postman, I can confirm that the distinction is that DELETE does not accept a body, but when the DELETE http method is used the API interprets this correctly as a delete, save for the issue noted where there is a library not included as expected. However, that issue aside I believe this helps to solve the immediate issue of how to use the API.

Again thank you!

Related Issue where delete entry point is not working as expected.
https://github.com/salesagility/SuiteCRM/issues/6761

1 Like