Awesome stuff coming soon!
logo

Snippets

AMPscript snippet to create Data Extention

Did you ever wonder that you could programatically create Data Extensions in Marketing cloud!



                       	
%%[
/CREATE THE DATA EXTENSION/
Set @de = CreateObject("DataExtension")
SetObjectProperty(@de,"Name","API-Created Data Extension")
SetObjectProperty(@de,"CustomerKey","2020JAN23_VIA_AMPSCRIPT")
SetObjectProperty(@de,"Description","Data Extension Created via API")
SetObjectProperty(@de,"IsSendable","True")
SetObjectProperty(@de,"IsTestable","False")
/DE Folder ID - mouse over in app to get this ID /
SetObjectProperty(@de,"CategoryID","494608")
/* THIS IS THE SENDABLE FIELD */
Set @deFields = CreateObject("DataExtensionField")
SetObjectProperty(@deFields,"FieldType","EmailAddress")
SetObjectProperty(@deFields,"IsRequired","true")
SetObjectProperty(@deFields,"IsPrimaryKey","true")
SetObjectProperty(@deFields,"IsNillable","false")
SetObjectProperty(@deFields,"MaxLength","100")
SetObjectProperty(@deFields,"Name","email_address")
AddObjectArrayItem(@de,"Fields",@deFields)
SetObjectProperty(@de,"SendableDataExtensionField",@deFields)
 Set @deFields = CreateObject("DataExtensionField")
SetObjectProperty(@deFields,"FieldType","Text")
SetObjectProperty(@deFields,"IsRequired","true")
SetObjectProperty(@deFields,"IsPrimaryKey","false")
SetObjectProperty(@deFields,"IsNillable","false")
SetObjectProperty(@deFields,"MaxLength","100")
SetObjectProperty(@deFields,"Name","subscriber_key")
AddObjectArrayItem(@de,"Fields",@deFields)
Set @deFields = CreateObject("DataExtensionField")
SetObjectProperty(@deFields,"FieldType","Text")
SetObjectProperty(@deFields,"IsRequired","false")
SetObjectProperty(@deFields,"IsPrimaryKey","false")
SetObjectProperty(@deFields,"IsNillable","false")
SetObjectProperty(@deFields,"MaxLength","50")
SetObjectProperty(@deFields,"Name","first_name")
AddObjectArrayItem(@de,"Fields",@deFields)
Set @deFields = CreateObject("DataExtensionField")
SetObjectProperty(@deFields,"FieldType","Text")
SetObjectProperty(@deFields,"IsRequired","false")
SetObjectProperty(@deFields,"IsPrimaryKey","false")
SetObjectProperty(@deFields,"IsNillable","false")
SetObjectProperty(@deFields,"MaxLength","75")
SetObjectProperty(@deFields,"Name","last_name")
AddObjectArrayItem(@de,"Fields",@deFields)
/* This field sets how the data extension field above relates to the all subscribers list */
SET @ts_attr = CreateObject("Attribute")
SetObjectProperty(@ts_attr, "Name", "Subscriber Key")
SetObjectProperty(@ts_attr, "Value", "Subscriber Key")
SetObjectProperty(@de,"SendableSubscriberField",@ts_attr)
Set @StatCode = InvokeCreate(@de, @StatMessage, @ErrorCode)
]%%
Status = %%= v(@StatMessage) =%% -- %%= v(@ErrorCode) =%%