Question

Unable to update Stock Item using webhook

  • 18 November 2022
  • 3 replies
  • 89 views

Userlevel 2
Badge

we have a requirement to update few fields in stock Items screen, but we are facing “Field not found: 'PX.Objects.IN.InventoryItemMaintBase.Item'." error. Please have a look at below code.
we are on Build 22.109.0023.

 

using Newtonsoft.Json;
using PX.Data;
using PX.Data.Webhooks;
using PX.Objects.IN;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Results;

namespace WebHooksConfiguration
{
    public class ObjectInventoryItemJSON
    {
        public string InventoryId { get; set; }
        public string Description { get; set; }     
    }
    public class KWInsInventoryItemSync : IWebhookHandler
    {
        public async Task<System.Web.Http.IHttpActionResult> ProcessRequestAsync(
       HttpRequestMessage request, CancellationToken cancellationToken)
        {
            using (var scope = GetAdminScope())
            {
                if (request.Method == HttpMethod.Post)
                {
                    string jsonContent = request.Content.ReadAsStringAsync().Result;

                    var obj = JsonConvert.DeserializeObject<ObjectInventoryItemJSON>(jsonContent);

                    if (obj != null)
                    {
                        using (var ts = new PXTransactionScope())
                        {
                            ProcessData(obj.InventoryId, obj.Description);

                            ts.Complete();
                        }
                    }
                }
            }

            return new OkResult(request);
        }

        public void ProcessData(string InventoryId, string Description) 
        {
            if (!string.IsNullOrEmpty(InventoryId))
            {
                var graph = PXGraph.CreateInstance<NonStockItemMaint>();
                InventoryItem newItem = graph.Item.Search<InventoryItem.inventoryCD>(InventoryId);

                if (newItem != null)
                {
                    graph.Item.Current = newItem;
                }
            }
        }      
        private IDisposable GetAdminScope()
        {
            var userName = "admin";
            if (PXDatabase.Companies.Length > 0)
            {
                var company = PXAccess.GetCompanyName();
                if (string.IsNullOrEmpty(company))
                {
                    company = PXDatabase.Companies[0];
                }
                userName = userName + "@" + company;
            }
            return new PXLoginScope(userName);
        }
    }
}
 


3 replies

Userlevel 7
Badge +17

Hi, @ckwiat46  I just modified a little bit of your code, can you please try the below code?

using Newtonsoft.Json;
using PX.Data;
using PX.Data.Webhooks;
using PX.Objects.IN;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Results;

namespace WebHooksConfiguration
{
public class ObjectInventoryItemJSON
{
public string InventoryId { get; set; }
public string Description { get; set; }
}
public class KWInsInventoryItemSync : IWebhookHandler
{
public async Task<System.Web.Http.IHttpActionResult> ProcessRequestAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
using (var scope = GetAdminScope())
{
if (request.Method == HttpMethod.Post)
{
string jsonContent = request.Content.ReadAsStringAsync().Result;

var obj = JsonConvert.DeserializeObject<ObjectInventoryItemJSON>(jsonContent);

if (obj != null)
{
using (var ts = new PXTransactionScope())
{
ProcessData(obj.InventoryId, obj.Description);

ts.Complete();
}
}
}
}

return new OkResult(request);
}

public void ProcessData(string InventoryId, string Description)
{
if (!string.IsNullOrEmpty(InventoryId))
{
var graph = PXGraph.CreateInstance<NonStockItemMaint>();
InventoryItem newItem = PXSelect<InventoryItem, Where<InventoryItem.inventoryCD, Equal<Required<InventoryItem.inventoryCD>>>>.Select(Base,inventoryid.Trim());


if (newItem != null)
{
graph.Item.Current = newItem;
}
}
}
private IDisposable GetAdminScope()
{
var userName = "admin";
if (PXDatabase.Companies.Length > 0)
{
var company = PXAccess.GetCompanyName();
if (string.IsNullOrEmpty(company))
{
company = PXDatabase.Companies[0];
}
userName = userName + "@" + company;
}
return new PXLoginScope(userName);
}
}
}

 

 

 

Userlevel 2
Badge

Hello @Naveen Boga i tried the code you suggested but still facing same issue.

//at this place we are getting error.
graph.Item.Current = newItem;

Userlevel 7
Badge

Hi @ckwiat46 were you able to find a solution? Thank you!

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