How to Create more than one record with single request using Web-Api?

Certified Senior Developer

I got one point in docs, i.e 

  • Using the Write Records web API template, define an API that lets you create more than one Employee with a single request.

link is - Web API Tutorial - Level II - Appian 23.3

so, please anybody help me on that, how to create or implement that?

Thanks in advance.

  Discussion posts and replies are publicly visible

Parents
  • +1
    Certified Lead Developer

    a!writeRecords takes any number of records. This means you just have to send a list of JSON dictionaries to that API.

  • 0
    Certified Senior Developer
    in reply to Stefan Helzle

    Thanks for reply  , Actually same way I am trying, but in Data Base table goes single entry.

    what is the issue?

  • 0
    Certified Senior Developer
    in reply to Stefan Helzle

    In Json Body I pass - 

    [{
    "empName": "Jhon",
    "empAddress": "Pune",
    "empPhno": 676776
    },
    {
    "empName": "Sophia",
    "empAddress": "Pune",
    "empPhno": 676776
    }
    ]

    But in DB table create single entry.

  • 0
    Certified Lead Developer
    in reply to rajnis1428

    And the code of your API?

  • 0
    Certified Senior Developer
    in reply to Stefan Helzle

    a!localVariables(
    local!value: cast(

    'recordType!{bd4b9ea2-4954-420e-ad92-2a6d15209cde}SP Employee',
    a!fromJson(http!request.body)

    ),

    a!writeRecords(
    records: local!value,
    /*
    * Construct an HTTP response to return to the caller
    */
    onSuccess: a!httpResponse(
    statusCode: 200,
    /*
    * Set an HTTP header that tells the client that the body of the response will be in JSON format
    */
    headers: {
    a!httpHeader(name: "Content-Type", value: "application/json")
    },
    /*
    *In the response body, return the records created or updated
    */
    body: a!toJson(fv!recordsUpdated)
    ),
    onError: a!httpResponse(
    statusCode: 500,
    headers: {
    a!httpHeader(name: "Content-Type", value: "application/json")
    },
    body: a!toJson(
    a!map(
    message: "Write request has failed",
    error: fv!error
    )
    )
    )
    )
    )

  • 0
    Certified Lead Developer
    in reply to rajnis1428

    This is because your cast() call transforms the JSON list into a single instance of a record. Use the a!listType() function to cast the JSON into a list of records.

  • a!localVariables(
      local!value: cast(a!listType('recordType!{b6372ff1-12f3-4b4c-96fd-a3ce8e6d553f}IA VOF Customer'),
       
        a!fromJson(http!request.body)
      ),
     
     if(a!isNotNullOrEmpty(index(local!value,'recordType!{b6372ff1-12f3-4b4c-96fd-a3ce8e6d553f}IA VOF Customer.fields.{7c162d1b-faa4-4448-986d-fad23864e190}id',null)),
     a!httpResponse(
       statusCode: 400,
       headers:  a!httpHeader(
         name: "Content-Type",
         value: "application/json"
       ),
       body: a!toJson(
         {
           error: "Employee data cannot include an id."
         }
       )
     ),
     a!writeRecords(
       records: local!value,
       /*
        * Construct an HTTP response to return to the caller
        */
       onSuccess: a!httpResponse(
         statusCode: 200,
         /*
          * Set an HTTP header that tells the client that the body of the response will be in JSON format
          */
         headers: {
           a!httpHeader(name: "Content-Type", value: "application/json")
         },
         /*
          *In the response body, return the records created or updated
          */
         body: a!toJson(fv!recordsUpdated)
       ),
       onError: a!httpResponse(
         statusCode: 500,
         headers: {
           a!httpHeader(name: "Content-Type", value: "application/json")
         },
         body: a!toJson(
           a!map(
             message: "Write request has failed",
             error: fv!error
           )
         )
       )
     )
     )
     
    )

    1. I tried using a!listType() function to cast the JSON into a list of records. Not sure if I used it correctly, but my code is entering the if condition even though I am not passing the id in  the body. I am getting status code 400 .Can you please  share the updated code for this so that it works. 

    2. Also , can you please explain from where this fv!recordsUpdated coming.

    Below is the data , I am passing in the body:

    [
    {

    "name" : "Annie",
    "tier" : "1",
    "phone" : "1234567",
    "createdon" : "12/7/2023",
    "addressid": "26"
    },
    {

    "name" : "Rachel",
    "tier" : "3",
    "phone" : "1234567",
    "createdon" : "12/7/2023",
    "addressid": "27"
    }
    ]

  • 0
    Certified Lead Developer
    in reply to ankitas1125

    Your code seems to be OK in general. The issue here is that this snippet

    index(local!value,'recordType!IA VOF Customer.fields.id',null)
    

    returns a list of null values, and isNotNullOrEmpty() returns false.

    You need to check each individual record in the list. The all() function can help you with this. See below.

    a!localVariables(
      local!data: {
        a!map(name: "Hans"),
        a!map(id: null, name: "Otto"),
        a!map(name: "Fritz")
      },
      all(a!isNullOrEmpty(_), local!data.id)
    )

  • Thanks   , the function worked but still there is an issue.

    As per the code logic what I want is , if I am passing the "id" in my body, that time it should return me status code 400. But now the code is working even if I am passing the "id" in my body.

    What I want is , it should add only those records in the records list where the id is null and it should return status 400 for the record where the id parameter is passed.

    In my code attached , I am giving id in one record and not passing id on another , but both of them are getting stored in the record list which should not be the case.

    Can you please share me the updated code for how to make this work.

    a!localVariables(
      local!value: cast(a!listType('recordType!{b6372ff1-12f3-4b4c-96fd-a3ce8e6d553f}IA VOF Customer'),
       
        a!fromJson(http!request.body)
      ),
     
      if(all(a!isNotNullOrEmpty(_),index(local!value,'recordType!{b6372ff1-12f3-4b4c-96fd-a3ce8e6d553f}IA VOF Customer.fields.{7c162d1b-faa4-4448-986d-fad23864e190}id',null)),
     a!httpResponse(
       statusCode: 400,
       headers:  a!httpHeader(
         name: "Content-Type",
         value: "application/json"
       ),
       body: a!toJson(
         {
           error: "Employee data cannot include an id."
         }
       )
     ),
     a!writeRecords(
       records: local!value,
       /*
        * Construct an HTTP response to return to the caller
        */
       onSuccess: a!httpResponse(
         statusCode: 200,
         /*
          * Set an HTTP header that tells the client that the body of the response will be in JSON format
          */
         headers: {
           a!httpHeader(name: "Content-Type", value: "application/json")
         },
         /*
          *In the response body, return the records created or updated
          */
         body: a!toJson(fv!recordsUpdated)
       ),
       onError: a!httpResponse(
         statusCode: 500,
         headers: {
           a!httpHeader(name: "Content-Type", value: "application/json")
         },
         body: a!toJson(
           a!map(
             message: "Write request has failed",
             error: fv!error
           )
         )
       )
     )
     )
     
    )

