Skip to main content
Solved

Run the code when clicking Process with the acction "Create Blank Items" in Process Page

  • October 21, 2024
  • 2 replies
  • 44 views

On the Process Order page.
I have created an action “Create Blanket Lines”


Here is my code
 

using System;
using System.Collections;
using System.Collections.Generic;
using PX.Data;
using PX.Objects.CN.Subcontracts.AP.CacheExtensions;
using PX.Objects.SO;
using static PX.Web.Customization.RowFilesTree;

public class SOCreateShipment_Extension : PXGraphExtension<PX.Objects.SO.SOCreateShipment>
{

    protected void ProcessCustomization()
    {
        PXTrace.WriteInformation("open 1");
        SOOrderFilter filter = Base.Filter.Current as SOOrderFilter;
        if (filter != null && filter.Action == "SO301000$addBlanketLine")
        {
            foreach (SOOrder order in Base.Caches[typeof(SOOrder)].Updated)
            {
                if (order.Selected == true)
                {
                    // My customization code
                }
            }
        }
    
    }
}


The problem I'm having is:
I want when I select the action “Create Blanket Lines’ and press the Process button, it will run the ProcessCustomization code( without affecting other actions ). Because I want to be able to use Processing Dialog Box to control whether my code runs correctly.

I haven't done that yet.

I have tried using the following ways:
    public PXAction<SOOrderFilter> Process;
    [PXButton]
    [PXUIField(DisplayName = "Process")]
    protected IEnumerable process(PXAdapter adapter) {
         ProcessCustomization();
    }

or 
 

use protected void SOOrderFilter_RowSelected(PXCache cache, PXRowSelectedEventArgs e)


However, it is not correct, it all affects when I click process for another action.

Please help me regarding this issue (I select the action “Create Blanket Lines’ and press the Process button, it will run the ProcessCustomization code( without affecting other actions )).
 

 

Best answer by aiwan

Hi @PhatPham 

 

Have you tried using a different name for the action?

I think it may be causing issues and running the original ‘Process’ action too.

Maybe renaming it to ‘Process Customizations’ would help with running just the ProcessCustomization() method.

Make sure you rename the method and the display name.

 

Aleks

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

2 replies

Forum|alt.badge.img+8
  • Captain II
  • 366 replies
  • Answer
  • October 21, 2024

Hi @PhatPham 

 

Have you tried using a different name for the action?

I think it may be causing issues and running the original ‘Process’ action too.

Maybe renaming it to ‘Process Customizations’ would help with running just the ProcessCustomization() method.

Make sure you rename the method and the display name.

 

Aleks


  • Author
  • Freshman I
  • 2 replies
  • October 31, 2024
aiwan wrote:

Hi @PhatPham 

 

Have you tried using a different name for the action?

I think it may be causing issues and running the original ‘Process’ action too.

Maybe renaming it to ‘Process Customizations’ would help with running just the ProcessCustomization() method.

Make sure you rename the method and the display name.

 

Aleks

I am using the code
 

    public PXAction<SOOrderFilter> processCustomization;
    [PXButton(CommitChanges = true)]
    [PXUIField(DisplayName = "Process Blanket Lines", Visible = true)]
    protected IEnumerable ProcessCustomization(PXAdapter adapter)
    {
        SOOrderFilter filter = Base.Filter.Current as SOOrderFilter;
        if (filter != null && filter.Action == "SO301000$addBlanketLine")
        {
            List<SOOrder> orders = new List<SOOrder>();
            foreach (SOOrder order in Base.Caches<SOOrder>().Updated)
            {
                orders.Add(order);
            }

            PXProcessing.SetCurrentItem(orders);

            foreach (SOOrder order in orders)
            {
                PXProcessing<SOOrder>.SetCurrentItem(order);  processed
                var filePath = PXCache<SOOrderFilter>.GetExtension<SOOrderFilterExt>(filter).UsrNextSchShipDate;
                if (filePath != null)
                {
                    try
                    {
                        PXTrace.WriteInformation(filePath.ToString());
                        PXTrace.WriteInformation($"OrderType: {order.OrderType}");
                        PXTrace.WriteInformation($"OrderNbr: {order.OrderNbr}");
                        SOOrder parentOrder = // mycode
                        if (parentOrder != null)
                        {
                            int shipOnMonthOffset = 3;
                            GenerateNewLineItems(parentOrder, filePath, shipOnMonthOffset);
                            PXProcessing<SOOrder>.SetProcessed(); /
                        }
                        else
                        {
                            PXProcessing<SOOrder>.SetError(new PXException("The order has not been selected from the list"));
                        }
                    }
                    catch (Exception ex)
                    {
                        PXProcessing<SOOrder>.SetError(new PXException($"Error processing order {order.OrderNbr}: {ex.Message}"));
                    }
                }
                else
                {
                    PXProcessing<SOOrder>.SetError(new PXException("There is no Next Sch Ship Date yet"));
                }
            }
        }
        return adapter.Get();
    }

    private void GenerateNewLineItems(SOOrder parentOrder, DateTime? nextSchShipDate, int shipOnMonthOffset)
    {
        PXTrace.WriteInformation("GenerateNewLineItems open 1");
        using (PXTransactionScope ts = new PXTransactionScope())
        {
            SOOrderEntry orderEntryGraph = PXGraph.CreateInstance<SOOrderEntry>();
            orderEntryGraph.Document.Current = // my code
            PXTrace.WriteInformation("GenerateNewLineItems open 2");
            var duplicatedBranchesAndInventories = new HashSet<(int?, int?)>();
            try
            {
                foreach (SOLine line in PXSelect<SOLine,
                    Where<SOLine.orderType, Equal<Required<SOLine.orderType>>,
                    And<SOLine.orderNbr, Equal<Required<SOLine.orderNbr>>>>>.Select(orderEntryGraph, parentOrder.OrderType, parentOrder.OrderNbr))
                {
                    if (line.Completed == true)
                    {
                        PXProcessing.SetCurrentItem(line);
                        try
                        {
                            var key = (line.BranchID, line.InventoryID);
                            if (!duplicatedBranchesAndInventories.Contains(key))
                            {
                                SOLine newLine = new SOLine
                                {
                                    BranchID = line.BranchID,
                                    InventoryID = line.InventoryID,
                                    UOM = line.UOM,
                                    RequestDate = line.RequestDate,
                                    CustomerLocationID = line.CustomerLocationID,
                                    OrderType = line.OrderType,
                                    OrderNbr = line.OrderNbr,
                                    ShipDate = nextSchShipDate,
                                    Completed = false,
                                    LineType = line.LineType,
                                    Qty = line.Qty
                                };
                                orderEntryGraph.Transactions.Cache.Insert(newLine);
                                duplicatedBranchesAndInventories.Add(key);
                            }
                        }
                        catch (Exception ex)
                        {
                            PXProcessing<SOLine>.SetError(new PXException($"Error inserting new line for order {parentOrder.OrderNbr}: {ex.Message}"));
                        }
                    }
                }
                orderEntryGraph.Save.Press();
                ts.Complete();
            }
            catch (Exception ex)
            {
                PXTrace.WriteError(ex);
                throw new PXOperationCompletedException("Error: Issue Generate New Line Items " + ex.Message);
            }
        }
    }
}

however the PXProcessing dialog is still not displayed. I want that when I click the Process Blanket Lines button then the PXProcessing dialog should be displayed like this image.


I want to use Processing to control the insert of Sales Order Lines
please help me
 


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