Update multiple tables with advanced BPM update

Here’s a butchered bit of an Advanced BPM Update in case it helps (I’ve taken out a load of stuff that isn’t relevant except to our particular requirements). As you can see, except for having two SvcContracts in one it isn’t very different from the AutoGenerated code. You just need to sift the ttResults data for the bits you need for each update, assemble each UpdateExt tableset, and do both updates.

try
{
		Erp.Contracts.PriceLstSvcContract hPriceLst = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.PriceLstSvcContract>(Db);
		Erp.Contracts.CustomerSvcContract hCustomer = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.CustomerSvcContract>(Db);

		if (hPriceLst != null)
		{
				var specials = (from row in ttResults where  ...);
				if (specials.Count() > 0)
				{
						Erp.Tablesets.UpdExtPriceLstTableset ds = new Erp.Tablesets.UpdExtPriceLstTableset();
						bool errorOccurred;
						var pl = new Erp.Tablesets.PriceLstRow()
						{
								Company = Session.CompanyID,
								ListCode = spListCode,
								ListDescription = listDesc,
								StartDate = startdate,
								EndDate = enddate,
								...
								RowMod = "U"
						};
						... UD fields
						ds.PriceLst.Add(pl);

						foreach (var special in specials)
						{
								var plp = new Erp.Tablesets.PriceLstPartsRow()
								{
										Company = Session.CompanyID,
										ListCode = spListCode,
										PartNum = special.PartNum,
										UOMCode = special.UOM
								};
								if (special.Price == null || special.Price == 0)
								{
										plp.RowMod = "D";
								}
								else
								{
										plp.BasePrice = special.Price;
										plp.RowMod = "U";
								}
								... UD fields
								ds.PriceLstParts.Add(plp);
						}
				
						BOUpdErrorTableset boUpdateErrors = hPriceLst.UpdateExt(ref ds, true, true, out errorOccurred);

						if (errorOccurred && boUpdateErrors != null && boUpdateErrors.BOUpdError != null)
						{
								this.PublishInfoMessage(boUpdateErrors.BOUpdError[0]["ErrorText"].ToString(),Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
								this.PublishInfoMessage(...,Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
						}
				}
		}
	
		if (hCustomer != null)
		{
				int nextSeqNum = 1;

				if ((from row in Db.CustomerPriceLst where row.Company == Session.CompanyID && row.CustNum == iCustNum && row.ShipToNum == string.Empty select row).FirstOrDefault() != null)
				{
						nextSeqNum = ((int?)(from row in Db.CustomerPriceLst where row.Company == Session.CompanyID && row.CustNum == iCustNum && row.ShipToNum == string.Empty select row.SeqNum).Max() ?? 0) + 1;
				}

				Erp.Tablesets.UpdExtCustomerTableset ds = new Erp.Tablesets.UpdExtCustomerTableset();
				bool errorOccurred;
				var cust = new Erp.Tablesets.CustomerRow()
				{
						Company = Session.CompanyID,
						CustNum = iCustNum,
						RowMod = "U"
				};
				ds.Customer.Add(cust);

				var newPL = (from row in ttResults ...);
				
				foreach (var pl in newPL)
				{
						string listCode = pl.Calculated_NewListCode;
						var plrow = new Erp.Tablesets.CustomerPriceLstRow()
						{
								Company = Session.CompanyID,
								CustNum = iCustNum,
								ShipToNum = string.Empty,
								ListCode = listCode,
								SeqNum = nextSeqNum,
								RowMod = "A"
						};
						ds.CustomerPriceLst.Add(plrow);
						nextSeqNum++;
				}
	
				BOUpdErrorTableset boUpdateErrors = hCustomer.UpdateExt(ref ds, true, true, out errorOccurred);

				if (errorOccurred && boUpdateErrors != null && boUpdateErrors.BOUpdError != null)
				{
						this.PublishInfoMessage(boUpdateErrors.BOUpdError[0]["ErrorText"].ToString() + System.Environment.NewLine + p + System.Environment.NewLine + msg,Ice.Common.BusinessObjectMessageType.Error, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
						//this.PublishInfoMessage(...,Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
				}
		}

		hCustomer.Dispose();
		hCustomer = null;
		hPriceLst.Dispose();
		hPriceLst = null;
}
catch (Exception e)
{
		this.PublishInfoMessage(... + e.Message,Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
}
2 Likes