Synthesize Missing Matters for Documents

Synthesize Missing Matters for Documents

Some systems only allow documents to be linked to matters.  These scripts will allow you to restore data into these systems. 

When Matters No Longer Exist

The following script with synthesize matters for document related to matters that no longer exist.

  1. --There are documents that are attached to matters that no longer exist
  2. --Create dummy matters out of the missing links.

  3. INSERT INTO __M_Matters (
  4. Id,
  5. Final_ReferenceCode,
  6. Final_Subject,
  7. Final_Description
  8. )
  9. SELECT DISTINCT
  10. Final_Parent_Id,
  11. Final_Parent_Id,
  12. CONCAT('Orphaned Matter ', Final_Parent_Id),
  13. CONCAT('Orphaned Matter ', Final_Parent_Id)
  14. FROM
  15. __M_Documents_Digital_Files
  16. WHERE 1=1
  17. AND Final_Parent_Type = '__M_Matters'
  18. AND Final_Parent_Id NOT IN (SELECT Id FROM __M_Matters)


When Documents are Related to Contacts

If there are documents that are only related to contacts, this script can be used to create a 'General Matter' for each client with documents and restore the documents into those matters.

  1. --There are documents that are only related to matters.
  2. --Create a 'General Matter' for each contact that has documents
  3. --And then move the documents into this matter.

  4. --Variables we can change DECLARE @PREFIX NVARCHAR(MAX) = 'CONTACT --- ' DECLARE @Status NVARCHAR(MAX) = 'Archived' DECLARE @ReferenceCode NVARCHAR(MAX) = 'General Matter' DECLARE @Subject NVARCHAR(MAX) = 'General Matter' DECLARE @Description NVARCHAR(MAX) = 'General Matter' --Create the Matters INSERT INTO __M_Matters (
  5. Id,
  6. Final_Status,
  7. Final_ClientContact_Id,
  8. Final_ReferenceCode,
  9. Final_Subject,
  10. Final_Description
  11. ) SELECT DISTINCT Concat(@PREFIX, Final_Parent_Id),
  12. @Status,
  13. Final_Parent_Id,
  14. @ReferenceCode,
  15. @Subject,
  16. @Description FROM __M_Documents_Digital_Files WHERE 1=1 AND Final_Parent_Type = '__M_Contacts' --Move the documents to the matters we just created UPDATE __M_Documents_Digital_Files SET Final_Parent_Type = '__M_Matters', Final_Parent_Id = CONCAT(@Prefix, Final_Parent_Id) WHERE 1=1 AND Final_Parent_Type = '__M_Contacts'

  17. GO