web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Get only those Purchase orders in which the Item in any of the lines of the Purchase Order is released in more than 1 Company

(0) ShareShare
ReportReport
Posted on by 50

Hi, I have been stuck at this requirement that I am working on. I want to get only those purchase orders in which the item mentioned in the Purchase order lines is released in more than 1 companies. for eg, I have 2 products, P1 and P2. There are 2 Purchase orders PO1 for P1 and PO2 for P2. P1 is released only in 1 company, C1,whereas, P2 is released in C1 as well as C2. So I want to make a runnable class in which I want the Purchase order where the item is released in more than 1 company.

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    304,553 Super User 2026 Season 1 on at

    Hi Anshul,

    Can you explain the purpose? What exact business requirement do you have to solve here? What process is involved? What do you want to execute in such runnable class?

    As a start: There is a global products table and a table for released products. You can use a cross company query to see if a product has been released in multiple legal entities. Then the next step would be probably also a cross company query on the purchase order lines where this product is used.

  • Anshul Thakur Profile Picture
    50 on at

    Hi Andre,

    Thanks for the start. I have used the cross company query for inventtable to check the Items that are released into multiple companies. Now I just want to find the Purchase orders in which these items are used. The requirement is that the client wants a list of purchase orders with items that are released in more than a single company. Here is the code that I have written:

    class ATRunnableClass

    {

       public static void main(Args args)

       {

           PurchLine purchLine;

           PurchTable purchTable;

           InventTable inventtable;

           int countCompany=0;

           while select PurchId from purchTable

           {

               while select ItemId from purchLine where purchLine.PurchId == purchTable.PurchId

               {

                   while select crosscompany ItemId from inventtable where purchLine.ItemId == inventtable.ItemId

                   {

                       countCompany++;

                       if(countCompany > 1)

                       {

                           info(purchTable.PurchId);

                       }

                   }

               }

           }

       }

    }

  • Martin Dráb Profile Picture
    239,392 Most Valuable Professional on at

    Your code is very inefficient - for example, you fetch all PurchTable and PurchLine records from the database, despite the fact that you're interested in just a fraction of them.

    I would consider the following design:

    1. Create a view that will return item IDs used in multiple companies
      1. Use a cross-company query grouped by ItemId
      2. Use count() aggregation function on RecId
      3. Use the having clause to return only ItemId with count(RecId) > 0
    2. Create a query joining PurchLine with the view.
      1. Group by PurchId

    Then you'll make a single DB query to fetch all the data at once.

    By the way, please use Insert > Insert code (in the rich formatting view) to paste source code.

  • Verified answer
    Rustem Galiamov Profile Picture
    8,072 on at

    Hi, you can try to use InventItemGroupItem table. The rows in that table are created when product released.

    It should be something like this:

    static void Job316(Args _args)
    {
        PurchLine           purchLine;
        InventItemGroupItem inventItemGroupItem;
        
        while select purchLine
        exists join inventItemGroupItem
            where inventItemGroupItem.ItemId         == purchLine.ItemId
               && inventItemGroupItem.ItemDataAreaId != purchLine.dataAreaId
        {
            info(purchLine.PurchId);
        }
    }
    

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 689

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 606 Super User 2026 Season 1

#3
CP04-islander Profile Picture

CP04-islander 356

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans