I'm running this code in the execute anonymous console and it works, the record gets assigned to the correct asset as expected..
String data = '{"DOKA_Device_Id__c": "DOKA789123456","Opening_Speed__c": 50.5,"Force_Limit_Open__c": 150}';
Door_Event__c newEvent = new Door_Event__c();
JSONParser parser = JSON.createParser(data);
String fieldName;
String fieldValTxt;
Decimal fieldValNum;
parser.nextToken();
//advance to the next token
while (parser.nextToken() != JSONToken.END_OBJECT) {
//if a name token, populate the fieldName
IF (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
fieldName = parser.getCurrentName();
parser.nextToken();
}
// add strings to text values and numbers to number values
IF (parser.getCurrentToken() == JSONToken.VALUE_STRING){
fieldValTxt = parser.getText();
System.debug('first one should be DOKA: ' + fieldValTxt);
IF (fieldName == 'DOKA_Device_Id__c') {
Asset newDoor = [SELECT Id from Asset where Doka_Device_Id__c = :fieldValTxt LIMIT 1];
newEvent.Asset__c = newDoor.Id;
System.debug('Asset is: ' + newEvent.Asset__c);
}else{
newEvent.put(fieldName, fieldValTxt);
}
}else if (parser.getCurrentToken() == JSONToken.VALUE_NUMBER_INT){
fieldValNum = parser.getDecimalValue();
newEvent.put(fieldName, fieldValNum);
}else if (parser.getCurrentToken() == JSONToken.VALUE_NUMBER_FLOAT){
fieldValNum = parser.getDecimalValue();
newEvent.put(fieldName, fieldValNum);
}
}
insert newEvent;
However when i submit this through an authenticated postman session (it's a @HttpPost class), i'm getting the following error..
[
{
"message": "Unexpected parameter encountered during deserialization: DOKA_Device_Id__c at [line:2, column:24]",
"errorCode": "JSON_PARSER_ERROR"
}
]
The data being submitted is a copy/paste from the String value in the anon console, but I can't figure out why this JSON Parser error is being thrown.
The HttpPost is below:
@HttpPost
global static Door_Event__c sendDoor(String data) {
//felipe
System.debug('The data is: ' + data);
Door_Event__c newEvent = new Door_Event__c();
JSONParser parser = JSON.createParser(data);
String fieldName;
String fieldValTxt;
Decimal fieldValNum;
parser.nextToken();
//advance to the next token
while (parser.nextToken() != JSONToken.END_OBJECT) {
//if a name token, populate the fieldName
IF (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
fieldName = parser.getCurrentName();
parser.nextToken();
}
// add strings to text values and numbers to number values
IF (parser.getCurrentToken() == JSONToken.VALUE_STRING){
fieldValTxt = parser.getText();
System.debug('first one should be DOKA: ' + fieldValTxt);
IF (fieldName == 'DOKA_Device_Id__c') {
Asset newDoor = [SELECT Id from Asset where Doka_Device_Id__c = :fieldValTxt LIMIT 1];
newEvent.Asset__c = newDoor.Id;
System.debug('Asset is: ' + newEvent.Asset__c);
}else{
newEvent.put(fieldName, fieldValTxt);
}
}else if (parser.getCurrentToken() == JSONToken.VALUE_NUMBER_INT){
fieldValNum = parser.getDecimalValue();
newEvent.put(fieldName, fieldValNum);
}else if (parser.getCurrentToken() == JSONToken.VALUE_NUMBER_FLOAT){
fieldValNum = parser.getDecimalValue();
newEvent.put(fieldName, fieldValNum);
}
}
insert newEvent;
return newEvent;
}