How to remove all linked records from a link group?

Daniel Manocchio
Daniel Manocchio Member Posts: 24
10 Comments First Anniversary 5 Likes Photogenic
**

Hi Guys,

I'm struggling with clearing a static record link group from all records. For clearing attributes, there is the method record.clear_attributes([names]), but I didn't find something similar for static link groups. I've already tried
record.links['rlg_name'].clear()
record.set_links(link_name='rlg_name', records=[])
mi_update_links([record])
but this didn't change anything on the server.

Any hints how to get rid of all records in a given record link group?

Thanks in advance & bests,
Daniel

Best Answer

  • Andy_Grigg
    Andy_Grigg Member, Employee Posts: 27
    5 Answers 10 Comments Name Dropper First Anniversary
    ✭✭✭✭
    Answer ✓

    Hi Daniel,

    I think you've found a bug in how we handle link group changes. The code you've written is essentially correct, it should be:

    record.set_links(link_name='rlg_name', records=[])
    mi.update([record], update_links=True)
    

    But this only works if at least one link is added to the link group. I'll double-check with my colleagues tomorrow if there's an intended alternative way of doing this, but to me it looks like a bug.

    As a workaround, you can use the foundation layer to do the link deletion. Something like this should work:

    gdl_session = mi.connect()
    record_reference = gdl.RecordReference(
        DBKey="db_key",
        historyGUID="history guid",  # Or another identifier
    )
    unlink_records = gdl.UnlinkAllRecords(sourceRecords=[record_reference])
    modifications = gdl.RecordLinkModifications(unlinkAllRecords=[unlink_records])
    link_reference = gdl.RecordLinkGroupReference(
        DBKey="db_key",
        name="Link group name",
        partialTableReference=gdl.PartialTableReference(tableName="Table name"),
        recordLinkGroupType=gdl.GRANTA_Constants.RecordLinkGroupTypes.Static,  # Or CrossDatabase
    )
    unlink_request = gdl.ModifyRecordLinksRequest(
        recordLinkModifications=modifications,
        recordLinkGroupReference=link_reference,
    )
    response = gdl_session.dataImportService.ModifyRecordLinks(unlink_request)
    

Answers

  • Daniel Manocchio
    Daniel Manocchio Member Posts: 24
    10 Comments First Anniversary 5 Likes Photogenic
    **

    Hi Andy,

    Thanks for your answer! :smile: I already feared that this isn't intended behavoir but a bug... g
    I'll give it a try with the foundation snippet.

    Bests,
    Daniel