Flatten Custom Object fields into Contact/Matter Fields

Flatten Custom Object fields into Contact/Matter Fields

Warning
WARNING:
Many systems that support custom objects allow multiple rows in custom objects.
Many systems that allow custom fields only allow one single value for each field.
When you use this script, if a matter has two values set for the same custom field, only one will be used.

  1. --Update the custom field definitions to link to contacts/matters.
  2. UPDATE
  3. __M_CustomField_Definitions
  4. SET
  5. Final_Parent_Type = V4.Final_RelatedTo_Type
  6. FROM
  7. __M_CustomField_Definitions V1,
  8. __M_CustomObject_Definitions V2,
  9. __M_CustomObject_Instances V3,
  10. __M_CustomObject_Instances_Related_Entities V4
  11. WHERE 1=1
  12. AND V1.Original_Parent_Type = V2.Id
  13. AND V2.Id = V3.Original_CustomObjectDefinition_Id
  14. AND V3.Id = V4.Original_CustomObjectInstance_Id

  15. --Update the values to have new parent types.
  16. UPDATE
  17. __M_CustomField_Values
  18. SET
  19. Final_Parent_Type = V3.Final_RelatedTo_Type,
  20. Final_Parent_Id = V3.Final_RelatedTo_Id
  21. FROM
  22. __M_CustomField_Values V1,
  23. __M_CustomObject_Instances V2,
  24. __M_CustomObject_Instances_Related_Entities V3
  25. WHERE 1=1
  26. AND V1.Original_Parent_Id = V2.Id
  27. AND V1.Original_Parent_Type = V2.Original_CustomObjectDefinition_Id
  28. AND V2.Id = V3.Original_CustomObjectInstance_Id
  29. a