Solved

Object reference not set to an instance of an object Error in create json body

  • 15 November 2023
  • 1 reply
  • 67 views

Userlevel 3
Badge

Hi,

I want to create a JSON body when user clicking the REMOVE HOLD in PO Screen.

public IEnumerable ReleaseFromHold(PXAdapter adapter, ReleaseFromHoldDelegate baseMethod){
RootObject obj = new RootObject();
obj.OrderNbr = order.OrderNbr?.ToString();
obj.PromisedOn = order.ExpectedDate?.ToString();
obj.Status = order.Status?.ToString();


foreach (PXResult<POLine> result in polineIntview.Select()){

POLine line = result;
Details detail = new Details();

detail.Amount = line.CuryExtCost?.ToString();
detail.CustomerOrderNbr = SOOrder.PK.Find(Base,"SO",line.SOOrderNbr).CustomerOrderNbr?.ToString();
detail.DiscountAmount = line.CuryDiscAmt?.ToString();
detail.DiscountPercent = line.DiscPct?.ToString();

if (obj.Details == null)
obj.Details = new List<Details>();

obj.Details.Add(detail);



}


var options = new JsonSerializerOptions { WriteIndented = true };
var json=JsonSerializer.Serialize(obj,options);

PXTrace.WriteInformation(json);

}

public class RootObject
{
public string OrderNbr { get; set; }
public string PromisedOn { get; set; }
public string Status { get; set; }
public List<Details> Details { get; set; }

}

public class Details
{
public string Amount { get; set; }
public string CustomerOrderNbr { get; set; }
public string DiscountAmount { get; set; }
public string DiscountPercent { get; set; }
}

But,in the some time I click the Remove hold button it shows an error.Then I tried again in same PO ,JSON is generated.

Why this is happen?

 

icon

Best answer by aaghaei 15 November 2023, 10:03

View original

1 reply

Userlevel 7
Badge +8

@jeewanishalika20 I am not sure what are you trying to do. If you are meaning to create SO from PO, there are better and more reliable options available. Anyways I did some re-write on your code to generate the json code. I tested it works, removes the PO from hold and based on my Status Diagram sets it to Email Purchase Order. Please note as I didn’t now how you are defining your variables like “order” I replaced them with Base Views so you might want to change them. Here is the revised code:

using System.Collections;
using System.Collections.Generic;
using System.Text.Json;
using PX.Data;
using PX.Objects.PO;
using PX.Objects.SO;
using PX.Web.UI;

namespace HCL.Custom.Misc
{
public class HCLPOOrderEntry_Misc : PXGraphExtension<POOrderEntry>
{
public static bool IsActive() => true;

public delegate IEnumerable ReleaseFromHoldDelegate(PXAdapter adapter);
[PXOverride]
[PXButton]
public virtual IEnumerable ReleaseFromHold(PXAdapter adapter, ReleaseFromHoldDelegate baseMethod)
{
RootObject obj = new RootObject();
obj.OrderType = Base.Document.Current.OrderType;
obj.OrderNbr = Base.Document.Current.OrderNbr;
obj.PromisedOn = Base.Document.Current.ExpectedDate?.ToString();
obj.Status = Base.Document.Current.Status;

foreach (PXResult<POLine> result in Base.Transactions.Select())
{

POLine line = result;
Details detail = new Details();

detail.Amount = line.CuryExtCost?.ToString();
detail.CustomerOrderNbr = SOOrder.PK.Find(Base, "SO", line.SOOrderNbr)?.CustomerOrderNbr;
detail.DiscountAmount = line.CuryDiscAmt?.ToString();
detail.DiscountPercent = line.DiscPct?.ToString();

if (obj.Details == null)
obj.Details = new List<Details>();

obj.Details.Add(detail);
}


var options = new JsonSerializerOptions { WriteIndented = true };
var json = JsonSerializer.Serialize(obj, options);

PXTrace.WriteInformation(json);

return baseMethod.Invoke(adapter);
}

public class RootObject
{
public string OrderType { get; set; }
public string OrderNbr { get; set; }
public string PromisedOn { get; set; }
public string Status { get; set; }
public List<Details> Details { get; set; }

}

public class Details
{
public string Amount { get; set; }
public string CustomerOrderNbr { get; set; }
public string DiscountAmount { get; set; }
public string DiscountPercent { get; set; }
}
}
}

 

"{\r\n  \"OrderType\": \"SB\",\r\n  \"OrderNbr\": \"PO000115\",\r\n  \"PromisedOn\": \"06/30/2022 12:00:00 AM\",\r\n  \"Status\": \"H\",\r\n  \"Details\": [\r\n    {\r\n      \"Amount\": \"36538.0000\",\r\n      \"CustomerOrderNbr\": null,\r\n      \"DiscountAmount\": \"0.0000\",\r\n      \"DiscountPercent\": \"0.000000\"\r\n    }\r\n  ]\r\n}"

 

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