Skip to main content

Notifications

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 208

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;        
    }

}


  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,587 Super User 2024 Season 1 on at
    RE: Want to add exception on site and warehouse while creating sale order through job class.

    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.

  • JJDunaid Profile Picture
    JJDunaid 208 on at
    RE: Want to add exception on site and warehouse while creating sale order through job class.

    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
    Mohit Rampal Profile Picture
    Mohit Rampal 12,552 Super User 2024 Season 1 on at
    RE: Want to add exception on site and warehouse while creating sale order through job class.

    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
    JJDunaid 208 on at
    RE: Want to add exception on site and warehouse while creating sale order through job class.

    I am not getting what sort method to create?

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

  • Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,587 Super User 2024 Season 1 on at
    RE: Want to add exception on site and warehouse while creating sale order through job class.

    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.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,817 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans