0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Apex REST でのエラー(入力に使うJsonの設定とクラスの定義)

Last updated at Posted at 2024-12-18

Apex Restを作っていてJson の値をポストした時のエラーです。

{
  "subject" : "Wingo Ducks",
  "Description" : "707-555-1234",
  "TaskSubtype" : "Task",
  "OwnerName" : "Wingo Ducks",
  "Status" : "Completed",
  "Date ActivityDate" : "2024-12-18",
  "supplier_code" : "10005"
  "customer_code" : null,
}

400エラーですね

[{"message":"Unexpected parameter encountered during deserialization: subject at [line:3, column:16]","errorCode":"JSON_PARSER_ERROR"}]

以下の例をみると... icって定義がありますね。
https://developer.salesforce.com/docs/atlas.ja-jp.apexcode.meta/apexcode/apex_rest_methods.htm

image.png

もしかして、calss名を追加する?

{
Body053 :{
  "subject" : "Wingo Ducks",
  "Description" : "707-555-1234",
  "TaskSubtype" : "Task",
  "OwnerName" : "Wingo Ducks",
  "Status" : "Completed",
  "Date ActivityDate" : "2024-12-18",
  "supplier_code" : "10005"
  "customer_code" : null,
}
}

エラーは変わらずです。
もしかしてb53aの変数と一緒にしないといけないのかな?

    @HttpPost
    global static string doPost(Body053 b53a){
        system.debug(Logginglevel.INFO,'==========> '+ b53a);
{
b53a :{
  "subject" : "Wingo Ducks",
  "Description" : "707-555-1234",
  "TaskSubtype" : "Task",
  "OwnerName" : "Wingo Ducks",
  "Status" : "Completed",
  "Date ActivityDate" : "2024-12-18",
  "supplier_code" : "10005"
  "customer_code" : null,
}
}

エラーが変わりました。あっ、日付型のところの項目名がおかしい

[{"message":"Unknown field: CreateTaskRESTAPI.Body053.Date ActivityDate at [line:8, column:26]","errorCode":"JSON_PARSER_ERROR"}]

"Date ActivityDate" --> "ActivityDate" に変更

[{"message":"Unexpected character ('"' (code 34)): was expecting comma to separate OBJECT entries at [line:10, column:4]","errorCode":"JSON_PARSER_ERROR"}]

"10005"の後にカンマが無いな。

無事に解決です。思ったより悩みました。

List型に対応させる

    @HttpPost
    global static string doPost(List<Body053> b53List){
        system.debug(Logginglevel.INFO,'==========> '+ b53List);

Postするデータは以下のフォーマット

{
"b53List" :[{
  "subject" : "Wingo Ducks",
  "Description" : "707-555-1234",
  "TaskSubtype" : "Task",
  "OwnerName" : "Wingo Ducks",
  "Status" : "Completed",
  "ActivityDate" : "2024-12-18",
  "supplier_code" : "10005",
  "customer_code" : null
},
{
  "subject" : "Wingo Ducks2",
  "Description" : "707-555-1234",
  "TaskSubtype" : "Task",
  "OwnerName" : "Wingo Ducks",
  "Status" : "Completed",
  "ActivityDate" : "2024-12-18",
  "supplier_code" : "10005",
  "customer_code" : null
}
]}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?