This is part 2 of 3 of The School of REST. In Part 1 we covered creating our Account “Burger Palace” and using the filter API to get just the records we wanted. Today we’ll cover retrieving records, creating our Contact “Bob Burger”, updating records, and associating records together.
6. Retrieve a specific Account – Burger Palace
We created “Burger Palace” with the id “demo_burger_palace”. To retrieve it using the RESTful interface, we just need to do a GET to rest/v10/Accounts/demo_burger_palace
7. Create a Contact – Bob Burger
We have our customer account “Burger Palace”, but relationships are all about the people. Let’s create a Contact named “Bob Burger”. Using what we learned against Accounts, we can apply it to Contacts. Let’s do a POST to /rest/v10/Contacts
We’ll create “Bob Burger” with the id “demo_bob_burger”. Remember the we DO NOT need to pass in the id. We are only doing this for demo purposes right now. SugarCRM will automatically generate an id and return it as part of the response.
8. Update a Record – Bob Burger
Great, we’ve created “Bob Burger” by doing a POST request. Now let’s update him by doing a PUT request to /rest/v10/Contacts/demo_bob_burger
9. Let’s associate our Contact and Account
We have “Burger Palace” and “Bob Burger” now let’s bring them together by doing a POST to /rest/v10/Accounts/demo_burger_palace/link
We’ll need to pass two arguments.
- a list of ids we wish to relate to “Burger Palace” which in this case is demo_bob_burger.
- a link_name which is the name of the relationship we wish to use. In this case it is “contacts”, but it’s not always the name of the related module.
10. Get Contacts Associated to an Account – Burger Palace
We just related “Bob Burger” to “Burger Palace” now let’s view that relationship by retrieving the contacts related to “Burger Palace”. All we need to do is a GET request to /rest/v10/Accounts/demo_burger_palace/link/contacts where “contacts” is the same link_name we passed up in step 9.
Now you’ll see in the response that “Bob Burger” is associated to “Burger Palace”