Skip to main content
Question

How to return response from webhook


Forum|alt.badge.img

Hello,

we are trying to update some fields in Stock Items screen using webhook and its working without issue, but the problem we would like to return some custom message after update is done. Can you some please help us how we can achieve this. we are in build 24R1 version 24.107.0004. Please have a look at sample code below
 

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using PX.Api.Webhooks;
using PX.Data;
using PX.Objects.CR;
using PX.Objects.PM;

namespace TogglWebhook
{
    public class TogglWebhookHandler : IWebhookHandler
    {
        private static readonly JsonSerializer Serializer = new JsonSerializer();
        public async Task HandleAsync(WebhookContext context, CancellationToken cancellation)
        {
            if (!context.Request.Headers.TryGetValue(HeaderNames.Authorization, out var authValue))
            {
                context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                return;
            }

            if (authValue != "Bearer token")
            {
                context.Response.StatusCode = StatusCodes.Status403Forbidden;
                return;
            }

            using (var scope = GetAdminScope())
            {
                using (var jr = new JsonTextReader(context.Request.CreateTextReader()))
                {
                    var payload = Serializer.Deserialize<Dictionary<String, Object>>(jr);
                    if (payload == null)
                    {
                        context.Response.StatusCode = StatusCodes.Status400BadRequest;
                        return;
                    }
                    
                    var graph = PXGraph.CreateInstance<NonStockItemMaint>();
                    InventoryItem newItem = graph.Item.Search<InventoryItem.inventoryCD>(InventoryId);

                    if (newItem != null)
                    {
                        graph.Item.Current = newItem;
                        
                         graph.Save.Press();
                    }
                }
            }
        }
        private IDisposable GetAdminScope()
        {
            var userName = "admin";
            return new PXLoginScope(userName);
        }
    }
}

0 replies

Be the first to reply!

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