Skip to main content
Solved

Create Purchase Receipt using API

  • November 11, 2023
  • 6 replies
  • 105 views

Forum|alt.badge.img

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.

Best answer by jinin

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; }
}

 

 

 

View original
Did this topic help you find an answer to your question?

6 replies

StevenRatner
Varsity I
Forum|alt.badge.img
  • Varsity I
  • 75 replies
  • November 11, 2023

@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);
}

 


Forum|alt.badge.img

Hi , 

I tried with that. but same error is occured


jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • 693 replies
  • November 13, 2023

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

            }

        }

    ]

}


Forum|alt.badge.img

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.


jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • 693 replies
  • Answer
  • November 13, 2023

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; }
}

 

 

 


Forum|alt.badge.img

Jinin,

Thanks it works


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings