The InvokeCreate function can be used to initiate triggered sends from SFMC. Using the InvokeCreate function allows you to use AMPscript to make an API call.
Use case: the InvokeCreate function can be used to trigger a transactional email or any other type of triggered send. For example, the InvokeCreate function can be used in the result page of a form fill to trigger an email send in real-time when the form is completed.
Syntax:
InvokeCreate(1,2,3,4)
1 = Name of the object API to be created 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 CreateOptions API object
Example:
%%[
var @emailaddr, @ts, @tsDef, @ts_subkey, @ts_sub, @ts_statusCode, @errorCode
set @emailaddr = “jackson@ampscript.com”
SET @ts = CreateObject(“TriggeredSend”)
SET @tsDef = CreateObject(“TriggeredSendDefinition”)
SET @ts_subkey = @emailaddr
SetObjectProperty(@tsDef, “CustomerKey”, “TriggeredSendKey”)
SetObjectProperty(@ts, “TriggeredSendDefinition”, @tsDef)
SET @ts_sub = CreateObject(“Subscriber”)
SetObjectProperty(@ts_sub, “EmailAddress”, @emailaddr)
SET @attribute = CreateObject(“Attribute”)
SetObjectProperty(@attribute, “Name”, “FirstName”)
SetObjectProperty(@attribute, “Value”, @FirstName)
AddObjectArrayItem(@ts_sub, “Attributes”, @attribute)
IF NOT EMPTY(@ts_subkey) THEN
SetObjectProperty(@ts_sub, “SubscriberKey”, @ts_subkey)
ELSE
SetObjectProperty(@ts_sub, “SubscriberKey”, @emailaddr)
ENDIF
AddObjectArrayItem(@ts, “Subscribers”, @ts_sub)
SET @ts_statusCode = InvokeCreate(@ts, @ts_statusMsg, @errorCode)
IF @ts_statusCode != “OK” THEN
RaiseError(@ts_statusMsg, 0, @ts_statusCode, @errorCode)
ENDIF
]%%
Output:
ts_statusMsg: OK errorCode: 0
Explanation:
In the InvokeCreate example above, the goal of the AMPscript block is to trigger an email send from an already existing triggered send with a customer key of “TriggeredSendKey.” In that sendable data extension are attributes such as Email Address and First Name which will be populated this AMPscript code. (The assumption is that this is a form fill and that variable is populated from a previous step — i.e. an AttributeValue function can be used to retrieve that FirstName value).
In the example above, I have hardcoded my email address at jackson@ampscript.com so that it can be easily tested.
The SetObjectProperty and AddObjectArrayItem functions are wrapped in an If/Then Function and the InvokeCreate function writes status message and error code to send the triggered email.
Related functions are the InvokeDelete function, CreateObject function, the function, and the AddObjectArrayItem function. SetObjectProperty