verified
Retrieve a SubscriberLearn how to retrieve a Subscriber in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include Core and WSProxy methods.
Core
var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var lst = List.Init(customerKey);
var filter = {
Property: "SubscriberKey",
SimpleOperator: "equals",
Value: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
}
var result = lst.Subscribers.Retrieve(filter);
<script runat="server">
Platform.Load("core", "1");
try {
var request = List.Retrieve({
Property: "ListName",
SimpleOperator: "equals",
Value: "MyNewList"
});
var customerKey = request[0].CustomerKey;
var lst = List.Init(customerKey);
var filter = {
Property: "SubscriberKey",
SimpleOperator: "equals",
Value: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
}
var result = lst.Subscribers.Retrieve(filter);
Write(Stringify(result));
} catch (error) {
Write(Stringify(error));
}
</script>
[
{
"ID": 123456789,
"EmailAddress": "example@mail.com",
"SubscriberKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"PartnerKey": null,
"Status": "Active",
"CreatedDate": "2020-04-21T08:30:00.000",
"UnsubscribedDate": "0001-01-01T00:00:00.000",
"EmailTypePreference": "HTML"
}
]
WSProxy
Retrieve a Subscriber
var api = new Script.Util.WSProxy();
var cols = [
"SubscriberKey",
"EmailAddress",
"Status",
"EmailTypePreference"
];
var filter = {
Property: "EmailAddress",
SimpleOperator: "equals",
Value: "example@mail.com"
};
var result = api.retrieve("Subscriber", cols, filter);
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
try {
var cols = [
"SubscriberKey",
"EmailAddress",
"Status",
"EmailTypePreference"
];
var filter = {
Property: "EmailAddress",
SimpleOperator: "equals",
Value: "example@mail.com"
};
var result = api.retrieve("Subscriber", cols, filter);
Write(Stringify(result));
} catch (error) {
Write(Stringify(error));
}
</script>
{
"Status": "OK",
"RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"Results": [
{
"SubscriberKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"Client": null,
"EmailAddress": "example@mail.com",
"Status": "Active",
"Attributes": null,
"ID": 0,
"Lists": null,
"UnsubscribedDate": "0001-01-01T00:00:00.000",
"PartnerType": null,
"EmailTypePreference": "HTML",
"GlobalUnsubscribeCategory": null,
"SubscriberTypeDefinition": null,
"Addresses": null,
"PrimarySMSAddress": null,
"PrimarySMSPublicationStatus": "OptedIn",
"PrimaryEmailAddress": null,
"Locale": null,
"PartnerKey": null,
"PartnerProperties": null,
"CreatedDate": "0001-01-01T00:00:00.000",
"ModifiedDate": null,
"ObjectID": null,
"CustomerKey": null,
"Owner": null,
"CorrelationID": null,
"ObjectState": null,
"IsPlatformObject": false
}
],
"HasMoreRows": false
}
Retrieve all Subscribers from a List
var api = new Script.Util.WSProxy();
var cols = [
"ObjectID",
"SubscriberKey",
"CreatedDate",
"ModifiedDate",
"Client.ID",
"Client.PartnerClientKey",
"ListID",
"Status"
];
var filter = {
Property: "ListID",
SimpleOperator: "equals",
Value: "123456"
};
var result = api.retrieve("ListSubscriber", cols, filter);
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
try {
var req1 = api.retrieve("List", ["ListName", "Category", "ID"], {
Property: "ListName",
SimpleOperator: "equals",
Value: "MyNewList"
});
var id = req1.Results[0].ID;
var cols = [
"ObjectID",
"SubscriberKey",
"CreatedDate",
"ModifiedDate",
"Client.ID",
"Client.PartnerClientKey",
"ListID",
"Status"
];
var filter = {
Property: "ListID",
SimpleOperator: "equals",
Value: id
};
var opts = {
BatchSize: 300
};
var props = {
QueryAllAccounts: false
};
var result = [],
moreData = true,
reqID = data = null;
while(moreData) {
moreData = false;
if(reqID) props.ContinueRequest = reqID;
var req2 = api.retrieve("ListSubscriber", cols, filter, opts, props);
if (req2) {
moreData = req2.HasMoreRows;
reqID = req2.RequestID;
var results = req2.Results;
for (var k in results) {
var subscriber = results[k];
result.push(subscriber);
}
}
}
Write(Stringify(result));
} catch (error) {
Write(Stringify(error));
}
</script>
[
{
"SubscriberKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"Client": {
"ID": 100000000000,
"ClientID1": 0,
"PartnerClientKey": null,
"UserID": 0,
"PartnerUserKey": null,
"CreatedBy": 0,
"ModifiedBy": 0,
"EnterpriseID": 0,
"CustomerKey": null,
"CustomerID": null
},
"ListID": 12345678,
"Status": "Active",
"CreatedDate": "2022-11-16T06:01:00.000",
"PartnerKey": null,
"PartnerProperties": null,
"ModifiedDate": "2022-11-16T06:01:00.000",
"ID": 0,
"ObjectID": "12345678",
"CustomerKey": null,
"Owner": null,
"CorrelationID": null,
"ObjectState": null,
"IsPlatformObject": false
}
]
Reference
Ressources and references related to the current methods.
Official documentation
SOAP object