[SalesForce] Update Data Extension via WSProxy

I've built a CloudPage which runs SQL queries quickly without having to use automation studio.

The SSJS that runs the query works perfectly, but I'm also trying to use this function to also update a data extension to use as an audit log of it being used. It's supposed to simply update a data extension called Query Log. It just has four columns, Target, Type, Definition, andRunDate. The data extension is set up and all fields are nullable, but I can't get it to run. It doesn't give me an error that I can see and the page loads fine, but it doesn't add a record to the data extension. This is the code:

<script>
    Platform.Load("Core","1");
var api = new Script.Util.WSProxy();
var target = Variable.GetValue("@target");
var type = Variable.GetValue("@type");
var qdef = Variable.GetValue("@definition");
/*Build DE Object*/
var updateObject = {
    CustomerKey: 'QueryLog',
    Properties: [
        {
            Name: 'Target'
            Value: target
        },
        {
            Name: 'Type'
            Value: type
        },
        {
            Name: 'QueryDef'
            Value: definition
        },
        {
            Name: 'RunDate'
            Value: Platform.Function.Now()
        }
    ]
};

var options = {SaveOptions:[{'PropertyName':'*',SaveAction:'UpdateAdd'}]};
var res = api.updateItem('DataExtensionObject',updateObject,options);
</script>

Best Answer

I'd put a try/catch around the whole thing and see what you get. You're also missing the runat attribute on your script tag.

<script runat="server">
Platform.Load("Core","1");

try {

    var api = new Script.Util.WSProxy();
    var target = Variable.GetValue("@target");
    var type = Variable.GetValue("@type");
    var qdef = Variable.GetValue("@definition");

    /*Build DE Object*/
    var updateObject = {
    CustomerKey: 'QueryLog',
    Properties: [
        {
            Name: 'Target'
            Value: target
        },
        {
            Name: 'Type'
            Value: type
        },
        {
            Name: 'QueryDef'
            Value: definition
        },
        {
            Name: 'RunDate'
            Value: Platform.Function.Now()
        }
    ]
    };

    var options = {SaveOptions:[{'PropertyName':'*',SaveAction:'UpdateAdd'}]};
    var res = api.updateItem('DataExtensionObject',updateObject,options);

} catch (e) {
    Write("<br>e: " + Stringify(e));
}
</script>
Related Topic