Example 03 : Creating multiple consignments

Printer-friendly version

This will create five single parcel Consignment objects (using the createDefaultConsignment() function, implemented in the base class), with an order reference of "EXAMPLE-3-C" with the index appended. Then it will submit them to the Web Service for creation in the DM system. Finally, it will print out the unique consignment codes assigned to each of them.

If the creation in DM fails for any reason (such as invalid postcodes), the service will respond with a fault code as an exception. If any of the consignments fails with an error, the whole batch is abandoned.

package com.metapack.dm.test;
 
import com.metapack.deliverymanager.client.Consignment;
import com.metapack.deliverymanager.client.webservices.ConsignmentService;
 
public class Example3 extends ExampleBase {
 
	public void run() throws Exception {
		ConsignmentService service = createConsignmentService();
 
		/*
		 * Create the consignment objects
		 */
 
		int numberOfConsignmentsToCreate = 5;
		Consignment[] consignments = new Consignment[ numberOfConsignmentsToCreate ];
		for( int i = 0 ; i < numberOfConsignmentsToCreate ; i++ ) {
			Consignment consignment = createDefaultConsignment( "EXAMPLE-3-C" + (i+1) , 1 );
			consignments[ i ] = consignment;
		}
 
		/*
		 * Create the consignments in DM
		 */
 
		Consignment[] createdConsignments = service.createConsignments( consignments );
 
		/*
		 * Iterate through the responses to see what codes the consignments were given
		 */
 
		for( int i = 0 ; i < createdConsignments.length ; i++ ) {
			System.out.println( "Consignment " 
					+ createdConsignments[i].getOrderNumber() 
					+ " was created with code " 
					+ createdConsignments[i].getConsignmentCode() );
		}
	}
 
}