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

Notifications

Announcements

Community site session details

Community site session details

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

Batch class not return for all records

(0) ShareShare
ReportReport
Posted on by

Hi all,

I want to write a batch class that allows it to add a line according to the objectid to the work order that has no line for all workorders. The code I wrote only adds for a work order. How can I return all work orders?

public void run()
    {
        EntAssetWorkOrderTable workOrder ;          
        
        EntAssetObjectTable objecttable;        
        
        EntAssetWorkOrderLine workOrderLine;
        
        ;
        #OCCRetryCount
     
        try
        {
            ttsbegin;
            while select workOrder
            {
               
                select firstonly objecttable
                    where objecttable.ObjectID == workOrder.ObjectID_WM;
             
                if(objecttable)
                {
                    if(workOrderLine.RecId == 0)
                    {
              

                  
                        //workOrderLine.selectForUpdate(true);
                        workOrderLine.WorkOrder = workOrder.RecId;
                        workOrderLine.Object = objecttable.RecId;
                        workOrderLine.JobType = workOrder.JobType;
                        workOrderLine.insert();
                 
                    
                    }
               
                }
               
            }
            ttscommit;
        }

I have the same question (0)
  • ergun sahin Profile Picture
    8,826 Moderator on at

    As Martin said, you are not selecting data before the control line. So it's empty.

    But after the first insertion the line table has value. In the second loop (and other loops) it never skips the control because the table  has first inserted record

  • Martin Dráb Profile Picture
    238,322 Most Valuable Professional on at

    You never put any value to workOrderLine variable, therefore it'll always be empty.

    If you want to check if a record exists, you need tp make a request to database. Maybe you can use one of find*() methods on EntAssetWorkOrderLine table.

  • Granger  Profile Picture
    on at

    But I want it to run the insert if there is no record in the line. That's why I added the if condition line. How should I change it taking this into consideration?

  • ergun sahin Profile Picture
    8,826 Moderator on at

    public void run()
        {
            EntAssetWorkOrderTable workOrder ;          
            
            EntAssetObjectTable objecttable;        
            
            EntAssetWorkOrderLine workOrderLine;
            
            ;
            #OCCRetryCount
         
            try
            {
                ttsbegin;
                while select workOrder
                {
                   
                    select firstonly objecttable
                        where objecttable.ObjectID == workOrder.ObjectID_WM;
                 
                    if(objecttable)
                    {
                        //clear workOrderLine or next line will create problem for you
                        workOrderLine.clear();
                        if(workOrderLine.RecId == 0)
                        {
                            
                            
                      
                            //workOrderLine.selectForUpdate(true);
                            workOrderLine.WorkOrder = workOrder.RecId;
                            workOrderLine.Object = objecttable.RecId;
                            workOrderLine.JobType = workOrder.JobType;
                            workOrderLine.insert();
                     
                        
                        }
                   
                    }
                   
                }
                ttscommit;
            }

    You need clear workOrderLine or you can delete workOrderLine.RecId control (it's meaningless anyway)

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 843 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 503 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 288 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans