Had my code working, creating POs for any jobs cut using Order Job Wizard but realized I need to put in a validation to make sure we’re not creating POs more than once. My code fails with an index out of bounds message, and I have no idea where it’s coming from. Hoping a second set of eyes can spot something obvious or might even offer a better solution, I’m a noob with C#. What I’m looking to do, enumerate through jobs, find related POs, if a PO is found, stop, if no PO exists, create one and add lines.
Erp.Contracts.POSvcContract hPOApproveHandle = null;
if(hPOApproveHandle == null)
{
hPOApproveHandle = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.POSvcContract>(Db);
}
foreach (var ttJWOrderRel_Row in (from row in ttJWOrderRel select row))
{
var JobProd = (from jp in Db.JobProd
where jp.Company == Session.CompanyID
&& jp.OrderNum == ttJWOrderRel_Row.OrderNum
&& jp.OrderLine == ttJWOrderRel_Row.OrderLine
&& jp.OrderRelNum == ttJWOrderRel_Row.OrderRelNum
select new {jp.Company, jp.OrderNum, jp.OrderLine, jp.OrderRelNum, jp.JobNum}).ToArray();
if (JobProd != null)
{
foreach(var prod in JobProd)
{
string JobNum = prod.JobNum;
var JobOper = (from jo in Db.JobOper
where jo.Company == Session.CompanyID
&& jo.JobNum == JobNum
&& jo.SubContract == true
&& jo.VendorNum == 497
select new {jo.JobNum, jo.AssemblySeq, jo.OprSeq, jo.PartNum}).ToArray();
if (JobOper != null)
{
foreach(var oper in JobOper)
{
string Job = oper.JobNum;
int Asm = oper.AssemblySeq;
int Opr = oper.OprSeq;
var PORel = (from por in Db.PORel
where por.Company == Session.CompanyID
&& por.JobNum == Job
select new {por.PONum}).ToArray();
if (PORel != null)
{
foreach(var PO in PORel)
{
POnumber = PO.PONum;
this.PublishInfoMessage("PO already exists!" + "\n" + "\nPO: " + POnumber, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "", "");
}
if (PORel == null)
{
var POtable = new Erp.Tablesets.POTableset();
hPOApproveHandle.GetNewPOHeader(ref POtable);
var poHeader = POtable.POHeader[0];
poHeader.OrderDate = DateTime.Today;
hPOApproveHandle.ChangeVendor("REACT1", ref POtable);
hPOApproveHandle.Update(ref POtable);
intPONum = poHeader.PONum;
foreach(var op in JobOper)
{
hPOApproveHandle.GetNewPODetail(ref POtable, intPONum);
hPOApproveHandle.ChangeDetailTranType("PUR-SUB", ref POtable);
hPOApproveHandle.ChangeDetailJobNum(Job, ref POtable);
hPOApproveHandle.ChangeDetailAssemblySeq(Asm, ref POtable);
hPOApproveHandle.ChangeDetailJobSeq(Opr, ref POtable);
hPOApproveHandle.CheckBeforeUpdate(out str2, out str3, out str4, "PODetail", POtable);
hPOApproveHandle.Update(ref POtable);
}
}
}
}
}
}
}
if(POtable != null)
{
POtable.POHeader[0].RowMod= "U";
hPOApproveHandle.ValidateAcctForGLControl(intPONum, out str1);
hPOApproveHandle.ChangeApproveSwitch(true,out ViolationMsg, ref POtable);
hPOApproveHandle.CheckBeforeUpdate(out str2, out str3, out str4, "POHeader", POtable);
hPOApproveHandle.Update(ref POtable);
this.PublishInfoMessage("PO: " + intPONum, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "", "");
}
}