Solved

Create Purchase Receipt using API

  • 11 November 2023
  • 6 replies
  • 64 views

Userlevel 3
Badge

I have a requirement to create a purchase receipt using Acumatica API.

This is my code for that.

      RootObject obj = new RootObject();
obj.Type = "Receipt";
obj.VendorID = "GRI SL";
obj.VendorRef = "2131";
obj.Locatrion = "Main";
obj.ShipmentRef = "00028";
obj.Date = DateTime.ParseExact("10/10/2022", "dd/MM/yyyy",CultureInfo.InvariantCulture);
obj.PostPeriod = DateTime.ParseExact("10/10/2022", "dd/MM/yyyy",CultureInfo.InvariantCulture);

var jsonStrings = JsonConvert.SerializeObject(obj);
PXTrace.WriteInformation(jsonStrings);
var jsonString = JObject.Parse(jsonStrings);

PXTrace.WriteInformation(jsonString.ToString());


var URL = "https://globalrubberindustrieslimited.acumatica.com/entity/CustomEndpoint/22.200.001/PurchaseReceipt";
var httpRequest = (HttpWebRequest)WebRequest.Create(URL);
httpRequest.Method = "PUT";
httpRequest.ContentType = "application/json";
httpRequest.Headers.Add("Authorization", "Bearer " + my token);
httpRequest.SendChunked = true;

using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(jsonString);
}

var httpResponse = (HttpWebResponse)httpRequest.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
var responseJson = JsonConvert.DeserializeObject(responseText);
PXTrace.WriteInformation(responseText);

}

Json object in Trace 

 

But , when calling the API It shows an error.And also ,When I use the same json via Postman It shows the same error.

Can someone please help me for that.

icon

Best answer by jinin 13 November 2023, 06:36

View original

6 replies

Userlevel 5
Badge

@jeewanishalika20 Try the following:

{
    RootObject obj = new RootObject
    {
        Type = "Receipt",
        VendorID = "GRI SL",
        VendorRef = "2131",
        Location = "Main", // Corrected the typo here
        ShipmentRef = "00028",
        Date = DateTime.ParseExact("10/10/2022", "dd/MM/yyyy", CultureInfo.InvariantCulture),
        PostPeriod = DateTime.ParseExact("10/10/2022", "dd/MM/yyyy", CultureInfo.InvariantCulture)
    };

    string jsonStrings = JsonConvert.SerializeObject(obj);
    PXTrace.WriteInformation(jsonStrings);

    // Directly use the serialized string
    string URL = "https://globalrubberindustrieslimited.acumatica.com/entity/CustomEndpoint/22.200.001/PurchaseReceipt";
    var httpRequest = (HttpWebRequest)WebRequest.Create(URL);
    httpRequest.Method = "PUT";
    httpRequest.ContentType = "application/json";
    httpRequest.Headers.Add("Authorization", "Bearer " + myToken); // Ensure 'myToken' is securely stored
    httpRequest.SendChunked = true;

    using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
    {
        streamWriter.Write(jsonStrings); // Directly write the serialized JSON string
    }

    using (var httpResponse = (HttpWebResponse)httpRequest.GetResponse())
    {
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();
            PXTrace.WriteInformation(responseText);
            
            // Optional: Deserialize the response if needed
            // var responseJson = JsonConvert.DeserializeObject(responseText);
        }
    }
}
catch (WebException webEx)
{
    // Handle WebException
    PXTrace.WriteInformation("WebException: " + webEx.Message);
}
catch (Exception ex)
{
    // Handle other exceptions
    PXTrace.WriteInformation("Exception: " + ex.Message);
}

 

Userlevel 3
Badge

Hi , 

I tried with that. but same error is occured

Userlevel 7
Badge +11

Hi @jeewanishalika20 ,

To create a purchase receipt, there is no need to pass the shipment number. I have provided a sample JSON for creating a purchase receipt through the API. You can structure the JSON as shown below and give it a try.

{

    "Type": {

        "value": "Receipt"

    },

    "VendorID": {

        "value": "CONGOODTOL"

    },

    "VendorRef": {

        "value": "000109-ret"

    },

    "Location": {

        "value": "MAIN"

    },

    "PostPeriod": {

        "value": "102023"

    },

    "Date": {

        "value": "2023-10-28"

    },

    "Details": [

        {

            "InventoryID": {

                "value": "CONTRAIN1"

            },

            "ReceiptQty": {

                "value": 1200.000000

            }

        }

    ]

}

Userlevel 3
Badge

Hi Jinin,

Okey , But my concern is acumatica is only accept like this format.

{"Type": {

        value: "Receipt"

    }

}

But , my generated json is below like that.

{"Type":  "Receipt"

}

How I do that.

Userlevel 7
Badge +11

Hi @jeewanishalika20 

Please find the Sample code,
 

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
// Create an instance of the PurchaseReceipt class and populate its properties
PurchaseReceipt purchaseReceipt = new PurchaseReceipt
{
Type = new PropertyValue { Value = "Receipt" },
VendorID = new PropertyValue { Value = "CONGOODTOL" },
VendorRef = new PropertyValue { Value = "000109-ret" },
Location = new PropertyValue { Value = "MAIN" },
PostPeriod = new PropertyValue { Value = "102023" },
Date = new PropertyValue { Value = "2023-10-28" },
Details = new List<Detail>
{
new Detail
{
InventoryID = new PropertyValue { Value = "CONTRAIN1" },
ReceiptQty = new PropertyValue { Value = "1200.000000" }
}
}
};

// Convert the object to JSON
string json = JsonConvert.SerializeObject(purchaseReceipt, Formatting.Indented);

// Print the generated JSON
Console.WriteLine(json);
}
}

// Define the classes to represent the structure of the JSON
public class PurchaseReceipt
{
public PropertyValue Type { get; set; }
public PropertyValue VendorID { get; set; }
public PropertyValue VendorRef { get; set; }
public PropertyValue Location { get; set; }
public PropertyValue PostPeriod { get; set; }
public PropertyValue Date { get; set; }
public List<Detail> Details { get; set; }
}

public class Detail
{
public PropertyValue InventoryID { get; set; }
public PropertyValue ReceiptQty { get; set; }
}

public class PropertyValue
{
public string Value { get; set; }
}

 

 

 

Userlevel 3
Badge

Jinin,

Thanks it works

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved