Convert Any Field into a Picklist
The following script lets you convert any field into a picklist.
- --This script will convert any field (usually text fields) into Picklists
- --and generate Picklist Options from the values.
- GO
- DECLARE @Ids Table(Id NVARCHAR(MAX))
- INSERT INTO
- @Ids
- VALUES
- --Enter all the IDs of Custom Field Definitions we want to Convert
- (''),
- (''),
- ('')
- --Look at the Options table and the Values table and generate any items that dont already exist
- INSERT INTO
- __M_CustomField_Definitions_Options
- (
- Id, Final_Subject
- )
- SELECT DISTINCT
- CONCAT(V2.Id, ' --- ', V1.Final_Value),
- V1.Final_Value
- FROM
- __M_CustomField_Values V1,
- __M_CustomField_Definitions V2
-
- WHERE 1=1
- AND V2.Id = V1.Final_CustomFieldDefinition_Id
- AND V2.Id IN (
- SELECT Id from @Ids
- )
- AND CONCAT(V2.Id, ' --- ', V1.Final_Value) NOT IN (
- SELECT
- Id
- FROM
- __M_CustomField_Definitions_Options
- )
- --If we want to convert a field into a Picklist, Run the below script.
- UPDATE
- __M_CustomField_Definitions
- SET
- Final_Kind = 'PickList'
- WHERE 1=1
- AND Id in (
- SELECT Id from @Ids
- )
- --Then update the Options table to reference the new items.
- UPDATE
- __M_CustomField_Values
- SET
- Final_Value = CONCAT(V2.Id, ' --- ', V1.Final_Value)
- FROM
- __M_CustomField_Values V1,
- __M_CustomField_Definitions V2
- WHERE 1=1
- AND V2.Id = V1.Final_CustomFieldDefinition_Id
- AND Final_Value NOT IN(
- SELECT
- Id
- FROM
- __M_CustomField_Definitions_Options
- )
- AND V2.Id In (
- SELECT Id from @Ids
- )
- GO