COMPILE SUBROUTINE Create_Note( SendTo, SentFrom, Subject, Message, AttachWindow, AttachKeys, SendToGroupName )

DECLARE FUNCTION Next_Key, Msg, FieldCount
DECLARE SUBROUTINE Record_Lock, obj_Notes_Sent

$INSERT LOGICAL
$INSERT NOTES_EQU
$INSERT NOTE_PTRS_EQU

StCnt = FieldCount( SendTo, @VM )
HoldSendTo = SendTo
NewSendTo  = ''

* LET'S STRIP OFF THE DUPLICATE NAMES

FOR I = 1 TO StCnt
   ThisSendTo = SendTo<1,I>
   LOCATE ThisSendTo IN NewSendTo USING @VM SETTING DPos ELSE
      NewSendTo<1,-1> = ThisSendTo
   END
NEXT I

TRANSFER NewSendTo TO SendTo  
*
IF ASSIGNED( SendToGroupName) ELSE
	SendToGroupName = ''
	* the only intent of the group parameter is for traceability for the sender
	* group names will be hidden on the read if you are not the owner
	* any system generated message will not pass this parameter
	* notes and notes_attach will do its own stripping of dups
END

OPEN 'NOTES' TO NotesTable ELSE
    Void = msg( '', 'Unable to open NOTES...' )
    RETURN
END
OPEN 'NOTE_PTRS' TO NotePtrTable ELSE
	Void = Msg( '', 'Unable to open NOTES_PTRS...' )
	RETURN
END

NextNoteKey = next_key( 'NOTES', NotesTable, 'NEXT', '' )

NoteRec                            = ''
NoteRec<notes_message_type$>       = 'I'
NoteRec<notes_entry_date$>         = date()
NoteRec<notes_entry_time$>         = time()
NoteRec<notes_send_to$>            = SendTo
NoteRec<notes_from$>               = SentFrom
NoteRec<notes_message$>            = Message
NoteRec<notes_entry_id$>           = @user4
NoteRec<notes_subject$>            = Subject
NoteRec<notes_attach_window$>      = AttachWindow
NoteRec<notes_attach_keys$>        = AttachKeys
NoteRec<notes_msg_groups_ids$>     = SendToGroupName

WRITE NoteRec ON NotesTable, NextNoteKey ELSE
	void = Msg( '', 'Unable to write ':NextNoteKey:' on NOTES...' )
	RETURN
END

UNLOCK NotesTable, NextNoteKey ELSE
	Void = msg( '', 'Unable to unlock ':NextNoteKey:' on NOTES...' )
	RETURN
END

Void = Next_Key( 'NOTES', NotesTable, 'UPDATE', NextNoteKey )

Attachment = 'No'

IF AttachWindow AND AttachKeys THEN Attachment = 'Yes'

FOR I = 1 TO StCnt
	ThisSendTo = SendTo<1,I>
	IF LEN(ThisSendTo) THEN
	
		Recipient	= ThisSendTo
		NoteID		= NextNoteKey
		CurrDTM		= OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTHS')
      
      	obj_Notes_Sent('Create',Recipient:@RM:NoteID:@RM:CurrDTM)
	
	END
NEXT I 


