Posted in Marketing Cloud, Salesforce

Marketing Cloud : Insert into Subscriber List using AMPScript

SSJS provides a simpler solution to add a subscriber to the list. However, with AMPscript, there is no one liner solution. We need to make use of SubscriberList object.

%%[
/* Replace with your list ID */
SET @ListID = "<YOUR_ID>"
SET @SubscriberKey = "<Subscriber_Key>"
SET @rr = CreateObject("RetrieveRequest")
SetObjectProperty(@rr,"ObjectType","ListSubscriber")
AddObjectArrayItem(@rr,"Properties","SubscriberKey")
AddObjectArrayItem(@rr,"Properties","ListID")
SET @sfpl = CreateObject("SimpleFilterPart")
SetObjectProperty(@sfpl,"Property","ListID")
SetObjectProperty(@sfpl,"SimpleOperator","equals")
AddObjectArrayItem(@sfpl,"Value",@ListID)
SET @sfpr = CreateObject("SimpleFilterPart")
SetObjectProperty(@sfpr,"Property","SubscriberKey")
SetObjectProperty(@sfpr,"SimpleOperator","equals")
AddObjectArrayItem(@sfpr,"Value",@SubscriberKey)
SET @cf = CreateObject("ComplexFilterPart")
SetObjectProperty(@cf,"LeftOperand",@sfpl)
SetObjectProperty(@cf,"RightOperand",@sfpr)
SetObjectProperty(@cf,"LogicalOperator","AND")
SetObjectProperty(@rr,"Filter",@cf)
SET @listFields = InvokeRetrieve(@rr)
/* Update only when it is not already present */
IF NOT(RowCount(@listFields)) THEN
SET @sub = CreateObject("Subscriber")
SetObjectProperty(@sub,"SubscriberKey",@SubscriberKey)
set @lSub = CreateObject("SubscriberList")
SetObjectProperty(@lSub,"ID",@ListID)
SetObjectProperty(@lSub,"Status","Active")
AddObjectArrayItem(@sub,"Lists",@lSub)
SET @updateStatus = InvokeUpdate(@sub)
IF @updateStatus == "OK" THEN
Output(v("Subscriber added to the list successfully!"))
ELSE
Output(v("There was a problem adding the subscriber added to the list!"))
ENDIF
ELSE
Output(v("Subscriber already exists in list"))
ENDIF
]%%

Provide the ListID variable with the ID of the List including the Publication List.

To get the ID of the list, either you can click on the export option and you can find the ID in the pop-up wizard. Otherwise, you can use a free Chrome extension that comes with loads of other information which you might need for other activities.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s