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, ...
Suggested Answer

Want to add exception on site and warehouse while creating sale order through job class.

(0) ShareShare
ReportReport
Posted on by 226

I have stored header  data into my "SaleOrderHeader" and lines data into "SaleOrderLines"  and simply created sales order from a job class. Code is given below.

Now want to add validation if have selected site "1" it should belong to the warehouse with site "1". if add site "1" and store it in warehouse "32" which belongs to site site "3" it should

show an error message. I simply want to throw and error.

can anyone help me with code ? how to add validation?

I've used following code in job class to create sales order.

class SalesOrderCreate
{
   
        /// 
    /// Runs the class with the specified arguments.
    /// 
    /// The specified arguments.
    public static void main(Args _args)
    {
        SalesTable           salesTable;
        SaleOrderHeader      saleOrderHeader;
        SalesLine            salesLine;
        SaleOrderLines       saleOrderLines;
        NumberSeq            numberSeq;
        SalesFormLetter      salesFormLetter;
        InventDim            inventDim;
    
        ttsbegin;
        while select * from saleOrderHeader
        {
            // number sequence for newly created sales order
            numberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId()); 
            salesTable.SalesId                 = numberSeq.num();
            numberSeq.used();
            salesTable.initValue();
            // Header data insertion 
            salesTable.CustAccount             = SaleOrderHeader.CustAccount;
            salesTable.initFromCustTable();

            // Validate
            if (!salesTable.validateWrite())
            {
                throw Exception::Error;
            }
            salesTable.insert();

            // Outputs the newly created saleid's
            info(strfmt("sale order created : %1",salesTable.SalesId));

           while  select * from saleOrderLines
            where saleOrderLines.SalesId == saleOrderHeader.SalesId
            // Lines data insertion
            {
                salesline.clear();
                inventDim.clear();
                salesLine.SalesId                       =  salesTable.SalesId;
                salesLine.ItemId                        =  saleOrderLines.ItemId;
                salesLine.SalesQty                      =  saleOrderLines.SalesQty;
                salesLine.CurrencyCode                  =  saleOrderLines.CurrencyCode;
                salesline.SalesPrice                    =  saleOrderLines.SalesPrice;
                salesLine.ShippingDateRequested         =  saleOrderLines.ShippingDateRequested;
                salesLine.LineAmount                    =  salesLine.calcLineAmount();
                inventDim.InventSiteId                  =  saleOrderLines.Warehouse;
                inventDim.InventLocationId              =  saleOrderLines.Location;
               
                salesLine.InventDimId=InventDim::findDim(inventDim).inventDimId ;
        
                salesLine.createLine(true,true,false,true,true);
            }
        }
        ttscommit;        
    }

}


I have the same question (0)
  • Bharani Preetham Peraka Profile Picture
    3,634 Moderator on at

    You can add a new method which validates the site and warehouse from respective tables and can call that method at line after 52. If it fails, then it will throw error.

  • JJDunaid Profile Picture
    226 on at

    I am not getting what sort method to create?

    can you explain through code how to validate site and warehouse?

  • Suggested answer
    Mohit Rampal Profile Picture
    12,573 Moderator on at

    Hi, You can add below code to validate if Site and Warehouse combination exist. A warehouse can have only one site. Call this method inside SalesOrderLines while loop at beginning. (Always good to validate data first, if all good then map other fields)

    public void validateSiteWarehouseCombination()
    {
        InventSiteId inventSiteIdLocal = InventLocation::Find(saleOrderLines.Location).InventSiteId;
        
        if (saleOrderLines.Warehouse != inventSiteIdLocal)
        {
            throw error("Invalid Site");
        }
    }

  • JJDunaid Profile Picture
    226 on at

    class SalesOrderCreate
    {
       
        InventSite inventSite;
        InventLocation inventLocation;
        InventDim      inventDim;
        SaleOrderLines saleOrderLines;
       
    
        public void validateSiteWarehouse()
        {
            info(strFmt("good : %1",inventDim.InventLocationId));
            InventSiteId inventSiteIdLocal = InventLocation::Find(saleOrderLines.warehouse).InventSiteId;
    
            if (saleOrderLines.warehouse != inventSiteIdLocal)
            {
                throw error("Invalid Site");
            }
        }
    
         //
         //Runs the class with the specified arguments.
         //
         //The specified arguments.
        public static void main(Args _args)
        {
            SalesTable           salesTable;
            SaleOrderHeader      saleOrderHeader;
            SalesLine            salesLine;
            SaleOrderLines       saleOrderLines;
            NumberSeq            numberSeq;
            SalesFormLetter      salesFormLetter;
            InventDim            inventDim;
            SalesOrderCreate     salesOrderCreate = new SalesOrderCreate();
    
            ttsbegin;
            
            while select * from saleOrderHeader
            {
                // number sequence for newly created sales order
                numberSeq                          = NumberSeq::newGetNum(SalesParameters::numRefSalesId()); 
                salesTable.SalesId                 = numberSeq.num();
                numberSeq.used();
                salesTable.initValue();
                // Header data insertion 
                salesTable.CustAccount             = SaleOrderHeader.CustAccount;
                salesTable.initFromCustTable();           
    
                // Validate
                if (!salesTable.validateWrite())
                {
                    throw Exception::Error;
                }
                salesTable.insert();
    
                // Outputs the newly created saleid's
                info(strfmt("sale order created : %1",salesTable.SalesId));
    
                while  select * from saleOrderLines
                where saleOrderLines.SalesId == saleOrderHeader.SalesId
                // Lines data insertion
               
                {    
                //  validation method 
                    salesOrderCreate.validateSiteWarehouse();
                    
                    salesline.clear();
                    inventDim.clear();
                    salesLine.SalesId                       =  salesTable.SalesId;
                    salesLine.ItemId                        =  saleOrderLines.ItemId;
                    salesLine.SalesQty                      =  saleOrderLines.SalesQty;
                    salesLine.CurrencyCode                  =  saleOrderLines.CurrencyCode;
                    salesline.SalesPrice                    =  saleOrderLines.SalesPrice;
                    salesLine.ShippingDateRequested         =  saleOrderLines.ShippingDateRequested;
                    salesLine.LineAmount                    =  salesLine.calcLineAmount(); 
                    inventDim.InventSiteId                  =  saleOrderLines.site;
                    inventDim.InventLocationId              =  saleOrderLines.warehouse; 
                    salesLine.InventDimId                   =  InventDim::findDim(inventDim).inventDimId ;
                    salesLine.createLine(true,true,false,true,true);
    
                   
                }
            }
            ttscommit;        
        }
    
    }

    Hi Rohit,

    I have tried your logic as you can see in above code ! but it is not working properly instead of showing error it is creating sales order.

    Thank you.

  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    3,634 Moderator on at

    public boolean validateSiteWarehouse()
        {
            boolean ret;
            info(strFmt("good : %1",inventDim.InventLocationId));
            InventSiteId inventSiteIdLocal = InventLocation::Find(saleOrderLines.warehouse).InventSiteId;
    
            if (saleOrderLines.warehouse != inventSiteIdLocal)
            {
                ret = checkFailed("Invalid Site");
            }
            
            return ret;
        }
        
    if (salesOrderCreate.validateSiteWarehouse())
    {
       //insert data
    }
    
    else
    {
       // throw error
    }
                    
                   

    You have to give a condition like if the validation fails then throw error. Otherwise insert the data. I have written the code for you and changed the method a bit.

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 733

#2
André Arnaud de Calavon Profile Picture

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

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 278 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans