Sunday, September 09, 2007

SaveHeader Function: an upgrade

SaveHeader Function

Upgrade Suggested for your dbgrid's saveheader call to allow entry in them:

// DAW coded save_header function.

Function Save_Header Returns Integer
Boolean bHasRec bChanged
Handle hoSrvr

Get Server to hoSrvr // The Header DDO.
Get HasRecord of hoSrvr to bHasRec // Do we have a record?
Get Should_Save to bChanged // Are there any current changes?

// If there is no record and no changes we have an error.
If ( not(bHasRec) and not(bChanged) ) Begin // no rec
Error DfErr_Operator 'You must First Create & Save Main Order Header'
Function_Return 1
End

// Attempt to Save the current Record
// request_save_no_clear does a save without clearing.
Send Request_Save_No_Clear

// The save succeeded if there are now no changes, and we
// have a saved record. Should_save tells us if we've got changes.
// We must check the data-sets hasRecord property to see if
// we have a record. If it is not, we had no save.
Get Should_Save to bChanged // is a save still needed
Get HasRecord of hoSrvr to bHasRec // current record of the DD
// if no record or changes still exist, return an error code of 1
If ( not(bHasRec) or (bChanged)) ;
Function_Return 1
End_Function // Save_Header

// Suggested replacement for same:

Function SaveHeader Returns Boolean
Send Request_Save_No_Clear
If (Should_Save(Server(Self))) Function_Return True
If (IsNullRowID(CurrentRowID(Server(Self)))) Begin
Error DFErr_Operator "Please First Enter Info in the Order Header"
End
If (Err) Function_Return True
Function_Return False
End_Function
// Notes:
// Save Requests never save blank records so it's safe to call at any time
// We don't give an error when changes exist because the errors already have run
// We only give an error if no info was entered at all in the header
// Ref: Garret Mott's whitepaper "Software as a conversation" we say please.
// This function returns a true value for ANY error not just the one that we send.
// We always return a value in a function whether implied or not.

The second function is both less verbose and superior in both coding technique and performance to the code many software engineers have copied intact from the sample examples.

In addition, it can be dropped almost intact into a dbview subclass!

Note: Garret Mott's "Software as a conversation" can be found in the whitepaper section on http://www.nedataflex.com/ and the author can be reached at http://www.automatesoftware.com/

The author, Peter Donovan, can be found at http://www.applausesoftware.com/

No comments: