Custom Code Function - Need Review Help

Season 4 Throw In The Towel GIF by SpongeBob SquarePants

I’m at the point that I can’t figure out why I keep getting an index out of range error. Yes, the code is a mess. Any help is appreciated.

References

CallService<Erp.Contracts.SalesOrderSvcContract>(soSvc =>
{
    Erp.Tablesets.SalesOrderTableset soTS = soSvc.GetByID(soNum); if (soTS == null) {funcOutput = "soTS"; return;}
    
    CallService<Erp.Contracts.ProjectSvcContract>(projSvc =>
    {
        Erp.Tablesets.OrderHedRow ohRow = soTS.OrderHed[0]; if (ohRow == null) { this.funcOutput = "ohRow"; return;}
        
        Erp.Tablesets.OrderDtlTable odTable = soTS.OrderDtl; if (odTable == null) { this.funcOutput = "odTable"; return;}
    
        int maxOrderLine = 0;
    
        foreach (var od2Row in odTable)
        { if (od2Row == null) {this.funcOutput = "od2Row 1"; return;}
            if (od2Row.OrderLine > maxOrderLine)
            {
                maxOrderLine = od2Row.OrderLine;
            }
        }
    
        Erp.Tablesets.ProjectTableset projTS = new Erp.Tablesets.ProjectTableset(); if (projTS == null) { this.funcOutput = "projTS"; return;}
    
        projSvc.GetNewProject(ref projTS); if (projTS == null) { this.funcOutput = "projTS 2"; return;}
        
        string pID = "P" + ohRow.OrderNum.ToString();
    
        Erp.Tablesets.ProjectRow projRow = projTS.Project.FirstOrDefault(); if (projRow == null) { this.funcOutput = "projRow"; return;}
    
        projRow.ProjectID = pID;
        projRow.Description = ohRow.OrderNum.ToString();
        projRow.StartDate = ohRow.OrderDate;
        projRow.EndDate = soTS.OrderDtl[maxOrderLine].NeedByDate;
        projRow.RowMod = "A";
    
        projSvc.Update(ref projTS);
        projTS = projSvc.GetByID(pID); if (projTS == null) { this.funcOutput = "first recall"; return;}
    
        foreach (var od2Row in odTable)
        {
            if (od2Row != null)
            {
            if (od2Row.MfgJobType == "PRJ")
            {
                projSvc.GetNewProjPhase(ref projTS, pID);
            
                Erp.Tablesets.ProjPhaseRow phaseRow = projTS.ProjPhase.FirstOrDefault(p => p.RowMod == "A"); if (phaseRow == null) { this.funcOutput = "phaseRow"; return;}
            
                phaseRow.PhaseID = od2Row.OrderLine.ToString();
                phaseRow.Description = od2Row.LineDesc.Substring(30);
                phaseRow.DueDate = od2Row.NeedByDate;
            
                projSvc.Update(ref projTS);
                projTS = projSvc.GetByID(pID); if (projTS == null) {this.funcOutput = "second recall"; return;}
            
                Erp.Tablesets.OrderRelRow orRow = soTS.OrderRel.FirstOrDefault(r => r.OrderLine == od2Row.OrderLine && r.OrderRelNum == 1); if (orRow == null) {funcOutput = "orRow"; return;}
            
                orRow.PhaseID = od2Row.OrderLine.ToString();
            
                soSvc.Update(ref soTS);
                soTS = soSvc.GetByID(soNum); if (soTS == null) {funcOutput = "so first recall"; return;}
            }}{funcOutput = "error2"; return;}
    
            foreach (var phRow in projTS.ProjPhase)
            {
                if (phRow != null)
                {
                projSvc.GetNewProjectMilestone(ref projTS, pID);
        
                Erp.Tablesets.ProjectMilestoneRow mRow = projTS.ProjectMilestone.FirstOrDefault(m => m.RowMod == "A"); if (mRow == null) {funcOutput = "mRow"; return;}
        
                mRow.MilestoneID = od2Row.OrderLine.ToString();
                mRow.Description = phRow.Description;
                mRow.BillingRequired = true;
                mRow.OrderNum = od2Row.OrderNum;
                mRow.OrderLine = od2Row.OrderLine;
                mRow.BillingType = "F";
                mRow.BillingAmount = od2Row.ExtPrice;
            
                projSvc.Update(ref projTS);
                projTS = projSvc.GetByID(pID); if (projTS == null) {funcOutput = "third recall"; return;}
            
                projSvc.GetNewProjectCriteria(ref projTS, projRow.ProjectID, od2Row.OrderLine.ToString());
            
                Erp.Tablesets.ProjectCriteriaRow cRow = projTS.ProjectCriteria.FirstOrDefault(c => c.RowMod == "A"); if (cRow == null) {funcOutput = "cRow"; return;}
            
                cRow.CriteriaType = "P";
                cRow.PhaseID = phRow.PhaseID;
            
                projSvc.Update(ref projTS);
                projTS = projSvc.GetByID(pID); if (projTS == null) {funcOutput = "die"; return;}
                } {funcOutput = "error1"; return;}
            }
        }
    });
}); funcOutput = "Success";

You’re order lines are 1 indexes, but the tableset is 0 indexed.

Also, order lines do not necessarily go in numerical order. You can delete order lines, and there will be a gap in the lines, but indexing won’t have a gap.

Instead of indexing, you should look for the explicit line number in the dataset.

projRow.EndDate = soTS.OrderDtl.Where(z=>z.orderLine == maxOrderLine).FirstOrDefault().NeedByDate;
2 Likes

That was not intended to be an index, I was literally going for the OrderDtl.OrderLine. Did I not do it correctly?

1 Like

Nevermind, I got it now.

The [] is indexing into the dataset. so [0] is the first row, no matter what’s in there. [1] is the second, and so forth.

1 Like

Ha ha! That was a :man_facepalming: and I knew it. Just couldn’t find it. Thanks