Validating a value list entry

Hello helpful community 
Good day to you :) 

I have faced an error that i don't exactly know how to solve it, if anyone would give me a tip I would be very thankful :) 

I have here a class called "Progress notes" This class has multiple properties. the important once are [Entry Type, Late Entry days, Late Entry reason]



Now, the late entry date is getting calculated automatically using this code:

Dim oCreatedUtc : oCreatedUtc = PropertyValues.SearchForProperty(20).TypedValue.Value
Dim oEnteredUtc : oEnteredUtc = PropertyValues.SearchForProperty(1071).TypedValue.Value
Output = DateDiff("d",oEnteredUtc,oCreatedUtc)


Now, what i wanted to do as well is to make the "Entry Type" be "Late Entry" if the "Late Entry Days" is bigger than one.

So, if the user put "Addendum" I will throw an error and prevent him from saving the changes.

Thus, i tried to add this code in the "Entry Type" property as a validation:

Dim PropertyValues : Set PropertyValues = Vault.ObjectPropertyOperations.GetProperties(ObjVer)
Dim dayscount : dayscount = PropertyValues.SearchForProperty(4868).TypedValue.Value
'Dim EntryType : EntryType = PropertyValues.SearchForProperty(5510).TypedValue.Value

if dayscount > 0 and (PropertyValue.TypedValue.DisplayValue) <> "Late Entry" Then
	Err.Raise mfscriptcancel , "Entry Type must be "Late Entry Type" " & vbCrLf & "Please change the Entry Type to save the process"
end if

But i keep getting this error:



Note that this code worked when i applied it to the property "Late Entry Reason" where i put this code as validation and it works:

Dim PropertyValues : Set PropertyValues = Vault.ObjectPropertyOperations.GetProperties(ObjVer)
Dim oCreatedUtc : oCreatedUtc = PropertyValues.SearchForProperty(20).TypedValue.Value
Dim oEnteredUtc : oEnteredUtc = PropertyValues.SearchForProperty(1071).TypedValue.Value

if DateDiff("d",oEnteredUtc,oCreatedUtc) > 0 and Len(PropertyValue.TypedValue.DisplayValue) < 20 then
	Err.Raise mfscriptcancel , "You need to provide reason for late entry! " & vbCrLf & "Reason for Late Entry should have minimum of 20 characters."
end if


But why it is not working the same way in "Entry Type"? Am I missing something somewhere? Is it because Entry type is a list not a normal text?


Thank you in advance for your efforts and response :) 

Parents
  • It looks like the property validation might trigger before the calculation of automatic properties, according to the section titled "The user creates an object and immediately checks it in".

    Here is a reference to the execution order of the different types of scripts and event handlers in the vault:

    https://www.m-files.com/user-guide/2018/eng/execution_order_of_scripts.html

    However if your script works and the value of the "Late entry days" is available when your script runs then you should be able to check if there is more than 1 version. I believe in the validation tab you probably have access to the "Vault" and "ObjVer" or "ObjID" variables. You should be able to add a check to see how many versions it has like this: 


    Dim ObjectVersions : Set ObjectVersions = Vault.ObjectOperations.GetHistory(ObjVer.ObjID)
    
    'if object version =1 then it is the first version ( new item) 
    If ObjectVersions.Count = 1 Then
    
    End If

    Alternatively, if the validation script triggers before you have the value of the Last entry days, you can also try to do your script in an event handler like "BeforeCreateNewObjectFinalize"

    Although if you do that, then you should probably add some additional validations to make sure it triggers for the correct class, and that all the appropriate properties exist on the metadata card ect...

  • What benefit i would get in checking the version of the object?Thinking

    Event handlers, yes.. this could work. I just need to find the correct code that checks in the wanted class. But for some reasons, my supervisor want to avoid event handlers as much as possible, says it slows down the system.

    I thought of making it in the workflow section as a precondition.. But i wanted it to be my last solution.. since it is annoying to figure out all the entries that i need to check in order to get it correctly working.

  • I do like Dino's solution if you want to go the Extension Kit method, it seems easy and user friendly. I think it is an additional purchase from a third party though. Here is a link from the M-Files Catalog : catalog.m-files.com/.../

    Also sorry, I misunderstood your previous question. I thought you were asking how to make it trigger only on the first save. The code I put previously was checking the versions so that it can make it trigger if it is the first version only.  

    I Know you are trying to avoid an event handler, but if you do end up going with that method you can check the class like this:

    This should have little performance hit since all off the operations are quick and most of the code would only run if it is the correct class, There would be minimal impact on the rest of the objects in the vault. 

    ' PropertyValues is not available in event handlers I think, but you can get them like this:
    Dim PropertyVals : set PropertyVals = Vault.ObjectPropertyOperations.GetProperties( ObjVer, false )
    Const Class_DefID =100
    
    'get the ids of the current class and the progress notes class
    Dim ProgressNotes_Class_DefID : ProgressNotes_Class_DefID = Vault.ClassOperations.GetObjectClassIDByAlias( "Class.ProgressNotes")
    Dim CurrentClass_DefID : CurrentClass_DefID =Vault.ObjectPropertyOperations.GetProperty( ObjVer, Class_DefID ).Value.GetLookupID()
    
    ' IF THE CLASS OF THE CURRENT OBJECT BEING CHECKDED IS "Progress Notes" continue with functionality
    If (CurrentClass_DefID = ProgressNotes_Class_DefID ) Then
    	// put rest of code here
    
    End If

    Here is an additional Idea as well, you can add the validation section inside the auto calculation script for your Late Entry Days property. You can update the auto calculation script to do check if your Entry type value has a valid value after the # of days is calculated ... if it does then return the value, otherwise you can add an  Err.Raise MFScriptCancel message directly in there .

    If the late entry days property is used in other classes however, you should also still add some validations to make sure it is for the correct class and that the Entry type property exists on the metadata card 

    'Check for correct class, and if entry type is in the property values before doing validation
    If (CurrentClass_DefID = ProgressNotes_Class_DefID And PropertyValues.IndexOf(EntryType_DefID) > -1) Then
        // add validation section here 
    End If

  • Ahhh i see.. No free cheats i guess haha.

    Old school we go then Smiley

    Well.. i guess i will go for the event handler to fix this issue. I will have to convince my supervisor though.
    Thank you for the code and all the ideas.


    Really appreciate your assistance in this regards sir and for giving me some of your time Slight smile
    Thanks a lot

    Wish you a wonderful day Smiley

Reply
  • Ahhh i see.. No free cheats i guess haha.

    Old school we go then Smiley

    Well.. i guess i will go for the event handler to fix this issue. I will have to convince my supervisor though.
    Thank you for the code and all the ideas.


    Really appreciate your assistance in this regards sir and for giving me some of your time Slight smile
    Thanks a lot

    Wish you a wonderful day Smiley

Children
No Data