Delete JobOper from Function

Hello, we have BPM that triggers off the AppendDetails that deletes unused future operations. Works great. I’m trying to move this functionality to function and I’m not able to do the same thing.

Here is what I have in the BPM

using (var txScope = IceDataContext.CreateDefaultTransactionScope()) {
	foreach (var JobOperRow in (from JobOper_Row in Db.JobOper.With(LockHint.UpdLock)
								where JobOper_Row.Company == Session.CompanyID
								&& JobOper_Row.JobNum == targetJob
								&& JobOper_Row.OprSeq >= targetOpr
								select JobOper_Row).OrderByDescending(x => x.OprSeq)) {
        Db.JobOper.Delete(JobOperRow);
	}
	Db.Validate();
	txScope.Complete();
}

I can’t use this in the function because Db.JobOper.Delete doesn’t exist.
I’ve tried Db.DeleteObject(JobOperRow) but it doesn’t appear to do anything.

I’ve tried deleting the row from the dataset and updating, but while I receive no errors it also doesn’t delete anything.

using (var txScope = IceDataContext.CreateDefaultTransactionScope()) {
  this.CallService<Erp.Contracts.JobEntrySvcContract>(jSvc => {
    Erp.Tablesets.JobEntryTableset jobDataSet = new Erp.Tablesets.JobEntryTableset();
    jobDataSet = jSvc.GetByID(targetJob);
    foreach (var JobOperRow in (from row in jobDataSet.JobOper
                                where row.OpCode >= targetOpr
                                select row)) {
		// Tried setting RowMod as well
		JobOperRow.RowMod = "D";
		jobDataSet.JobOper.RemoveAll(x => x.OprSeq == JobOperRow.OprSeq);
	}
    jSvc.Update(ref jobDataSet);
  });
  txScope.Complete();
}

The function DB access is set to readwrite and the JobOper table is marked updateable. I have no problem adding operations, resources, material to the job so I know the table is updateable.

Can anyone enlighten what is the proper way to delete records from within a function?

I think I got this working. Mark the “Requires Transaction” box on the function as true, and JobOper as updateable. Code:


  foreach ( var row in this.Db.JobOper.Where( x => x.JobNum ==targetJob && x.OprSeq >= targetOpr ) ) {
    

    Db.DeleteObject(row);
    
  }

Db.SaveChanges();

I would not recommend circumventing the business object this way as the JobOper record has JobOpDtl child record(s). Deleting directly like this would cause orphans in JobOpDtl

2 Likes

Ah, good point. I’ll see if I can get it working with JobEntry.

Thank you Kevin. I tried the Db.DeleteObject(row) and it does work. I’ve seen elsewhere recommendations not to use DeleteObject, is the orphaned records the reason or are there other reasons?

I’ve made some changes to my function to call the AppendDetails method, so my original BPM is kicking in again and I don’t need to reinvent the wheel. I tested the Db.JobOper.Delete(row) from there are no orphaned JobOpDtl records with that one. If there is a better way I’m open to it but for now I should be ok.

I’ve got quite a few BPMs but this is my first foray into functions and it’s interesting to learn where the small differences are between the two.