This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

[SOLVED] Sum same property from different lookups in another property

Hi community!

First time posting, I'm starting to getting in touch with the API and VBScript with M-Files.
I have a vault which has 2 object types that are making me struggling : Invoices and Articles

To be short, every Article(1098) has an Amount(1038) and every Invoice can contain multiple Articles.
The invoice has a total amount value and I would like to calculate it by summing every Amount in the different Articles.

I started by looking at the different topics and I got to this :


' Declare our variables.
Dim objArtTypedValue, objArtPropertyValues,  objArtObjVer, strAmount, totAmount,

' This is the typed value Lookups collection of the Article class from the current object's properties.
Set objArtTypedValue = PropertyValues.SearchForProperty(1098).TypedValue.GetValueAsLookups()

' Is this really a collection/array? Let's try some black magic...
For Each article in objArtTypedValue

' ObjVer object creation to load the Article's properties
Set objArtObjVer = CreateObject("MFilesAPI.ObjVer")
objArtObjVer.SetIDs article.ObjectType, article.Item, article.Version


' Load the Art property values from the vault (for this ObjVer).
Set objArtPropertyValues= Vault.ObjectPropertyOperations.GetProperties(objArtObjVer, True)

' Now get the Amount.
strAmount = objArtPropertyValues.SearchForProperty(1038).TypedValue.DisplayValue

'And as "+=" doesn't work let's try it the old school way
totAmount = totAmount + strAmount

Next

' Output the total
Output =  totAmount 


But I'm not really getting it working, here is an error message :

Montant calcul?, PropertyDefCalculatedValue, 16, Non trouv?. (0x8004000B) //Calculated amount, PropertyDefCalculatedValue,16, Not found

Which refers to the following line :
Set objArtPropertyValues= Vault.ObjectPropertyOperations.GetProperties(objArtObjVer, True)

Like if those lines weren't working :
Set objArtObjVer = CreateObject("MFilesAPI.ObjVer")
objArtObjVer.SetIDs article.ObjectType, article.Item, article.Version


I tried to debug by getting the article.Item/Version/ObjectType properties by throwing an error, and looks like my foreach works at this point and the problem really looks like to be further.

Maybe someone of you has an idea on the way to do? Maybe the For Each shouldn't work like this?

Thanks in advance for your time and have a nice day

Manuel Cabras
Altern8 SA - www.evok.ch
  • Hi,

    This is bizarre, as I had this query via another medium earlier today. I assume you got the majority of that code from this post: community.m-files.com/index.php

    If so then I believe I know what the problem is. When you get the Lookup, it will contain an ObjectType, an Item, and a Version. In most situations the Version will be zero. This is because, typically, a lookup points to the latest version of the item, rather than a specific version. If you call SetIDs and pass a version of zero, though, and pass the result to GetProperties, it fails with that "Not found" error.

    I believe you probably want something like this:


    ' Declare our variables.
    Dim objArtTypedValue, objArtPropertyValues, objArtObjVer, strAmount, totAmount
    totAmount = 0

    ' This is the typed value Lookups collection of the Article class from the current object's properties.
    Set objArtTypedValue = PropertyValues.SearchForProperty(1098).TypedValue.GetValueAsLookups()

    ' Is this really a collection/array? Let's try some black magic...
    For Each article in objArtTypedValue

    ' If the ID is zero then find the latest version in the vault.
    If article.Version

    ' Create an ObjID pointing at the article.
    Dim objArticleObjID: Set objArticleObjID = CreateObject("MFilesAPI.ObjID")
    objArticleObjID.SetIDs article.ObjectType, article.Item

    ' Get the latest version.
    Set objArtObjVer = Vault.ObjectOperations.GetLatestObjVer(objArticleObjID, False, True)

    Else

    ' Otherwise: use the version directly.
    Set objArtObjVer = CreateObject("MFilesAPI.ObjVer")
    objArtObjVer.SetIDs article.ObjectType, article.Item, article.Version

    End If

    ' Load the amount property value from the vault (for this ObjVer).
    strAmount = CDbl(Vault.ObjectPropertyOperations.GetProperty(objArtObjVer, 1038).TypedValue.DisplayValue)

    'And as "+=" doesn't work let's try it the old school way
    totAmount = totAmount + strAmount

    Next

    ' Output the total
    Output = totAmount


    Does that make sense?

    Regards,

    Craig.
  • Craig,

    It makes totally sense. I didn't get this Version concept before so I totally didn't think about it. You made my day, thanks!

    Kind Regards

    Manuel
    Altern8 SA- www.evok.ch