Pseudo Attributes Criteria Search using STK
Can you run a criteria search through a database/table using pseudo attributes with Granta MI Python Scripting Toolkit 2.3 ?
For example finding all records within a given table that have been modified in the last 24 hours.
Answers
-
This is possible but the approach is slightly different than searching on Attributes. In a future version we intend to make this more discoverable.
The approach is to create a
SearchCriterion
object directly, this can then be passed totable.search_for_records_where
as normal.import granta as mpy from datetime import datetime, timedelta s = mpy.Session('mi-server', autologon=True) db = s.dbs[0] table = db.get_table('TableName') now = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") early = (datetime.now()-timedelta(hours=48)).strftime("%Y-%m-%dT%H:%M:%S") ref = mpy.PseudoAttributeDefinition('modifiedDate') val = (early, now) sc = mpy.SearchCriterion(ref, 'BETWEEN', value=val) recs = table.search_for_records_where([sc]) print(recs)
There are a few things to note here:
- Performing this search on a database will return multiple copies of each record, so I suggest you perform the search on a specific table.
- You must pass the search value as a tuple of strings, there is a known issue with passing
datetime.datetime
objects. - The docstring and type hint for
SearchCriterion
does not listPseudoAttributeDefinition
as a valid type. It is.
The documentation does not make clear exactly which Pseudoattributes can be searched on, for the moment the list is below for reference. Only the Pseudoattributes listed below the search criterion can be searched with that criterion, and only those criteria support searches on Pseudoattributes.
BETWEEN
- modifiedDate
- createdDate
CONTAINS
- lastModifier
- creator
- name
CONTAINS_ANY
- recordColor
- recordType
EQUALS
- recordColor
- recordType
- versionState
2