BPM Custom Code to save Tracking number from Master Pack to Packs not working (No compile errors)

Hi,

Something needs to be wrong but I cannot see what. Please help.


Erp.Tables.ShipHead ShipHead;
Erp.Tables.MasterPack MasterPack;
Erp.Tables.MasterPackDtl MasterPackDtl;

var _ttMasterPack = (from P in ttMasterPack where (P.Added()  ||  P.Updated()) select P);
if(_ttMasterPack != null)
{
	foreach(var ttMasterPack_Row in _ttMasterPack)
		{
			if(ttMasterPack_Row.TrackingNumber != string.Empty)
				{
					string trackingNumber = ttMasterPack_Row.TrackingNumber; 
					PublishInfoMessage(trackingNumber,Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"",""); //this display the tracking number and nothing else is happening SIC! No other pop-ups.
					var _DtlPackNumbers = (from P in ttMasterPackDtl where P.PackNum == ttMasterPack_Row.PackNum select P);
					if(_DtlPackNumbers != null)
					{
						foreach(var DtlPackNumRow in _DtlPackNumbers)
						{
							PublishInfoMessage("DtlPackNum",Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
							using (var txScope = IceContext.CreateDefaultTransactionScope())
							{
							var ShipHeadRow = (from P in Db.ShipHead where P.PackNum == DtlPackNumRow.DtlPackNum select P).FirstOrDefault();
							ShipHeadRow.TrackingNumber = trackingNumber;
							PublishInfoMessage("Saved to database",Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
							Db.Validate();
							txScope.Complete();
							}
						}
					}
					else
					{
						PublishInfoMessage("NoPacks in dataset",Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
						var _DtlPackNumbersDB = (from P in Db.MasterPackDtl where P.PackNum == ttMasterPack_Row.PackNum select P);
						if(_DtlPackNumbersDB != null)
						{
							foreach(var DtlPackNumRow in _DtlPackNumbersDB)
							{
								PublishInfoMessage("DtlPackNum from DB",Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
								using (var txScope = IceContext.CreateDefaultTransactionScope())
								{
								var ShipHeadRow = (from P in Db.ShipHead where P.PackNum == DtlPackNumRow.DtlPackNum select P).FirstOrDefault();
								ShipHeadRow.TrackingNumber = trackingNumber;
								PublishInfoMessage("Saved to database",Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
								Db.Validate();
								txScope.Complete();
								}
							}
						}
						else{ this.PublishInfoMessage("NoPacks in database",Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"",""); }
					}
				}
		}
}

Thank you
Regards
Aleksander

Note that you may want to send this type of request with code formatting in the future. :slight_smile:
Here is what I would write (fixed):

using (var txScope = IceContext.CreateDefaultTransactionScope())
{
       foreach(var tt in ttMasterPack.Where(tt => tt.Updated() && tt.TrackingNumber != ""))
	{
		foreach (var q in (from D in Db.MasterPackDtl.Where(D => D.Company == tt.Company && D.PackNum == tt.PackNum)
							join S in Db.ShipHead on
								new {D.Company, D.DtlPackNum} equals
								new {S.Company, DtlPackNum = S.PackNum}
							where S.TrackingNumber != tt.TrackingNumber
							select S))
		{				
			q.TrackingNumber = tt.TrackingNumber;
		}
	}
	Db.Validate();
	txScope.Complete();
}
1 Like

Gotcha! :innocent:

Hi Jason,

Thank you for your help and example of a very clean piece of code :). A very good example for me for future reference.

Just if anybody else needs to use this code it has one bit missing and one unnecessary dot :).

foreach(var tt in ttMasterPack.Where(tt => tt.Updated() && tt.TrackingNumber != ""))

Thank you again. Works like charm.

Regards
Aleksander

Fixed :slight_smile: