The InvokeDelete function is used to invoke the delete method on the API object. Using the InvokeDelete function allows you to use AMPscript to make an API call.
Use case: the function can be used to delete an object in SFMC. In the two examples below, we will try to delete a data extension and also delete a folder.InvokeDelete
Syntax:
InvokeDelete(1,2,3,4)
1 = Name of the object API to be deleted and defined in your AMPscript
2 = An AMPscript variable where you want the output parameter to be written to in as a string
3 = An AMPscript variable where you want the error code to be written to in as a string
4 = (Optional) Calls the DeleteOptions API object
Example:
%%[
/* declare variables */
var @de, @statusCode, @statusMsg, @errorCode
/* create DE */
Set @de = CreateObject(“DataExtension”)
SetObjectProperty(@de,”Name”,”TEST-API-Created Data Extension”)
SetObjectProperty(@de,”CustomerKey”,”TEST-API-Created Data Extension”)
SetObjectProperty(@de,”Description”,”Data Extension Created via API”)
SetObjectProperty(@de,”IsSendable”,”False”)
SetObjectProperty(@de,”IsTestable”,”False”)
/* create in specific folder – mouse over in SFMC to get ID */
SetObjectProperty(@de,”CategoryID”,”123″)
/* create fields in data extension */
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)
/* delete DE */
set @statusCode = InvokeDelete(@de, @statusMsg, @errorCode)
]%%
statusCode: %%=v(@statusCode)=%%
statusMsg: %%=v(@statusMsg)=%%
errorCode: %%=v(@errorCode)=%%
Output:
statusCode: Error statusMsg: Exception occurred during [Delete] errorCode: 2
Explanation:
In the InvokeDelete example above, the AMPscript creates a data extension to be deleted. The SetObjectProperty is used to created the data extension that is to be deleted in this test code. The data extension gets created but because of authorization, the data extension cannot be deleted.
Trying it again with a simple folder in the example below allows the InvokeDelete function to work. Using the example here on Creating a Data Extension Folder via API, we first create the folder that we intend to delete.
Example with Folder:
%%[
/* folder created to be deleted */
Set @df = CreateObject(“DataFolder”)
SetObjectProperty(@df,”Name”,”TESTING1234″)
SetObjectProperty(@df,”ID”, 375224)
SetObjectProperty(@df,”Description”,”Files associated with AMPscript dot com”)
SetObjectProperty(@df,”ContentType”,”DataExtension”)
SetObjectProperty(@df,”IsActive”,1)
SetObjectProperty(@df,”IsEditable”,1)
SetObjectProperty(@df,”AllowChildren”,1)
Set @pf = CreateObject(“DataFolder”)
SetObjectProperty(@pf,”ID”, 375217)
SetObjectProperty(@df,”ParentFolder”,@pf)
/* delete folder */
set @statusCode = InvokeDelete(@df, @statusMsg, @errorCode)
]%%
Output:
statusCode: statusMsg: Folder deleted successfully. errorCode: 0
Explanation:
When deleting a folder, identify the folder using the categoryID and the API call will be able to delete it using the InvokeDelete function.
Related functions are the InvokeCreate, CreateObject function, the function, and the AddObjectArrayItem function. SetObjectProperty
Related post: Creating a Data Extension Folder via API