AP Job Charge Custom Code

I’m trying to automate the process of adding a Job Misc Charge through AP Invoice Entry as it’s the same information every time. The goal is to type in the job number and have it do the rest upon tabbing.
I have it successfully going out and adding the new material sequence to the job, but my issue is the final few methods in the trace in AP Invoice Entry. After creating the new material sequence on the job, it comes back and changes the material sequence for the table APInvJob.
This is where I’m running into issues. I did apBO.GetRows(insert too many parameters); and it’s returning the proper information for the APInvHed and APInvDtl tables, but it appears like the APInvJob table doesn’t exist. Am I missing a step?

Trace: TraceData12808.txt (271.9 KB)

Code:

  callContextBpmData.Character01 = "";
  int newSeq = 0;
  string grpID = "";
  string jNum = "";
  int assSeq = 0;
  int vNum = 0;
  string iNum = "";

  var ttAPInvJobR = ttAPInvJob.FirstOrDefault(r=>r.Added() || r.Updated());
  var ttAPInvHedR = ttAPInvHed.FirstOrDefault();

  if( ttAPInvHedR != null )
  {
    grpID = callContextBpmData.Character01 =(string)ttAPInvHedR.GroupID; 
  }


  if( ttAPInvJobR != null )
  {
    jNum = (string)ttAPInvJobR.JobNum;
    vNum = (int)ttAPInvJobR.VendorNum;
    assSeq = (int)ttAPInvJobR.AssemblySeq;
    iNum = (string)ttAPInvJobR.InvoiceNum;
    string PART = "FRT";
    
    using( var jobBO = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.JobEntrySvcContract>(Db))
    {
      try
      {
        JobEntryTableset jTS = new JobEntryTableset();
        
        //Get Rows Method
        string whereClauseJobHead = "JobNum = '" + jNum + "' BY JobNum";
        string whereClauseJobHeadAttch = "";
        string whereClauseJobAsmbl = "JobAsmbl.AssemblySeq = " + assSeq;
        string whereClauseJobAsmblAttch = "";
        string whereClauseJobAsmblInsp = "";
        string whereClauseJobMtl = "JobMtl.AssemblySeq = " + assSeq + " AND MtlSeq = 0";
        string whereClauseJobMtlAttch = "";
        string whereClauseJobMtlInsp = "";
        string whereClauseJobMtlRefDes = "";
        string whereClauseJobMtlRestriction = "";
        string whereClauseJobMtlRestrictSubst = "";
        string whereClauseJobOper = "";
        string whereClauseJobOperAttch = "";
        string whereClauseJobOperInsp = "";
        string whereClauseJobOperMachParam = "";
        string whereClauseJobOpDtl = "";  
        string whereClauseJobResources = "";  
        string whereClauseJobOperRestriction = "";  
        string whereClauseJobOperRestrictSubst = "";  
        string whereClauseJobAsmblRestriction = "";  
        string whereClauseJobAsmblRestrictSubst = "";  
        string whereClauseJobAsmRefDes = "";  
        string whereClauseJobAudit = "";  
        string whereClauseJobPart = "";  
        string whereClauseJobProd = "";  
        int pageSize = 0;
        int absolutePage = 0;
        bool morePages = false;
        
        jTS = jobBO.GetRows(whereClauseJobHead,
                            whereClauseJobHeadAttch,
                            whereClauseJobAsmbl,
                            whereClauseJobAsmblAttch,
                            whereClauseJobAsmblInsp,
                            whereClauseJobMtl,
                            whereClauseJobMtlAttch,
                            whereClauseJobMtlInsp,
                            whereClauseJobMtlRefDes,
                            whereClauseJobMtlRestriction,
                            whereClauseJobMtlRestrictSubst,
                            whereClauseJobOper,
                            whereClauseJobOperAttch,
                            whereClauseJobOperInsp,
                            whereClauseJobOperMachParam,
                            whereClauseJobOpDtl,
                            whereClauseJobResources,
                            whereClauseJobOperRestriction,
                            whereClauseJobOperRestrictSubst,
                            whereClauseJobAsmblRestriction,
                            whereClauseJobAsmblRestrictSubst,
                            whereClauseJobAsmRefDes,
                            whereClauseJobAudit,
                            whereClauseJobPart,
                            whereClauseJobProd,
                            pageSize,
                            absolutePage,
                            out morePages);
        
        //GetNewJobMtl Method
        string jobNum = jNum;
        int assemblySeq = (int)ttAPInvJobR.AssemblySeq;
        jobBO.GetNewJobMtl(ref jTS, jobNum, assemblySeq);
        
        newSeq = (
           from jm in Db.JobMtl.With(LockHint.NoLock)
           where jm.Company == Session.CompanyID &&
                 jm.JobNum == jNum &&
                 jm.AssemblySeq == assemblySeq
           orderby jm.MtlSeq descending
           select jm.MtlSeq).Take(1).DefaultIfEmpty(0).FirstOrDefault() + 10;
        
        //ChangeJobMtlVendorNumVendorID Method   
        string ipProposedVendorNumVendorID = (
           from v in Db.Vendor.With(LockHint.NoLock)
           where v.Company == Session.CompanyID &&
                 v.VendorNum == vNum
           select v.VendorID).DefaultIfEmpty("").FirstOrDefault();
        
        jobBO.ChangeJobMtlVendorNumVendorID(ipProposedVendorNumVendorID, ref jTS);           

        
        //ChangeJobMtlPurPoint Method
        jobBO.ChangeJobMtlPurPoint(ref jTS);
        
        
        //CheckChangeJobMtlMiscCharge Method
        bool ipMiscCharge = true;

        jobBO.CheckChangeJobMtlMiscCharge(ipMiscCharge, jTS);
        
        //ChangeJobMtlMiscCharge Method
        jTS.JobMtl[0].MiscCharge = true;
        jobBO.ChangeJobMtlMiscCharge(ref jTS);
        
        //ChangeJobMtlQtyPer Method
        jobBO.ChangeJobMtlQtyPer(ref jTS);
        
        //ChangeJobMtlEstSplitCosts Method
        jobBO.ChangeJobMtlEstSplitCosts(ref jTS);
        
        //ChangeJobMtlFixedQty Method
        jobBO.ChangeJobMtlFixedQty(ref jTS);
        
        //ChangeJobMtlPartNum Method  
        bool ipValidatePart = true;
        string ipPartNum = PART;
        Guid SysRowID = Guid.Empty;
        string xrefPartNum = "";
        string xrefPartType = "";
        string vMsgText = "";
        bool vSubAvail = false;
        string vMsgType = "";
        bool multipleMatch = false;
        bool opPartChgCompleted = false;
        string opMtlIssuedAction = "";
        
        jTS.JobMtl[0].Description = "Freight Charge";
        jobBO.ChangeJobMtlPartNum(ref jTS,
                                  ipValidatePart,
                                  ref ipPartNum,
                                  SysRowID,
                                  xrefPartNum,
                                  xrefPartType,
                                  out vMsgText,
                                  out vSubAvail,
                                  out vMsgType,
                                  out multipleMatch,
                                  out opPartChgCompleted,
                                  out opMtlIssuedAction);
        
        //Update Method
        jTS.JobMtl[0].MiscCode = "OUTF";
        jTS.JobMtl[0].BuyIt = true;
        jTS.JobMtl[0].AddedMtl = true;
        jTS.JobMtl[0].RowMod = "A";
        jobBO.Update(ref jTS);
      }
      catch(Exception e){callContextBpmData.Character01 += e.ToString();}
    }   

    using( var apBO = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.APInvoiceSvcContract>(Db))
    {
      try
      {
        APInvoiceTableset apTS = new APInvoiceTableset();
        
        //GetRows Method
        string whereClauseAPInvHed = "InvoiceNum = '" + iNum + "' AND VendorNum = " + vNum.ToString() + " BY InvoiceNum";
        string whereClauseAPInvHedAttch = "";
        string whereClauseAPIHAPInvExp = "";
        string whereClauseAPIHAPInvExpTGLC = "";
        string whereClauseAPIHAPInvMsc = "";
        string whereClauseRcvMisc = "";
        string whereClauseAPIHAPLnTax = "";  
        string whereClauseAPInvDtl = "";//"APInvDtl.InvoiceLine = 1";
        string whereClauseAPInvDtlAttch = "";
        string whereClauseAPInvDtlDEASch = "";
        string whereClauseAPInvExp = "";
        string whereClauseAPInvExpTGLC = "";
        string whereClauseGlbAPIETGLC = "";
        string whereClauseAPInvJob = "";//"APInvJob.JobNum = '" + jNum + "' AND APInvJob.AssemblySeq = " + assSeq.ToString() + " AND APInvJob.MtlSeq = " + newSeq.ToString();
        string whereClauseAPInvMsc = "";
        string whereClauseAPInvLnMscTax = "";
        string whereClauseAPInvPB = "";
        string whereClauseAPLnTax = "";  
        string whereClauseAPInvDtlTGLC = "";
        string whereClauseAPInvHedMscTax = "";
        string whereClauseAPInvSched = "";
        string whereClauseAPInvTax = "";
        string whereClausePESUNATDtl = "";
        string whereClauseEntityGLC = "";
        string whereClausePEAPInvTax = "";
        string whereClauseLegalNumGenOpts = "";
        string whereClauseSupplierXRef = "";  
        string whereClauseTaxConnectStatus = "";
        int pageSize = 0;
        int absolutePage = 0;
        bool morePages = false;  
          
        apTS = apBO.GetRows(whereClauseAPInvHed,
                            whereClauseAPInvHedAttch,
                            whereClauseAPIHAPInvExp,
                            whereClauseAPIHAPInvExpTGLC,
                            whereClauseAPIHAPInvMsc,
                            whereClauseRcvMisc,
                            whereClauseAPIHAPLnTax,
                            whereClauseAPInvDtl,
                            whereClauseAPInvDtlAttch,
                            whereClauseAPInvDtlDEASch,
                            whereClauseAPInvExp,
                            whereClauseAPInvExpTGLC,
                            whereClauseGlbAPIETGLC,
                            whereClauseAPInvJob,
                            whereClauseAPInvMsc,
                            whereClauseAPInvLnMscTax,
                            whereClauseAPInvPB,
                            whereClauseAPLnTax,
                            whereClauseAPInvDtlTGLC,
                            whereClauseAPInvHedMscTax,
                            whereClauseAPInvSched,
                            whereClauseAPInvTax,
                            whereClausePESUNATDtl,
                            whereClauseEntityGLC,
                            whereClausePEAPInvTax,
                            whereClauseLegalNumGenOpts,
                            whereClauseSupplierXRef,
                            whereClauseTaxConnectStatus,
                            pageSize,
                            absolutePage,
                            out morePages);

        
        //ChangeMtlSeq Method
        int ProposedMtlSeq = newSeq;
        
        apTS.APInvJob[0].RowMod = "A";
        apBO.ChangeMtlSeq(ProposedMtlSeq, ref apTS);
        
      }
      catch(Exception e){callContextBpmData.Character01 += e.ToString();}
    }
  }

