I had a heck of a time finding an example that worked for me, so here are a few I figured out. Obviously, you need to replace the subDomain, apiToken, appId, and field details with your app info.
VBA macro (Excel, Work, etc.):
Public Sub test()
Dim myRecord1, myRecord2, myRecord3, response1, response2, subDomain, apiToken, appId
Dim myData As New Collection
'change these for your app/environment
subDomain = "YourSubdomain"
apiToken = "YourApiToken"
appId = YourAppID
Set myRecord1 = CreateObject("Scripting.Dictionary")
myRecord1("NumberFieldCode") = 1
myRecord1("TextFieldCode") = "text"
Set myRecord2 = CreateObject("Scripting.Dictionary")
myRecord2("NumberFieldCode") = 2
myRecord2("TextFieldCode") = "more text"
Set myRecord3 = CreateObject("Scripting.Dictionary")
myRecord3("NumberFieldCode") = 3
myRecord3("TextFieldCode") = "even more text"
'first test sending one record as dictionary
response1 = addKintoneRecords(subDomain, apiToken, appId, myRecord1)
'next test sending multiple dictionary objects in a collection
myData.Add myRecord2
myData.Add myRecord3
response2 = addKintoneRecords(subDomain, apiToken, appId, myData)
'show results
MsgBox response1 & Chr(10) & response2
End Sub
Public Function addKintoneRecords(ByVal subDomain As String, ByVal apiToken As String, ByVal appId As Integer, ByVal data)
'use at your own risk. no warranty expressed or implied
'initial 3/12/2020 Dave Gilpin
Dim recordString, jsonString, recType, fieldType, rec, fieldCode, objRequest
jsonString = ""
For Each rec In data
recordString = ""
recType = VarType(rec)
If recType <> 9 Then 'if the record type is not object set rec to data
Set rec = data
End If
For Each fieldCode In rec
fieldType = VarType(rec(fieldCode))
If recordString <> "" Then recordString = recordString & "," 'more than one field
recordString = recordString & """" & fieldCode & """:{""value"":" 'add field name
If fieldType = 8 Then recordString = recordString & """" 'string so add quotes
recordString = recordString & rec(fieldCode) 'add value
If fieldType = 8 Then recordString = recordString & """" 'string so add quotes
recordString = recordString & "}" 'close json record object
Next
If jsonString <> "" Then jsonString = jsonString & "," 'more than one record
jsonString = jsonString & "{" & recordString & "}" 'add the recordString to the jsonString
If recType = 8 Then Exit For 'if the record type is not object don't keep looping
Next
jsonString = "{""app"":" & appId & ",""records"":[" & jsonString & "]}" 'put it all together
Set objRequest = CreateObject("MSXML2.XMLHTTP")
With objRequest
.Open "POST", "https://" & subDomain & ".kintone.com/k/v1/records.json", True
.setRequestHeader "X-Cybozu-API-Token", apiToken
.setRequestHeader "Content-Type", "application/json"
.Send jsonString 'send to kintone
While objRequest.readyState <> 4 'wait for response
DoEvents
Wend
addKintoneRecords = .ResponseText 'return response
End With
End Function
Javascript in local file: I didn’t put as much effort into this one, since I figure it might not be as useful. You have to run it from IE, since Chrome can be strict.
<html>
<head>
<script type="text/javascript">
function test() {
var subDomain = "YourSubdomain";
var apiToken = "YourApiToken";
var appId = YourAppID;
json = {
"app": appId, "records": [
{
"NumberFieldCode": { "value": 1 }, "TextFieldCode": { "value": "some test data" }
},
{
"NumberFieldCode": { "value": 2 }, "TextFieldCode": { "value": "some more test data" }
}
]
};
var req = new XMLHttpRequest();
req.addEventListener("load", requestResponse);
req.open("POST", "https://" + subDomain + ".kintone.com/k/v1/records.json", true);
req.setRequestHeader("X-Cybozu-API-Token", apiToken);
req.setRequestHeader("Content-Type", "application/json");
req.send(JSON.stringify(json));
}
function requestResponse() {
document.getElementById("response").innerText = this.responseText;
}
</script>
</head>
<body>
<input type="button" onclick="test();" value="test" />
<textarea id="response" cols="100" rows="20"> </textarea>
</body>
</html>