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

how to create a Batch job to update previous packing slips

(1) ShareShare
ReportReport
Posted on by 1,836
hi,
I have a requirement to update all the packing slips with the invoice id against them 
using a batch job how can i get it done i plz guide me on it.
thanks 
 
I have the same question (0)
  • Dineshkarlekar Profile Picture
    1,836 on at
    i have made controller class and service class which will check for all packing slips and updates but i have not created any batchjob so not have idea how to get it done can anyone plz guide me on this my code is below 
    final class CustPackingSlpJrSysOperationController    extends SysOperationServiceController
    {
        protected void new()
        {
            super(classStr(CustPackingSlpJrSysOperationService), methodStr(CustPackingSlpJrSysOperationService,processoperation ), SysOperationExecutionMode::Synchronous);
        }
    
        public ClassDescription defaultCaption()
        {
            return "Process Job";
        }
    
        public static CustPackingSlpJrSysOperationController  construct(SysOperationExecutionMode _executionMode = SysOperationExecutionMode::Synchronous)
        {
            CustPackingSlpJrSysOperationController        controller;
            controller = new CustPackingSlpJrSysOperationController ();
            controller.parmExecutionMode(_executionMode);
            return controller;
        }
    
        public static void main(Args _args)
        {
            CustPackingSlpJrSysOperationController        controller;
            controller = CustPackingSlpJrSysOperationController::construct();
            controller.parmArgs(_args);
            controller.startOperation();
        }
    }
    
    final class CustPackingSlpJrSysOperationService  extends SysOperationServiceBase
    {
        public void processoperation()
        {
            CustPackingSlipJour    custPackingSlipJour;
            CustPackingSlipTrans   custPackingSlipTrans;
            CustInvoiceTrans       custInvoiceTrans;
            InventTransOrigin      inventTransOrigin;
            InventTrans            inventTrans;
            str                    curCompany;
    
            CustInvoiceJour             _custInvoiceJour ;
    
            curCompany = curExt();
    
            while select  custInvoiceTrans
                    where custInvoiceTrans.InvoiceId == _custInvoiceJour.InvoiceId
                join inventTransOrigin
                    where inventTransOrigin.InventTransId ==  custInvoiceTrans.InventTransId
                join custPackingSlipTrans
                    group by custPackingSlipTrans.PackingSlipId, custInvoiceTrans.InvoiceId
                    where custPackingSlipTrans.InventTransId == inventTransOrigin.InventTransId
                join inventTrans
                    where inventTrans.InventTransOrigin == inventTransOrigin.RecId
                    && inventTrans.InvoiceId ==  custInvoiceTrans.InvoiceId
                    && inventTrans.PackingSlipId == custPackingSlipTrans.PackingSlipId
            {
                select firstonly custPackingSlipJour
                        where  custPackingSlipJour.PackingSlipId == custPackingSlipTrans.PackingSlipId;
                if(custPackingSlipJour.RecId )
                {
                    custPackingSlipJour.selectForUpdate(true);
                    custPackingSlipJour.InvoiceId =  _custInvoiceJour.InvoiceId;
                    custPackingSlipJour.update();
                }
            }
    
        }
    }
     
  • Suggested answer
    Martin Dráb Profile Picture
    238,740 Most Valuable Professional on at
    You can create a batch job by running the class (its main() method), configuring batch processing in the dialog and confirming the dialog.
     
    Note that it won't have any effect, because you have a bug in processoperation(). You should have tested your code before using it in a batch. You'd see that it does nothing and if you debugged your code, you'd see why. You're trying to find CustInvoiceTrans without any InvoiceId, which makes no sense, and the reason is that you never populated _custInvoiceJour variable.
     
    According to what you said in your to get the invoice no against packing slip, you want to update all historic data, therefore trying to filter by a single Invoice ID is a bug. You should remove that and search all CustPackingSlipJour records without InvoiceId.
  • Dineshkarlekar Profile Picture
    1,836 on at
    hi i have updated the code for batch job 
  • Dineshkarlekar Profile Picture
    1,836 on at
    final class CustPackingSlpJrSysOperationService  extends SysOperationServiceBase
    {
        public void processoperation()
        {
            CustPackingSlipJour    custPackingSlipJour;
            CustPackingSlipTrans   custPackingSlipTrans;
            CustInvoiceTrans       custInvoiceTrans;
            InventTransOrigin      inventTransOrigin;
            InventTrans            inventTrans;
            str                    curCompany;
    
            CustInvoiceJour             _custInvoiceJour ;
    
            curCompany = curExt();
    
            while select custPackingSlipJour
                   join custPackingSlipTrans
                     group by custPackingSlipJour.PackingSlipId, custInvoiceTrans.InvoiceId
                     where  custPackingSlipJour.PackingSlipId == custPackingSlipTrans.PackingSlipId;
                   join inventTransOrigin
                     where  inventTransOrigin.InventTransId == custPackingSlipTrans.InventTransId
                     join  custInvoiceTrans
                     where custInvoiceTrans.InventTransId == inventTransOrigin.InventTransId
                   join inventTrans
                     where inventTrans.InventTransOrigin == inventTransOrigin.RecId
                      && inventTrans.InvoiceId ==  custInvoiceTrans.InvoiceId
                      && inventTrans.PackingSlipId == custPackingSlipTrans.PackingSlipId
                   join custInvoiceJour
                       where  custInvoiceJour.InvoiceId == custInvoiceTrans.InvoiceId
                   {
                       custPackingSlipJour.selectForUpdate(true);
                       custPackingSlipJour.DTInvoiceId =  custInvoiceJour.InvoiceId;
                       custPackingSlipJour.update();
                }
    
        }
    }
    
    plz let me know if this will work in batch job 
    thanks
  • Martin Dráb Profile Picture
    238,740 Most Valuable Professional on at
    Your code won't ever work, because it can't even compile. Let me repeat what I told you recently in another thread of yours:

    1. Compile your code. If it fails to compile, don't both trying to run it. Read the error message and fix the problem. If you don't know how to do it, tell us what are you getting and where.
    2. Run your code. If you get an exception, look at the error message and the place where it occurred. If you can't solve it, share both pieces of information with us.
    3. Review your code.
    4. Debug your code to see where it fails. When you see that, you may be able to resolve the problem by yourself, and if you need someone's help, you'll have more detailed information to share.


    When you fix the bugs, your code will work the same way in batch as when executed directly. Therefore when you successfully test your code and use it from a batch (executed in the right company), there shouldn't be any problem.
     
    Whether the query is correct is another question, not related to batch jobs.
     
    Apart from problems with the business logic, discussed in your other thread called to get the invoice no against packing slip, it's not clear to me why you decided to ignore my advice to find CustPackingSlipJour without DTInvoiceId and why you group by InvoiceId instead. This means that you'll update records that already have DTInvoiceId and you could update the same record several times.
     
    Another suspicious thing is that you're calling update() on a buffer that was grouped (and contains PackingSlipId only).
     
    I think that's enough for now. Please come back when you fixed compilation errors, tested your code (and debugged if needed) and you have questions to ask. Don't forget to give us enough details about the problem.
  • Dineshkarlekar Profile Picture
    1,836 on at
    Hi martin 
     thanks for clarifying all the things where i am getting wrong .
     can  you plz tell me how can i make the query without invoice id do i need to remove any join from my code plz let me know .
     
    thanks 
     
  • Martin Dráb Profile Picture
    238,740 Most Valuable Professional on at
    To find CustPackingSlipJour records without any value in DTInvoiceId, you need to use a where clause, refer to DTInvoiceId field, use == operator and use the empty string as the value. Like this: where custPackingSlipJour.DTInvoiceId == ''.
     
     
  • Dineshkarlekar Profile Picture
    1,836 on at
    hi ,
    martin ,
    i have passed the condition , i am trying to debug , but my debugger is getting exitted unexpectedly , but plz have look on my code i have made some changes you suggested ,should i remove the line from code 
     group by custPackingSlipTrans.PackingSlipId
    final class CustPackingSlpJrSysOperationService  extends SysOperationServiceBase
    {
        public void processoperation()
        {
            CustPackingSlipJour    custPackingSlipJour;
            CustPackingSlipTrans   custPackingSlipTrans;
            CustInvoiceTrans       custInvoiceTrans;
            InventTransOrigin      inventTransOrigin;
            InventTrans            inventTrans;
            str                    curCompany;
    
            CustInvoiceJour             custInvoiceJour ;
    
            curCompany = curExt();
    
            while select custPackingSlipJour
                   join custPackingSlipTrans
                     group by custPackingSlipTrans.PackingSlipId
                     where  custPackingSlipJour.PackingSlipId == custPackingSlipTrans.PackingSlipId
                   join inventTransOrigin
                     where  inventTransOrigin.InventTransId == custPackingSlipTrans.InventTransId
                     join  custInvoiceTrans
                     where custInvoiceTrans.InventTransId == inventTransOrigin.InventTransId
                   join inventTrans
                     where inventTrans.InventTransOrigin == inventTransOrigin.RecId
                      && inventTrans.InvoiceId ==  custInvoiceTrans.InvoiceId
                      && inventTrans.PackingSlipId == custPackingSlipTrans.PackingSlipId
                   join custInvoiceJour
                       where  custInvoiceJour.InvoiceId == custInvoiceTrans.InvoiceId
                        &&    custPackingSlipJour.DataAreaId ==  curCompany
                        &&    custPackingSlipJour.DTInvoiceId == ''
    
                   {
                       custPackingSlipJour.selectForUpdate(true);
                       custPackingSlipJour.DTInvoiceId =  custInvoiceJour.InvoiceId;
                       custPackingSlipJour.update();
                   }
    
           
        }
    }
     
     
    thanks ,
    regards ,
    Dinesh
  • Martin Dráb Profile Picture
    238,740 Most Valuable Professional on at
    The fact that you can't debug your code is a huge problem preventing you from doing your job, therefore resolving it is more important than writing new code that you can't debug. If you need our help, create a new thread (with an appropriate title) and explain your problem in detail there.
     
    Ask yourself why you added the group by clause there and whether the reasons are still valid. You can also explain your reasoning to us, if you want to discuss it.
     
    It's difficult for me to discuss a design of a query that doesn't make sense to me. If I really wanted something like that, I would probably use a view with a computed column returning the first invoice ID for each packing slip ID, and I would likely use this view when needed instead of writing the information to packing slips. But that's likely too advanced for you.
     
    What you can do, for example, is removing grouping and sorting the data by PackingSlipId. To prevent multiple updates of the same record, you can remember the previously processed PackingSlipId and if it's the same, you'll skip update().
     
    Or you can use grouping and then use CustPackingSlipJour::find() to load the record to update.
  • Dineshkarlekar Profile Picture
    1,836 on at
    hi ,
    martin 
    i have debugged my code , actually no record is being select for update so i am getting the error at update statement , i will post image below ,  i use find method what should i have to pass there recid?

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!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

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

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 349

#3
Diego Mancassola Profile Picture

Diego Mancassola 263

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans