1

I am creating a publication centre using which a user can opt-in for a list. For this purpose, I have to initially check if the subscriber already exists in a list or not. I am using the following code to check whether he is a part of the list or not.

 %%[
var @rr1, @sfp4, @sfp5, @cf0, @LookUpSub, @RowCountSub
SET @rr1 = CreateObject("RetrieveRequest")
   SetObjectProperty(@rr1, "ObjectType", "ListSubscriber")
   AddObjectArrayItem(@rr1,"Properties","ListID") 
   AddObjectArrayItem(@rr1,"Properties","SubscriberKey")

SET @sfp4 = CreateObject("SimpleFilterPart")
   SetObjectProperty(@sfp4, "Property", "SubscriberKey")
   SetObjectProperty(@sfp4, "SimpleOperator", "equals")
   AddObjectArrayItem(@sfp4, "Value",@sub_ID)

SET @sfp5 = CreateObject("SimpleFilterPart")
   SetObjectProperty(@sfp5, "Property", "ListID")
   SetObjectProperty(@sfp5, "SimpleOperator", "equals")
   AddObjectArrayItem(@sfp5, "Value",@lis_ID)
   
Set @cf0 = CreateObject("ComplexFilterPart")
   SetObjectProperty(@cf0,"LeftOperand",@sfp4)
   SetObjectProperty(@cf0,"RightOperand",@sfp5)
   SetObjectProperty(@cf0,"LogicalOperator","AND")

   SetObjectProperty(@rr1, "Filter", @cf0)
   SET @LookUpSub = InvokeRetrieve(@rr1)
   SET @RowCountSub = Rowcount(@LookUpSub) 
]%%

This is inside a for loop checking for every list. But when I run this I get an error.

ExactTarget.OMM.InvalidScriptException: The function expression is invalid. See inner exception for detail.
Script: ContentBlockByName(@contentAreaPath)\r\n Index: 4342\r\n ListID: 0\r\n Error Code: OMM_FUNC_EXPR_INVALID
- from Jint --> 
 --- inner exception 1---
ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a ContentBlockByName function call.
Function Call: ContentBlockByName(@contentAreaPath)
See inner exception for details.
Error Code: OMM_FUNC_EXEC_ERROR
- from OMMCommon --> 
 --- inner exception 2---
ExactTarget.OMM.InvalidScriptException: The function expression is invalid. See inner exception for detail.
Script: AddObjectArrayItem(@sfp5, \"Value\",@lis_ID)
Index: 839
ListID: 0
Error Code: OMM_FUNC_EXPR_INVALID
- from OMMCommon --> 
--- inner exception 3---
nExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a AddObjectArrayItem function call. See inner exception for details.
Error Code: OMM_FUNC_EXEC_ERROR
- from OMMCommon --> 
--- inner exception 4---
System.InvalidCastException: Object cannot be stored in an array of this type. - from mscorlib

This is because I am guessing I cannot reuse the same variable names for different search purposes. I looked around and I found that declaring variables like var @xyz can empty or flush the previously existing values but it's not working here. Is there a work around this issue or something that can resolve the problem? Thank you for your help.

1 Answer 1

1

If we are sticking to AMPScript, rather then do all that with API - you can also just do a Lookup to the _ListSubscriber Data View which should have all the lists a person is on. As this is on a Cloudpage and not an email, the delay/extra processing should not be significant and likely less then the AMPScript API route.

I would first do a lookup to get all the publication lists associated with that subscriberKey:

SET @LURows = LookupRows("_ListSubscribers","SubscriberKey", AttributeValue("_subscriberkey"), "ListType","Publication List")

Then you would iterate through this creating a modified delimited list:

SET @listStr = "|"
FOR @i=1 TO Rowcount(@LURows) DO
  SET @row = ROW(@LURows,@i)
  SET @listName = FIELD(@row,"ListName")
  SET @listStr = CONCAT(@listStr,@listName,"|")
NEXT @i

This will generate something like |list1|list2|list3| etc.

Then in order to check if they exist on a publication list, you can just do an IndexOf() against this string to verify.

For example, if you want to see if the person is on list2 then you would do:

IF INDEXOF(@listStr,CONCAT("|","list2","|")) > 0 THEN
  set @list2  = 1
ENDIF

This will search for the value of |list2| inside the modified delimited string. This will remove any partial value matches as each will start and end with a pipe | forcing only exact matches.

If you still wanted to use the API, I would instead shift to using WSProxy via SSJS as there are a lot more options there and it is a ton more performant for this task.

2
  • 1
    This works, only you have left out a "THEN" in the if condition by mistake. Thank you for your help!
    – Apoorva
    Jan 27, 2021 at 15:59
  • Updated to proper syntax. Thanks for the comment letting me know. Jan 27, 2021 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.