Reply
  • Thanks   , the function worked but still there is an issue.

    As per the code logic what I want is , if I am passing the "id" in my body, that time it should return me status code 400. But now the code is working even if I am passing the "id" in my body.

    What I want is , it should add only those records in the records list where the id is null and it should return status 400 for the record where the id parameter is passed.

    In my code attached , I am giving id in one record and not passing id on another , but both of them are getting stored in the record list which should not be the case.

    Can you please share me the updated code for how to make this work.

    a!localVariables(
      local!value: cast(a!listType('recordType!{b6372ff1-12f3-4b4c-96fd-a3ce8e6d553f}IA VOF Customer'),
       
        a!fromJson(http!request.body)
      ),
     
      if(all(a!isNotNullOrEmpty(_),index(local!value,'recordType!{b6372ff1-12f3-4b4c-96fd-a3ce8e6d553f}IA VOF Customer.fields.{7c162d1b-faa4-4448-986d-fad23864e190}id',null)),
     a!httpResponse(
       statusCode: 400,
       headers:  a!httpHeader(
         name: "Content-Type",
         value: "application/json"
       ),
       body: a!toJson(
         {
           error: "Employee data cannot include an id."
         }
       )
     ),
     a!writeRecords(
       records: local!value,
       /*
        * Construct an HTTP response to return to the caller
        */
       onSuccess: a!httpResponse(
         statusCode: 200,
         /*
          * Set an HTTP header that tells the client that the body of the response will be in JSON format
          */
         headers: {
           a!httpHeader(name: "Content-Type", value: "application/json")
         },
         /*
          *In the response body, return the records created or updated
          */
         body: a!toJson(fv!recordsUpdated)
       ),
       onError: a!httpResponse(
         statusCode: 500,
         headers: {
           a!httpHeader(name: "Content-Type", value: "application/json")
         },
         body: a!toJson(
           a!map(
             message: "Write request has failed",
             error: fv!error
           )
         )
       )
     )
     )
     
    )

Children
No Data