How to get the Job ID in a KPL macro throughout the product lifetime

Sometimes we need to get the Order ID for use in our macros. This can be for any number of reasons, but what’s important to know is that you need to grab the job id in different ways depending on where a product item is during its life-cycle.

When a product is sitting in the items window in a job, it is in the product item stage. Once released it will sit on the queue until printed, this is the queued stage. The final stage is printing.

Depending on what stage the product is, the order id will need to be retrieved from different database tables or using KPL provided functions. So here is the Code:

STR GetJobID()
{
   // Connect to Database
   if ( !ADO( dBase,Connect ) ) { return ~1~; } 

   //Order ID when sitting on the order items window
   Query = ~Select ORDERID From OrderItems Where JobPath = '<$Job.Path>'~;

   // If Order Items window Fails try something else
   if ( !dBase( GetValuesFor,~~,OrderID ) ) 
   {
      if ( Printing() ) // If printing
      { 
         // This is only available during printing
         OrderID = GetNodeValue( Output,~OrderID:~ ); 
      }
      else
      {
         // When sitting on the printer queue
         Query = ~Select OrderID From JobQueue Where JobPath = '<$Job.Path>'~;
         if (!dBase( GetValuesFor,~~,OrderID ) )
         {
            OrderID = ~~; // Could not get an order id
         }
      }
   }
   return ~<OrderID >~;
}

Leave a Reply

Your email address will not be published. Required fields are marked *