Skip to content

Instantly share code, notes, and snippets.

@eliotharper
Created January 31, 2021 09:48
Show Gist options
  • Save eliotharper/d3673ce31c79e2ccd3b31843a5724f6c to your computer and use it in GitHub Desktop.
Save eliotharper/d3673ce31c79e2ccd3b31843a5724f6c to your computer and use it in GitHub Desktop.
Updates SFMC Query Activity with Auto-Suppression list as target Data Extension
<script language="javascript" runat="server">
// Author: Eliot Harper <eliot@eliot.com.au>
// Revision date: January 31, 2021
// DISCLAIMER
// While due care has been taken in the preparation of this
// supplied code example, no liability is assumed for incidental
// or consequential damages in connection with or arising out its
// use. Example code is provided on an "as is" basis and no
// expressed or implied warranty of any kind is made for the
// suitability of this code for your purpose. Salesforce Marketing
// Cloud operational procedures and programming methods may change
// between releases and you are solely responsible for determining
// whether this code is applicable for your intended use.
Platform.Load('Core', '1');
var prox = new Script.Util.WSProxy();
var queryStr = '';
queryStr += 'SELECT DISTINCT';
queryStr += '\nEmailAddress AS [Email Address]';
queryStr += '\nFROM _Sent s';
queryStr += '\nLEFT JOIN _Subscribers sub'; // add ENT. prefix if using in child BU
queryStr += '\n ON s.SubscriberKey = sub.SubscriberKey';
queryStr += '\nLEFT JOIN _Open o';
queryStr += '\n ON s.SubscriberKey = o.SubscriberKey';
queryStr += '\nWHERE o.SubscriberKey IS NULL';
var queryDef = {
CustomerKey: 'QUERY_ACTIVITY_EXTERNAL_KEY',
ObjectID: 'QUERY_ACTIVITY_URL_ID',
QueryText: queryStr,
TargetType: 'DE',
TargetUpdateType: 'Update',
DataExtensionTarget: {
CustomerKey: 'AUTO-SUPRESSION_LIST_EXTERNAL_KEY',
Name: 'AUTO-SUPRESSION_NAME'
}
};
var updateQuery = prox.updateItem('QueryDefinition', queryDef);
Write('result: ' + Stringify(updateQuery))
</script>
@robertwynter
Copy link

I tried to use this and it did not reduce the system-managed auto suppression list DE. I did not get an error. It just simply did not work. Any suggestions please and thank you in advance

@eliotharper
Copy link
Author

Odd, happy to help you investigate further as it works for me, however there are now a couple of easier ways to do this declaratively. You can either use Query Studio to create your query then you can select an auto-suppression list as the Target DE when you save the query in the UI, or you can use the new Data Copy or Import Activity in Automation Studio which now enables you to select auto-suppression list as the source.

@robertwynter
Copy link

Oh wow, I didn't realize i could do that through Query Studio. Nice thank you. I would still like to know what I did wrong in the script, if possible please. below is the tweak I made:

`<script language="javascript" runat="server">
// Author: Eliot Harper [email protected]
// Revision date: January 31, 2021

// DISCLAIMER
// While due care has been taken in the preparation of this
// supplied code example, no liability is assumed for incidental
// or consequential damages in connection with or arising out its
// use. Example code is provided on an "as is" basis and no
// expressed or implied warranty of any kind is made for the
// suitability of this code for your purpose. Salesforce Marketing
// Cloud operational procedures and programming methods may change
// between releases and you are solely responsible for determining
// whether this code is applicable for your intended use.

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

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

var queryStr = '';
queryStr += 'SELECT DISTINCT';
queryStr += '\n EmailAddress AS [Email Address],';
queryStr += '\n hed__Do_Not_Contact__c,';
queryStr += '\n ID';
queryStr += '\nFROM Contact_Salesforce';
queryStr += '\nWHERE hed__Do_Not_Contact__c = 'True'';

var queryDef = {
CustomerKey: '79C0A4FD-E7B6-44C5-8301-AF60273377CB',
ObjectID: '79c0a4fd-e7b6-44c5-8301-af60273377cb',
QueryText: queryStr,
TargetType: 'DE',
TargetUpdateType: 'Update',
DataExtensionTarget: {
CustomerKey: 'A98B89C3-701E-4A81-9A5E-2174C9B5C8B8',
Name: 'Auto-Suppression List'
}
};
var updateQuery = prox.updateItem('QueryDefinition', queryDef);

Write('result: ' + Stringify(updateQuery))

</script>`

@eliotharper
Copy link
Author

eliotharper commented Feb 17, 2025

The issue is the line queryStr += '\nWHERE hed__Do_Not_Contact__c = 'True''; which ends with two quotation marks — you did not escape the quotation marks around the value. However, as this is a Boolean value, you should use queryStr += '\nWHERE hed__Do_Not_Contact__c = 1';

@robertwynter
Copy link

Wanted to follow up. Once I correct the error you identified, this fixed it. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment