Hallo zusammen,
ich weiß leider nicht mehr weiter. Habe einige Sachen hier zu der Methode durchprobiert, aber leider brachte weder ein "view.refresh", ein "True" um den richtigen Treffer zu erzwingen, noch definitiv einen String als GetDocumentByKey Variable zu verwenden oder Shift + F9 ...
Der Hintergrund:
Ich habe ein Dokument pro Ausbilder in einer Urlaubsdatenbank. In diesem Dokument werden dem Ausbilder bestimmte Gruppen zugeordnet (Feld mit Checkboxen). Ich möchte mit dem QuerySave nun prüfen, ob eine der ausgewählten Gruppen schon eine Zuordnung hat. Dazu gibt es die Ansicht "_Profile_Ausbildung_byGroups", die 3 Spalten enthält:
Berufsgruppe (Standard, Sortiert, aufsteigend)
AusbilderIn (Standard, nicht sortiert)
Profil erstellt (Standard, nicht sortiert)
Querysave:
Sub Querysave(Source As Notesuidocument, Continue As Variant)
' Checks the view "_Profile_Ausbildung_byGroups", if for one of the selected groups there
' is already a trainer assigned
Dim session As New NotesSession
Dim db As NotesDatabase
Dim lookup_view As NotesView
Dim view_doc_ausbilder As NotesDocument
Dim doc_value_ausbilder As String
Dim view_value_ausbilder As String
Dim str_doc_value_gruppe As String
Set db = session.CurrentDatabase
Set lookup_view = db.GetView("_Profile_Ausbildung_byGroups")
Set doc = source.Document
doc_value_ausbilder = Source.FieldGetText("ausbilder")
doc_value_gruppen = doc.GetItemValue("gruppen")
If doc_value_gruppen(0) <> "" Then ' no group is assigned in the document therefore we do not need to check anything
Forall doc_value_gruppe In doc_value_gruppen
' to ensure, that we use the GetDocumentByKey Function with a string
str_doc_value_gruppe = Cstr(doc_value_gruppe)
' Get view with name of the group currently handled
Set view_doc_ausbilder = lookup_view.GetDocumentByKey(str_doc_value_gruppe,True)
If Not view_doc_ausbilder Is Nothing Then
view_value_ausbilder = view_doc_ausbilder.ausbilder(0)
If (view_value_ausbilder <> "") And (view_value_ausbilder <> doc_value_ausbilder) Then
continue = False
Messagebox "Leider kann eine Gruppe ... das Dokument wurde nicht gespeichert."
End If
Else
' view couldn't be located; means either the view does not exist or the viewdocument cannot be found
' (happend during development, if only one group is assigned and the document is saved the first time)
Msgbox "view_doc_ausbilder couldn't be located"
End If
End Forall
End If
End Sub
Das Fehlerbild:
Wenn ich das Dokument abspeichere ohne eine einzige Gruppe ausgewählt zu haben dann läuft alles super durch und er checkt auch, dass keine Gruppe ausgewählt ist. Sobald ich nun eine einzige Gruppe auswähle und speichere wird die Variable 'view_doc_ausbilder' nicht gefüllt und meine jetzt eingebaute Fehlermeldung erscheint. Sobald ich unmittelbar danach nochmal abspeicher, wird die Variable ordnungsgemäß gefunden.
Ich wäre Euch für jeden heißen Tipp dankbar, da ich echt nicht mehr weiß, woran das noch liegen kann.
Danke und schönen Gruß
Sascha
Hallo,
danke Dir erstmal für die Antwort. Zu den Punkten:
1) Da stimme ich Dir zu; mein Variablennaming ist ziemlich unstrukturiert; ich stolper manchmal selber darüber. Aktuell ist das nur eine logische Benamung. Ich kann aber verstehen, wenn das Probleme bereitet. Bin gerne für Vorschläge zu haben; ich habe mal gelesen, dass jeder eine andere Ansicht dazu hat (was ein einheitliches Naming angeht), aber konkret etwas gesehen habe ich noch nicht.
2. Ok, ja; das habe ich geändert. Du hast vollkommen recht. War ein Denkfehler; wahrscheinlich weil zu spät. Im Grunde war ich mir darüber aber im Klaren.
3. Option declare ist nun drin und wird Pflichtprogramm :-)
Allerdings lag es nicht daran; doc_value_gruppe wird in der Forall Schleife gefüllt. Ich nehme also aus der Gesamtheit der Gruppeneinträge doc_value_gruppen jeweils eine doc_value_gruppe heraus ...
4. Wenn ein genereller Fehler vorliegen würde, dann dürfte es ja garnicht funktionieren; meiner Ansicht nach. Aber sobald ich ein zweites Mal speicher funktioniert es einwandfrei ... das ist ja die Krux. Ich dachte zuerst es wäre ein Refresh Problem, aber die View wird ja zu dem Zeitpunkt garnicht geändert .... und einen Refresh hatte ich ja auch schon drin ...
Hier noch der declarte Code:
Sub Querysave(Source As Notesuidocument, Continue As Variant)
' Checks the view "_Profile_Ausbildung_byGroups", if for one of the selected groups there
' is already a trainer assigned
Dim session As New NotesSession
Dim db As NotesDatabase
Dim lookup_view As NotesView
Dim doc As NotesDocument
Dim view_doc_ausbilder As NotesDocument
Dim doc_value_ausbilder As String
Dim view_value_ausbilder As String
Dim str_doc_value_gruppe As String
Dim doc_value_gruppen As Variant
Set db = session.CurrentDatabase
Set lookup_view = db.GetView("_Profile_Ausbildung_byGroups")
Set doc = source.Document
doc_value_ausbilder = Source.FieldGetText("ausbilder")
doc_value_gruppen = doc.GetItemValue("gruppen")
If doc_value_gruppen(0) <> "" Then ' no group is assigned in the document therefore we do not need to check anything
Forall doc_value_gruppe In doc_value_gruppen
' to ensure, that we use the GetDocumentByKey Function with a string str_doc_value_gruppe = Cstr(doc_value_gruppe)
' Get document with name of the group currently handled
Set view_doc_ausbilder = lookup_view.GetDocumentByKey(str_doc_value_gruppe,True)
If Not view_doc_ausbilder Is Nothing Then
view_value_ausbilder = view_doc_ausbilder.ausbilder(0)
If (view_value_ausbilder <> "") And (view_value_ausbilder <> doc_value_ausbilder) Then
continue = False
Messagebox "Leider kann eine Gruppe nicht zwei Ausbildern gleichzeitig zugeordnet sein ... das Dokument wurde nicht gespeichert."
End If
Else
' document couldn't be located; means either the document does not exist or the document cannot be found
' (happend during development, if only one group is assigned and the document is saved the first time)
Msgbox "no document with name " + str_doc_value_gruppe + " located"
End If
End Forall
End If
End Sub
Wenn ich es richtig entziffere, wird dem Argument str_doc_value_gruppe für die GetdocumentByKey Methode kein Wert mitgegeben.
Sollte es vielleicht sowas werden?
Forall X In doc_value_gruppen
Set view_doc_ausbilder = lookup_view.GetDocumentByKey(X ,True)
If Not view_doc_ausbilder Is Nothing Then
view_value_ausbilder = view_doc_ausbilder.ausbilder(0)
If (view_value_ausbilder <> "") And (view_value_ausbilder <> doc_value_ausbilder) Then
continue = False
Messagebox "Leider kann eine Gruppe nicht zwei Ausbildern gleichzeitig zugeordnet sein ... das Dokument wurde nicht gespeichert."
End If
Else
Msgbox "no document with name " + X + " located"
End If
End Forall
Hallo zusammen,
sorry, wenn das so vermeintlich schlecht zu lesender Code ist ... ich versuche es etwas besser zu machen, aber wie im Post vorher schon angedeutet, bin ich da bisher unbedarft und für Tipps offen, wenn jmd. so ein Manual bzw. eine Vorlage hat.
Naja, hier mein Versuch:
Sub Querysave(Source As Notesuidocument, Continue As Variant)
' Checks the view "_Profile_Ausbildung_byGroups", if for one of the selected groups there
' is already a trainer assigned
'
' Declare variables
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view_Gruppenverantwortliche As NotesView
Dim doc_currentDocument As NotesDocument
Dim doc_Gruppenverantwortlicher As NotesDocument
Dim str_docfld_ausbilder As String
Dim str_viewdocfld_ausbilder As String
Dim str_gruppe As String
Dim docitem_gruppen As Variant
' Define variables
Set db = session.CurrentDatabase
Set view_Gruppenverantwortliche = db.GetView("_Profile_Ausbildung_byGroups")
Set doc_currentDocument = source.Document
' Get documentvalues
str_docfld_ausbilder = Source.FieldGetText("ausbilder")
docitem_gruppen = doc_currentDocument.GetItemValue("gruppen")
' Action
If docitem_gruppen(0) <> "" Then ' no group is assigned in the document therefore we do not need to check anything
' every group will be checked, if there is any document already existing in the view "view_Gruppenverantwortliche"
Forall arrayvalue_gruppe In docitem_gruppen
' to ensure, that we use the GetDocumentByKey Function with a string
str_gruppe = Cstr(arrayvalue_gruppe)
' Get document with name of the group currently handled
Set doc_Gruppenverantwortlicher = view_Gruppenverantwortliche.GetDocumentByKey(str_gruppe,True)
If Not doc_Gruppenverantwortlicher Is Nothing Then
' Get name of Ausbilder from found document
str_viewdocfld_ausbilder = doc_Gruppenverantwortlicher.ausbilder(0)
' If a Ausbilder is found and this name is not the same as the current profile name then cancel save and provide error message
If (str_viewdocfld_ausbilder <> "") And (str_viewdocfld_ausbilder <> str_docfld_ausbilder) Then
continue = False
Messagebox "Leider kann eine Gruppe nicht zwei Ausbildern gleichzeitig zugeordnet sein ... wurde nicht gespeichert."
End If ' End > If (str_viewdocfld_ausbilder <> "") And (str_viewdocfld_ausbilder <> str_docfld_value_ausbilder)
Else
' document couldn't be located; means either the document does not exist or the document cannot be found
' (happend during development, if only one group is assigned and the document is saved the first time)
Msgbox "no document with name " + str_gruppe + " located"
End If ' End > If Not doc_Gruppenverantwortlicher Is Nothing
End Forall ' End > Forall arrayvalue_gruppe In docitem_value_gruppen
End If ' End > If docitem_value_gruppen(0) <> ""
End Sub
@gab: Ich fülle die Variable an dieser Stelle, um sicher zu gehen, dass es ein String ist; hatte ich vorher auch nicht drin, aber ich bin halt schon einige Posts durch gegangen ...
' to ensure, that we use the GetDocumentByKey Function with a string
str_gruppe = Cstr(arrayvalue_gruppe)
@Bernhard: Ja, auch hier sorry für die Verwirrung! Das ist natürlich ein Feldinhalt; also ein Wert, der in dem Feld "Gruppen" im Dokument ausgewählt sein kann.
Ich habe nun noch einen Screenshot von der View "_Profile_Ausbildung_byGroups.gif" und einen Screenshot vom Dokument "Profil_Ausbilder.gif" angehangen; vielleicht trägt das noch etwas mehr zum Verständnis bei.
Danke Euch für Eure Mühen!!!
Da gibt es zwei Fehler:
1.) Du vergleichst mit :FieldGetText einen 'abbreviate' Name aus dem Frontend mit einem
canonical Name in der Ansicht, d.h. du solltest den Ausbilder übers 'Backend' holen:
str_docfld_ausbilder = Source.Document.GetFirstItem("Ausbilder").Values(0)
2.) Unabhängig von deinem Fehler musst du noch ausschliessen, dass das Dokument "sich selber" findet, so dass der Code
so aussieht:
If (str_viewdocfld_ausbilder <> "") And (str_viewdocfld_ausbilder <> str_docfld_ausbilder) Then
If doc_currentDocument.UniversalID <> doc_Gruppenverantwortlicher.UniversalID Then
continue = False
Messagebox "Leider kann eine Gruppe nicht zwei Ausbildern gleichzeitig zugeordnet sein ... das Dokument wurde nicht gespeichert."
End If
End If ' End > If (str_viewdocfld_ausbilder <> "") And (str_viewdocfld_ausbilder <> str_docfld_value_ausbilder)
HTH
Gabor
EDIT: wg. den Coding Richtlinienen für LScript muss ich mal suchen...