Add a Subscriber to a List verified

Learn how to add a Subscriber (subscribe a customer) to a List 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 attributes = {
    FirstName: "John",
    LastName: "Doe"
}

var result = lst.Subscribers.Add("example@mail.com", attributes);
<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 attributes = {
            FirstName: "John",
            LastName: "Doe"
        }

        var result = lst.Subscribers.Add("example@mail.com", attributes);

        Write(Stringify(result));

    } catch (error) {

        Write(Stringify(error));

    }

</script>
"OK"

WSProxy

Add a Subscriber to a List

var api = new Script.Util.WSProxy();

var config = {
    SubscriberKey: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    EmailAddress: "example@mail.com",
    Lists: [
        {
            ID: "12345"
        }
    ]
};

var options = {
    SaveOptions: [
        {
            PropertyName: '*',
            SaveAction: 'UpdateAdd'
        }
    ]
};

var result = api.updateItem("Subscriber", config, options);
<script runat="server">

    Platform.Load("core", "1");

    var api = new Script.Util.WSProxy();

    try {

        var request = api.retrieve("List", ["ListName", "ID"], {
            Property: "ListName",
            SimpleOperator: "equals",
            Value: "All Subscribers"
        });

        var id = request.Results[0].ID;

        var config = {
            SubscriberKey: GUID(),
            EmailAddress: "example@mail.com",
            Lists: [
                {
                    ID: id
                }
            ]
        };

        var options = {
            SaveOptions: [
                {
                    PropertyName: '*',
                    SaveAction: 'UpdateAdd'
                }
            ]
        };

        var result = api.updateItem("Subscriber", config, options);

        Write(Stringify(result));

    } catch (error) {

        Write(Stringify(error));

    }

</script>
{
    "Status": "OK",
    "RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    "Results": [
        {
            "Object": {
                "SubscriberKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                "Client": null,
                "EmailAddress": "example@mail.com",
                "Status": "Active",
                "ID": 123456789,
                "UnsubscribedDate": "0001-01-01T00:00:00.000",
                "EmailTypePreference": "Text",
                "Attributes": null,
                "PartnerType": null,
                "Lists": [
                    {
                        "List": null,
                        "Client": null,
                        "Status": "Active",
                        "Action": null,
                        "Subscriber": null,
                        "PartnerKey": null,
                        "PartnerProperties": null,
                        "CreatedDate": "0001-01-01T00:00:00.000",
                        "ModifiedDate": null,
                        "ID": 123456,
                        "ObjectID": null,
                        "CustomerKey": null,
                        "Owner": null,
                        "CorrelationID": null,
                        "ObjectState": null,
                        "IsPlatformObject": false
                    }
                ],
                "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
            },
            "UpdateResults": null,
            "ParentPropertyName": null,
            "StatusCode": "OK",
            "StatusMessage": "Updated Subscriber.",
            "OrdinalID": 0,
            "ErrorCode": 0,
            "RequestID": null,
            "ConversationID": null,
            "OverallStatusCode": null,
            "RequestType": "Synchronous",
            "ResultType": null,
            "ResultDetailXML": null
        }
    ]
}

Add a subscriber to an Auto-Suppression List

var api = new Script.Util.WSProxy();

var request = api.retrieve("SuppressionListDefinition", ["CustomerKey"], {
    Property: "Name",
    SimpleOperator: "equals",
    Value: "MyNewSuppressionList"
});

var customerKey = request.Results[0].CustomerKey;

var config = {
    CustomerKey: customerKey,
    Properties: [
        {
            Name: "Email Address",
            Value: "example@mail.com"
        }
    ]
};

var options = {
    SaveOptions: [
        {
            PropertyName: '*',
            SaveAction: 'UpdateAdd'
        }
    ]
};

var result = api.updateItem("DataExtensionObject", config, options);
<script runat="server">

    Platform.Load("core", "1");

    var api = new Script.Util.WSProxy();

    try {

        var request = api.retrieve("SuppressionListDefinition", ["CustomerKey"], {
            Property: "Name",
            SimpleOperator: "equals",
            Value: "MyNewSuppressionList"
        });

        var customerKey = request.Results[0].CustomerKey;

        var config = {
            CustomerKey: customerKey,
            Properties: [
                {
                    Name: "Email Address",
                    Value: "example@mail.com"
                }
            ]
        };

        var options = {
            SaveOptions: [
                {
                    PropertyName: '*',
                    SaveAction: 'UpdateAdd'
                }
            ]
        };

        var result = api.updateItem("DataExtensionObject", config, options);

        Write(Stringify(result));

    } catch (error) {

        Write(Stringify(error));

    }

</script>
{
    "Status": "OK",
    "RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    "Results": [
        {
            "ErrorMessage": null,
            "KeyErrors": null,
            "ValueErrors": null,
            "Object": {
                "Name": null,
                "Keys": null,
                "Type": null,
                "Properties": [
                    {
                        "Name": "Email Address",
                        "Value": "example@mail.com"
                    }
                ],
                "Client": null,
                "PartnerKey": null,
                "PartnerProperties": null,
                "CreatedDate": "0001-01-01T00:00:00.000",
                "ModifiedDate": null,
                "ID": 0,
                "ObjectID": null,
                "CustomerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                "Owner": null,
                "CorrelationID": null,
                "ObjectState": null,
                "IsPlatformObject": false
            },
            "UpdateResults": null,
            "ParentPropertyName": null,
            "StatusCode": "OK",
            "StatusMessage": "Upserted DataExtensionObject",
            "OrdinalID": 0,
            "ErrorCode": 0,
            "RequestID": null,
            "ConversationID": null,
            "OverallStatusCode": null,
            "RequestType": "Synchronous",
            "ResultType": null,
            "ResultDetailXML": null
        }
    ]
}

Reference

Ressources and references related to the current methods.

Official documentation
SOAP object

Last Updated: