Skip to end of metadata
Go to start of metadata

How does one access the work order against which a schedule is executed from a Material Lot generated from said scheduled operation?

Step-by-step guide

Use the following code 

##Given the lot name or the lot object:
obj = system.mes.loadMESObject('LotName', 'MaterialLot')
##Get the Response Segment from which the Lot was produced:
newSeg = system.mes.loadMESObject(obj.getPropertyValue('ResponseSegmentUUID'))
##If the reference is a Request Segment, then the runtime was executed against a schedule.
if newSeg.getPropertyValue('OperationsSegmentRefType') == 'RequestSegment':
	##Get the request segment object
	reqSeg = system.mes.loadMESObject(newSeg.getPropertyValue('OperationsSegmentRefUUID'))
	##Get the operations request, the parent of the request segment.
	opReq = system.mes.loadMESObject(reqSeg.getPropertyValue('OperationsRequestRefUUID'))
	##Get the work order link on the operations request
	woLink = opReq.getWorkOrderLink()
	##Ensure the work order link is not empty/blank.
	if woLink.getName() is not None:
		##Load the work order object:
		woObj = woLink.getMESObject()
		print woObj
	print 'This lot was produced outside of a schedule, and therefore apart from a work order.'
	print 'Therefore another mechanism is necessary to access WO info, such as custom properties.'

but what if I only have the Work Order # and am looking for a lot #?

because of a gap in our product at this time, we need to take a longer route..

i.e. go directly to the DB to get some objects the Response Segments linked back to the Work Order. 
Assuming you have the Work Order object, and therefore the UUID for it, it is the parameter that is passed into the runPrepQuery() function.

Then build a list of Material Lot objects to simplify, you may want to only append the name/uuid,
or consider a list with [lot name, lot sequence, uuid] of all the "out" lots for all the segments that are linked to all the requests against the work order.

query = """SELECT opreq.MESOperationsRequestUUID, opreq.Name, reqseg.MESRequestSegmentUUID, rspseg.MESResponseSegmentUUID
FROM mesoperationsrequest opreq
JOIN MESRequestSegment reqseg ON opReq.MESOperationsRequestUUID = reqseg.OperationsRequestRefUUID  
JOIN MESResponseSegment rspseg ON reqseg.MESRequestSegmentUUID = rspseg.OperationSegmentRefUUID
WHERE opreq.WorkOrderRefUUID = ? AND opreq.Enabled = 1"""

ds = system.dataset.toDataSet(system.db.runPrepQuery(query, ['WORK ORDER UUID GOES HERE'], 'MES', ''))
materialLots = []
for i in range(ds.getRowCount()):
	rspSeg = system.mes.loadMESObject(ds.getValueAt(i, 'MESResponseSegmentUUID'))
	if rspSeg.getComplexPropertyCount('ResponseMaterial') > 0:
		for j in range(rspSeg.getComplexPropertyCount('ResponseMaterial')):
			matPropName = rspSeg.getComplexPropertyItemNames('ResponseMaterial').get(j)
			matProp = rspSeg.getComplexProperty('ResponseMaterial', matPropName)
			if matProp is not None:
				if matProp.use == 'Out':

print materialLots

code, courtesy of Mark French

Material Lot

Filter for a Material Lot by Custom Property

Adding Custom Properties to a Work Order and Custom Properties Column(s) in the MES Work Order Table

Object Functions