Using AMPscript with the Web Service API

From $1

    This document contains conceptual and procedural information about using AMPscript in conjunction with the web service API. You can make calls to the API using AMPscript functions. 

    Prerequisites

    You must be familiar with both AMPscript and the web service API in order to use the information in this document. You must also have access to use the web service API via your ExactTarget account. Please contact your ExactTarget representative if you wish to enable your account to use the web service API.

    Why Use AMPscript with the Web Service API

    Because AMPscript is a server-side scripting language, you can build interactions with the web service API directly into email messages or landing pages. This allows you to tightly integrate your content and messages with the web service API.

    How To Use AMPscript with the Web Service API

    AMPscript uses a series of functions to interact with the web service API. These functions interact with the API's methods, objects, and properties as detailed below:

    Object-related Functions

    CreateObject(S1)

    Returns a new ExactTarget web service API object.

    Arguments
    • S1    The API Object receiving the new item in text form
    Example
    SET @subscriber = CreateObject("Subscriber")

    Creates a new Subscriber object for the web service API.

    NOTE: an object created with CreateObject() should only be used for one particular API call. So, if a Subscriber object is created and then updated via InvokeUpdate, it cannot be passed to a Triggered Send.

    SetObjectProperty(O1,S1,S2)

    Sets a value for an object created by the CreateObject function.

    Arguments
    • O1    The API object receiving the assigned value
    • S1    The name of the property being assigned
    • S2    The value of the property being assigned
    Example

    The following example creates an API object named "Subscriber" and gives it the the "EmailAddress" property with the value of an email address.

    SET @subscriber=CreateObject("Subscriber")
    SetObjectProperty(@subscriber,"EmailAddress","user@example.com")

    The following example creates an API object named "Attribute", gives it a name of "First Name", and sets the value to John.

    SET @attribute=CreateObject(Attribute)
    SetObjectProperty(@attribute,"Name","First Name")
    SetObjectProperty(@attribute,"Value","John")

    AddObjectArrayItem(O1,S1,S2)

    Appends an object to an array on an API object.

    Arguments
    • O1    The API object containing the array
    • S1    The array property to which an item is to be added
    • S2    The item to be added to the array
    Example

    Given the example below:

    AddObjectArrayItem(@mySubscriber,"Attribute",@myNewAttribute)

    The Attributes array on the mySubscriberObject would receive the attribute myNewAttribute.

    Field(S1,S2)

    Returns the specified field in the specified row or property value from an API object.

    Arguments
    • S1    Row from which to return the field, or object from which to return the attribute
    • S2    Name of the field or attribute to return
    Example
    %%=Field(@row, "City")=%%

    The system returns value of the City field in the row specified in the @row variable.

     

    %%=Field(@subscriber, "EmailAddress")=%%

    The system returns the EmailAddress value from the @subscriber WSDL object.

    Method-related Functions

    InvokeCreate(O1,S1,N1,O2)

    Invokes the ExactTarget web service API Create method on an API object. Returns the API Status Code.

    Arguments
    • O1    The API object to be created
    • S1    Output parameter for the API status message
    • N1    Output parameter for the API error code
    • O2    The CreateOptions API call
    Example

    Given the example below:

    %%[
    
    var @emailaddr
    SET @emailaddr = "help@example.com"
    
       SET @ts = CreateObject("TriggeredSend")
       SET @tsDef = CreateObject("TriggeredSendDefinition")
       SET @ts_subkey = @emailaddr
    
       SetObjectProperty(@tsDef, "CustomerKey", "VolConfirm")
       SetObjectProperty(@ts, "TriggeredSendDefinition", @tsDef)  
    
       SET @ts_sub = CreateObject("Subscriber")
       SetObjectProperty(@ts_sub, "EmailAddress", @emailaddr)
       SetObjectProperty(@ts_sub, "SubscriberKey", @ts_subkey)
    
       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
     
    ]%%

    The AMPscript creates the TriggeredSend and TriggeredSendDefinition objects as @ts and @tsDef. It also assigns the correct properties to the objects and passes the object into the InvokeCreate call. The InvokeCreate call returns a status code and status message by which exception handling can be built. In this example, the AMPscript returns an error to the screen.

    InvokeUpdate(P1,P2,P3,P4)

    Invokes the Update method on an API object.

    Arguments
    • P1    Text object for the API object
    • P2    StatusMessage - optional text status message
    • P3    ErrorCode - optional numeric error code
    • P4    Options - optional options in text

    InvokeDelete(O1,S1,N1,O2)

    Invokes the Delete method on an API object. Returns the API Status Code.

    Arguments
    • O1    The API object to be deleted
    • S1    Output parameter for the API status message
    • N1    Output parameter for the API error code
    • O2    The DeleteOptions API call

    InvokeRetrieve(P1,P2,P3)

    Returns an array of API objects from a RetrieveRequest object.

    Arguments
    • P1    The RetrieveRequest object from which you wish to return an array of API objects.
    • P2    (Optional) The OverallStatus parameter passes the Status of the retrieve call to a previously defined AMPscript variable.
    • P3    (Optional) The RequestID parameter passes the RequestID of the retrieve call to a previously defined AMPscript variable.

    InvokePerform(O1,S1,S2)

    Invokes the ExactTarget web service API Perform method on an API object.  Returns the API Status Code.

    Arguments
    • O1    The API object to be performed
    • S1    The action to be performed - valid parameter values vary depending on object type
    • S2    Output parameter that gives the API status message

    InvokeRetrieve(P1,P2,P3)

    Returns an array of API objects from a RetrieveRequest object.

    Arguments
    • P1    The RetrieveRequest object from which you wish to return an array of API objects.
    • P2    (Optional) The OverallStatus parameter passes the Status of the retrieve call to a previously defined AMPscript variable.
    • P3    (Optional) The RequestID parameter passes the RequestID of the retrieve call to a previously defined AMPscript variable.

    InvokeUpdate(P1,P2,P3,P4)

    Invokes the Update method on an API object.

    Arguments
    • P1    Text object for the API object
    • P2    StatusMessage - optional text status message
    • P3    ErrorCode - optional numeric error code
    • P4    Options - optional options in text

    RaiseError(S1,B1,S2,S3,B2)

    Raises the error given in S1 and stops processing of job. If optional B1 is included with a value of "true," this function stops the send for the current subscriber only.

    Arguments
    • S1    The error message to be displayed
    • B1    Optional - a value of true stops the send for the current subscriber only
    • S2    Optional - displays API Error Code
    • S3    Optional - displays API Error Number
    • B2    Optional - records information written to data extension before the error occurs. A value of 1 retains information written to data extensions before the error occurs, even if the subscriber is skipped.
    NOTE: The B2 flag applies to data written by the following AMPscript functions:
    • InsertDE
    • InsertData
    • UpdateDE
    • UpdateData
    • UpsertDE
    • UpsertData
    • DeleteDE
    • DeleteData
    Example
    RaiseError("An Error Occurred")

    System returns the error message "An Error Occurred" and stops the job.

    RaiseError("Do not send to subscriber", true)

    System returns the error message "Do not send to subscriber" and stops the send to that subscriber only.

    NOTE: Please note that because the system pre-processes and builds these emails, tracking and reporting numbers include these emails despite the errors and may cause inaccuracies. Use this function to handle the errors of a small number of subscribers, rather than as a method to segment out large numbers of subscribers. Instead, use query activities and exclusion lists to handle your segmentation needs.

    SetObjectProperty(O1,S1,S2)

    Sets a value for an object created by the CreateObject function.

    Arguments
    • O1    The API object receiving the assigned value
    • S1    The name of the property being assigned
    • S2    The value of the property being assigned
    Example

    The following example creates an API object named "Subscriber" and gives it the the "EmailAddress" property with the value of an email address.

    SET @subscriber=CreateObject("Subscriber")
    SetObjectProperty(@subscriber,"EmailAddress","user@example.com")

    The following example creates an API object named "Attribute", gives it a name of "First Name", and sets the value to John.

    SET @attribute=CreateObject(Attribute)
    SetObjectProperty(@attribute,"Name","First Name")
    SetObjectProperty(@attribute,"Value","John")

    This page was last updated by Ryan Williams on Tue, 13 Dec 2011 13:51:41 GMT.

    If you require assistance with the ExactTarget application, please contact Global Support. If you wish to send Ryan direct feedback, fill out the form below:

     


    Was This Page Helpful?
    Suggestions or Comments:
    Name (optional):
    Email Address (optional):
    Enter 19035 backwards:
       
    Tags: (Edit tags)
    • No tags
     
    Comments (0)
    You must login to post a comment.

     
    Powered by MindTouch 2010
    Admin