Retrieve Automation history verified

Learn how to retrieve an Automation's history in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include the WSProxy method.

WSProxy

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

var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

var cols = [
    "Status", 
    "Name", 
    "CustomerKey",
    "CompletedTime",
    "StartTime"
];

var filter = {
    Property: "CustomerKey",
    SimpleOperator: "equals",
    Value: customerKey
};

var opts = {
    BatchSize: 300
};

var props = {
    QueryAllAccounts: false
};

var records = [],
    moreData = true,
    reqID = data = null;

while (moreData) {

    moreData = false;

    if(reqID) props.ContinueRequest = reqID;

    var data = api.retrieve("AutomationInstance", cols, filter, opts, props);

    if (data) {

        moreData = data.HasMoreRows;
        reqID = data.RequestID;
    
        for (var k in data.Results) {

            var item = data.Results[k];

            var o = {
                Status: item.Status,
                StatusMessage: item.StatusMessage,
                StartTime: DateTime.SystemDateToLocalDate(item.StartTime),
                CompletedTime: DateTime.SystemDateToLocalDate(item.CompletedTime)
            }

            records.push(o);
        }

    }
}

records.sort(function (a, b) { return (new Date(a.CompletedTime) < new Date(b.CompletedTime)) ? 1 : -1 });

var result = records;
<script runat="server">

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

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

    try {

        var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

        var result = retrieveAutomationHistory(customerKey);

        Write(Stringify(result));

    } catch(error) {

        Write(Stringify(error));

    }

    function retrieveAutomationHistory(key, limit) {

        var limit = limit || null;

        var cols = [
            "Status", 
            "Name", 
            "CustomerKey",
            "CompletedTime",
            "StartTime"
        ];

        var filter = {
            Property: "CustomerKey",
            SimpleOperator: "equals",
            Value: key
        };

        var opts = {
            BatchSize: 300
        };

        var props = {
            QueryAllAccounts: false
        };

        var records = [],
            moreData = true,
            reqID = data = null;
    
        while (moreData) {

            moreData = false;

            if(reqID) props.ContinueRequest = reqID;

            var data = api.retrieve("AutomationInstance", cols, filter, opts, props);

            if (data) {

                moreData = data.HasMoreRows;
                reqID = data.RequestID;
            
                for (var k in data.Results) {

                    var item = data.Results[k];

                    var o = {
                        Status: item.Status,
                        StatusMessage: item.StatusMessage,
                        StartTime: DateTime.SystemDateToLocalDate(item.StartTime),
                        CompletedTime: DateTime.SystemDateToLocalDate(item.CompletedTime)
                    }

                    records.push(o);
                }

            }
        }
    
        records.sort(function (a, b) { return (new Date(a.CompletedTime) < new Date(b.CompletedTime)) ? 1 : -1 });
    
        var num = (limit != null && limit <= records.length) ? limit : records.length;
    
        return records.slice(0, num);
    
    }

</script>
[
    {
        "Status": 1,
        "StatusMessage": "Complete",
        "StartTime": "2022-10-25T16:01:55.383",
        "CompletedTime": "2022-10-25T16:03:35.543"
    },
    {
        "Status": 1,
        "StatusMessage": "Complete",
        "StartTime": "2022-10-25T15:00:49.807",
        "CompletedTime": "2022-10-25T15:01:36.807"
    },
    {
        "Status": 1,
        "StatusMessage": "Complete",
        "StartTime": "2022-10-25T14:05:13.247",
        "CompletedTime": "2022-10-25T14:09:04.656"
    }
]

WARNING

The Automation needs to be scheduled in order to retrieve its activity's history.

Reference

Ressources and references related to the current methods.

Official documentation
SOAP object

Last Updated: