Custom Fields: Creating Custom Fields via Queries

Custom Fields: Creating Custom Fields via Queries

Sometimes you may need to create a new custom field to handle an unmapped column in a database.

The queries below will help you do that.

  1. -- This is the ID we are giving our custom field definition.
  2. DECLARE @FieldId NVARCHAR(MAX) = 'ConNo --- CUSTOM'

  3. --This is a custom field for Contacts
  4. DECLARE @ParentType NVARCHAR(MAX) = '__M_Contacts'

  5. --This first part will create our custom field definition
  6. INSERT INTO __M_CustomField_Definitions (
  7. Id,
  8. Final_Kind,
  9. Final_Parent_Type,
  10. Final_Subject,
  11. Final_Visibility
  12. ) VALUES (
  13. --These are the setting for our custom field definition
  14. 'TextLine', --It is a text line
  15. @FieldId,
  16. @ParentType,
  17. 'Contact Number', --It is named 'Contact Number'
  18. 'Default' --It is visible
  19. )

  20. --This query creates the values.
  21. --In this case, we're creating a custom field definition for a contact.
  22. INSERT INTO __M_CustomField_Values (
  23. Id,
  24. Final_Parent_Id,
  25. Final_Parent_Type,
  26. Final_CustomFieldDefinition_Id,
  27. Final_Value
  28. )
  29. --This part of the query will change based on the table you are pulling values from.
  30. SELECT
  31. --SysId needs to be replaced with the column that
  32. --will match the Id in the __M_Contacts table
  33. CONCAT(SysId, ' --- ', @FieldId),
  34. SysId,
  35. @ParentType,
  36. @FieldId,
  37. con_no --This is the column that contains our custom value.
  38. FROM
  39. --This is the database and table our custom values are coming from.
  40. [LEGACY_TimeMatters_TestDb2].lntmuid.contact

  41. GO