1

I would like use AMPscript in an email to check whether the Subscriber Key already exists in the All Subscribers list at the time of sending an email.

Obviously, I'm counting on the AMPscript being evaluated first before creating a Subscriber Record at send time.

I've tried using the code below:

%%[
var @retrieveSubscriber, @retrieveSubscriberFilter, @subscriberRow, @thisSubscriber, @subscriberField, @abort, @requestId

set @retrieveSubscriber = CreateObject("RetrieveRequest")
    SetObjectProperty(@retrieveSubscriber, "ObjectType", "Subscriber")
    AddObjectArrayItem(@retrieveSubscriber, "Properties", "SubscriberKey")

    set @retrieveSubscriberFilter = CreateObject("SimpleFilterPart")
    SetObjectProperty(@retrieveSubscriberFilter, "Property", "SubscriberKey")
    SetObjectProperty(@retrieveSubscriberFilter, "SimpleOperator", "equals")
    AddObjectArrayItem(@retrieveSubscriberFilter, "Value", @subscriberEncodedsKey)

    SetObjectProperty(@retrieveSubscriber, "Filter", @retrieveSubscriberFilter)
    set @subscriberRow = InvokeRetrieve(@retrieveSubscriber, @requestId)
    set @thisSubscriber = row(@subscriberRow, 1)
    set @subscriberField = field(@thisSubscriber, "SubscriberKey")
    if @subscriberField == _subscriberkey then
        set @abort = true
    endif
]%%

I'm not sure if my code is correct or not but I receive the error:

An error occurred when attempting to evaluate a TreatAsContent function call. See inner exception for details. CreateObject Function is not valid in content. This function is only allowed in non sendable content. Function: CreateObject("RetrieveRequest")

Is there any way that I can check to see if a subscriber exists in the All Subscribers list within an email?

3

I don't think retrieves like this are allowed in emails. You could just use a lookup against the _Subscribers data view:

%%[

var @subscriberField, @subscriberEncodedsKey, @abort, @sk

set @abort = false
set @sk = AttributeValue("_subscriberkey")

set @subscriberEncodedsKey = "aspriggs@degdigital.com"
set @subscriberField = Lookup("ent._Subscribers", "SubscriberKey", "SubscriberKey", @subscriberEncodedsKey)

if @subscriberField == @sk then
    set @abort = true
endif


]%%
<br>@sk: %%=v(@sk)=%%
<br>@subscriberField: %%=v(@subscriberField)=%%
<br>@abort: %%=v(@abort)=%%

Output

@sk: aspriggs@degdigital.com 
@subscriberField: aspriggs@degdigital.com 
@abort: True 
  • Thanks Adam. I've tested this and it works correctly when previewing. however if I delete the subscribers from the All Subscribers list, when I send the email all subscribers are result as @abort: True. It appears that the Subscriber records are added before OMM evaluates the AMPscript, which is annoying. Can you think of a solution for this? – Eliot Harper Oct 25 '16 at 17:15
  • HI Eliot, what if this code executed from an exclusion script? Will this evaluate before the record is added to all_subscribers? – David Devoy Oct 25 '16 at 18:12
  • Hi @DavidDevoy, we're not using a Triggered Send for this however, I have come up with a solution; ultimately I want to ensure that the email address in the Triggered Send DE matches the Subscriber so I've used the AMPscript at gist.github.com/eliotharper/abaec0a7d378af4f2a9328b915dedb77 – Eliot Harper Oct 26 '16 at 0:02
  • Thanks @AdamSpriggs, I've adapted your answer further but this certainly helped me in the right direction and your answer is technically correct although just be aware that OMM evaluates AMPscript after creating the subscriber. So I'm accepting your answer for the benefit of others. – Eliot Harper Oct 26 '16 at 0:03

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.