Simple answer.
Update the temp table from the BPM.

1 Like

The last step I need is to call UpdateMaster.
Running into the same situation where I don’t have that APInvJob table in the dataset.
Is there a way to commit the updates in the ttAPInvJob table?
Db.Validate() didn’t seem to work.

First thing to do to your ds is:
ds = YourBO.GetByID(key);

Or, if you prefer to do it later, you should be able to use a tempDS and merge to your ds.

I did that, but apparently there are no rows in the APInvJob table.
I did an update without assigning anything to the DS–nothing errored out, but nothing happened, either.
Then I tried apTS.APInvJob[0].RowMod = “A” and it’s all like “Dude, index error. You’s got no rows”.
And then I said “Fine. I hate you.”

1 Like

Is this in PRE proc? Maybe move it to POST, so the record will be created?

The BPM fires in the POST processing on the APInvoice.ChangeJobNum method.

Ah, so I believe that method just updates the dataset, not the DB. You may have to move your logic

I’m not sure of a spot to put it if not that one.
It needs to fire right after the job, so that it can go off and create the new material sequence for that job, then come back and select that sequence.
I guess I could just tell the user to hit save instead of trying to do it for them.

How is Epicor doing it?
Do they have some EpiMagic going on in between the job updates and pushing it back to the invoice?

GetNewJobHead? Better yet - Update with RowMod of “A”

I tried setting the RowMod of the APInvJob table to “A” before doing an update, but that’s where it gave me an index error.

I’m saying put your logic in Update, and use a RowMod of “A” to be the condition to run it

At that point, the user would be manually clicking save regardless, right?
That’s essentially where I’m at. I was just trying to call UpdateMaster to cut that step out.