Update multiple static links for a record as a sequence
Hi,
I am running a script of Granta MI STK 3.1 with Granta MI 2023 R1 where I create records in a table, then update different static links to other records in other tables. It seems that I can't update more than one link per run of the "record.set_links" command, is there a limitation there ?
Many Thanks,
Mickaël
Best Answer
-
Hi Mickael!
This is possible, the trick is that you can call the
set_links
method multiple times for the same record. See the sample code below:from GRANTA_MIScriptingToolkit import granta as mpy mi = mpy.Session("localhost", autologon=True) # Define some useful variables db = mi.get_db(db_key="MI_Training") source_table = db.get_table(name="Tensile Test Data") target_table_pedigree = db.get_table(name="Metals Pedigree") target_table_statistical = db.get_table(name="Tensile Statistical Data") # Create a source record, and two records each in two destination tables source_record = source_table.create_record(name="Source record") target_record_pedigree_1 = target_table_pedigree.create_record(name="Target record 1A") target_record_pedigree_2 = target_table_pedigree.create_record(name="Target record 2A") target_record_statistical_1 = target_table_statistical.create_record(name="Target record 1B") target_record_statistical_2 = target_table_statistical.create_record(name="Target record 2B") records_to_create = [ source_record, target_record_pedigree_1, target_record_pedigree_2, target_record_statistical_1, target_record_statistical_2, ] # Push the records to MI added_records = mi.update(records=records_to_create) pedigree_records = set() statistical_records = set() # Separate the created records into different variables based on the table name for record in added_records: if record.table_name == "Tensile Test Data": source_record = record elif record.table_name == "Metals Pedigree": pedigree_records.add(record) elif record.table_name == "Tensile Statistical Data": statistical_records.add(record) # Add the pedigree records to the pedigree link source_record.set_links("Metals Pedigree", pedigree_records) # Add the statistical records to the statistical link source_record.set_links("Tensile Statistical Data", statistical_records) # Push the links to MI mi.update_links(records=[source_record])
This will result in a record in the 'Tensile Test Data' table that is linked to two records in the 'Metals Pedigree' table, and two records in the 'Tensile Statistical Data' table.
The actual linking is in the last 3 lines of code, everything else is just setting up the records needed for the example. As I said above, the 'trick' is that you can call
set_links
multiple times on the same record to add records to different link groups, before pushing the changes to Granta MI.6