StrataFrame Forum

Saving Child Records Under a Transaction

http://forum.strataframe.net/Topic17925.aspx

By Geoff Hirst - 7/20/2008

Guys,

It's getting late into the day on Sunday and I have been trying to wrap an area of code into a transaction. Now, first up without the transaction everything works fine.

When the transaction is in place, it all SEEMS to work fine. With the exception that it is only saving a single child record.

Now, what I am doing is creating an invoice (with lines) from a job form. Basically creating an invoice record and copying the parts used on the job, to invoice lines. No rocket science. No business rules broken. It all saves fine.

Until I go look at the invoice and there is only the very first invoice line saved. I have been through heaps of other posts and I guess I am falling foul of some IsDirty rule, but I can't see where. For the time being I have removed the transactional code. I would rather it was there though, given that it's invoice production.

Try

With Inv

.Add()

.CustomerID = Me.Job1.CustomerID

.SiteID = Me.Job1.InstallationID

.InvoiceDate = Date.Today

.OrderReference = Me.Job1.OrderReference

.InvoiceTotal = Me.Job1.TotalJobCost

.Status = 1 '

.Printed = False

.Terms = 1

.PaymentDue = Date.Today

.FreeForm = String.Empty

.Notes = "All goods remain the property of " & " until paid for in full." & ControlChars.CrLf

.InvoiceTotal = Me.Job1.TotalJobCost

' BusinessLayer.TransactionBegin("", Data.IsolationLevel.ReadCommitted)

.InvoiceNumber = GetNextInvoiceNumber(False)

' If .Save(True) = MicroFour.StrataFrame.Data.SaveUndoResult.Success Then

Try

Me.JobLine1.MoveFirst()

Do

With invline

.Add()

' .InvoiceID = Inv.ID

.Partcode = Me.JobLine1.PartCode

.Qty = Me.JobLine1.Qty

.LineTotal = Me.JobLine1.LineTotal

If AlarmInstallation1.NewBuild = True Then

.VATCode = "E" ' exempt

Else

.VATCode = 1 ' normal

Using vcode As New VATCodes

vcode.FillByPrimaryKey(.VATCode)

If vcode.CurrentRowIndex > -1 Then

vatrt += (.LineTotal * vcode.VATRate / 100)

Else

vatrt += 0 ' if the vatcode doesn't exist then we can't calculate it.

End If

End Using

End If

Select Case .Save(False)

Case MicroFour.StrataFrame.Data.SaveUndoResult.AbortedWithBrokenRules

System.Diagnostics.Debug.WriteLine("BR")

Case MicroFour.StrataFrame.Data.SaveUndoResult.Cancelled

System.Diagnostics.Debug.WriteLine("cancelled")

Case MicroFour.StrataFrame.Data.SaveUndoResult.CompletedWithExceptions

System.Diagnostics.Debug.WriteLine("CWR")

Case MicroFour.StrataFrame.Data.SaveUndoResult.FailedWithExceptions

System.Diagnostics.Debug.WriteLine("fwe")

Case MicroFour.StrataFrame.Data.SaveUndoResult.Success

System.Diagnostics.Debug.WriteLine("amazing!")

End Select

End With

Loop Until Me.JobLine1.MoveNext = False

Catch ex As Exception

Throw ex

End Try

'Else

'MessageForm.ShowMessage("Broken Rules:" & Inv.BrokenRules.ToString)

'End If

' BusinessLayer.TransactionCommit("")

' finally mark the job as invoiced so it can't be invoiced again.

With Me.Job1

.Edit()

.Invoiced = True

.Save()

End With

.Edit()

.VAT = vatrt

.Save()

End With

Me.tsbInvoiceJob.Enabled = Not Job1.Invoiced

Catch ex As Exception

MessageForm.ShowMessage("Alarm64 Job", "An unexpected issue has occurred. An email has been created and sent to Alarm64 Support" _

, "7001", MessageFunction.OK, MessagingIcon.Information, MessagingSounds.Notify)

SendemailException(ex, Me, "7001")

'BusinessLayer.TransactionRollback("")

End Try

Thanks for any ideas.

Geoff

By Trent L. Taylor - 7/21/2008

Before I dig into your code, did the child record have any broken rules, throw an exception on the save, etc? 
By Geoff Hirst - 7/21/2008

Hi Trent,

No broken rules. I looked for that before I posted the message. I sat through 7 iterations of the code to watch them all save without issue.

This is the first time I have looked at transactions with SF so definately prepared to be told I have do something wrong.

thanks

Geoff.

By Trent L. Taylor - 7/21/2008

Well, here is a quick example of how to start a transaction add and then save on a transaction:

Public Sub SaveOnTransaction()
'‐‐ Start the transaction
MicroFour.StrataFrame.Business.BusinessLayer.TransactionBegin("", "MyTranKey", _
IsolationLevel.ReadCommitted)

'‐‐ Start the try block so that we can roolback
' the transaction if an error is encountered
Try
    '‐‐ Save the business object on the transaction
    Me.BusinessObject11.Save(True, "MyTranKey")

    '‐‐ Commit the transaction
    MicroFour.StrataFrame.Business.BusinessLayer.TransactionCommit("", "MyTranKey")
Catch
    '‐‐ Rollback the transaction
    MicroFour.StrataFrame.Business.BusinessLayer.TransactionRollback("", "MyTranKey")
End Try

End Sub