Skip to content

Instantly share code, notes, and snippets.

@VijayaSankarN
Last active February 24, 2021 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VijayaSankarN/0900a9275231dfc63829870cbae875ca to your computer and use it in GitHub Desktop.
Save VijayaSankarN/0900a9275231dfc63829870cbae875ca to your computer and use it in GitHub Desktop.
AMPScript - Insert subscriber into list (including Publication List) - Details: https://vijayasankarn.wordpress.com/2021/02/24/marketing-cloud-insert-into-subscriber-list-using-ampscript/
%%[
/* 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
]%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment