Solved

Include refernce which creating Entry from java sdk

  • 11 September 2023
  • 2 replies
  • 35 views

I am able to create new entries through my Spring boot app using Java Management SDK. How can I include the references to other content types while creating the entries?

 

Example: I have a content type called Product with a reference to another content type Item. So when I will create a new Entry for a Product from Java SDK, how can I include the Item reference along with the product request? (I have already available Item entries inside the stack)

 

Note: I have already worked with entry.includeReference() to get the reference data for another content type entry along with the main Content type Entry.

icon

Best answer by shailesh Mishra 12 September 2023, 09:21

View original

2 replies

Userlevel 1

Hi @lalatendu, To do this, you need to add a key reference_field_uid that will be an array of objects. In this array, you should create an object and provide the UID of the entry and the _content_type_uid from which you want to include the reference, as shown below.

{
"entry": {
"title": "Entry title",
"url": "Entry URL",
"reference_field_uid": [{
"uid": "blt111000d1e110b001",
"_content_type_uid": "referred_content_type_uid"
}]
}
}

complete code :

// Create Content Object
JSONObject content = new JSONObject()
content.put("title", "Entry title");
content.put("url", "Entry url");

// Create Referece array objects
JSONArray arrayReference = new JSONArray()
JSONObject objRef = new JSONObject()
objRef.put("uid", "blt111000d1e110b001")
objRef.put("_content_type_uid", "referred_content_type_uid")
arrayReference.put(objRef)
content.put("reference_field_uid", arrayReference);

// Add content
JSONObject body = JSONObject()
body.put("entry", content)

// Use Java Management SDK
Contentstack contentstack = Contentstack.Builde().build();
Entry entry = contentstack.stack("apiKey").entry();
Call<ResponseBody> response = entry.create(body).execute();
if (response.isSuccessful) {
System.out.println("Response" + response);
}


See the documentation:

https://www.contentstack.com/docs/developers/apis/content-management-api#create-an-entry

@shailesh Mishra Thank you for the quick update. Much appreciated.

Reply