Snippets
Created by
Vlad Silak
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | <script runat="server">
/*
* entry here should be done from the email using following notation:
* <a href="%%=RedirectTo(CloudPagesURL(ID)=%%">Unsubscribe from these communications</a>
*/
Platform.Load("Core", "1");
var error = false, errorMessage = "", data;
var proxy = new Script.Util.WSProxy();
var PARAMS = {};
PARAMS.contactKey = Attribute.GetValue("_subscriberkey");
PARAMS.emailAddress = Attribute.GetValue("emailaddr");
PARAMS.emailID = Attribute.GetValue("_emailid");
PARAMS.dataSourceName = Attribute.GetValue("_DataSourceName");
PARAMS.listID = Attribute.GetValue("listID");
PARAMS.jobID = Attribute.GetValue("jobID");
PARAMS.batchID = Attribute.GetValue("_JobSubscriberBatchID");
PARAMS.subscriberID = Attribute.GetValue("subscriberID");
PARAMS.emailName = Attribute.GetValue("emailname_");
PARAMS.listName = Attribute.GetValue("_listname");
PARAMS.unsubLogDEKey = "DATA_EXTENSION_KEY";
/* the processing should only occur when there are email and contact ID's available */
if (PARAMS.emailID.length > 0 && PARAMS.contactKey.length > 0) {
try {
/* get field name from publication list details */
var props = ["Description"];
var filter = {
Property: "ID"
, SimpleOperator: "equals"
, Value: PARAMS.listID
};
data = proxy.retrieve("List", props, filter);
if (data.Results.length > 0) {
PARAMS.preferenceField = data.Results[0].Description;
Variable.SetValue("@preferenceField", PARAMS.preferenceField);
}
/* get send event date */
props = ["EventDate", "SubscriberKey", "EventType"];
filter = {
LeftOperand: {
Property: "SendID"
, SimpleOperator: "equals"
, Value: PARAMS.jobID
}
, LogicalOperator: "AND"
, RightOperand: {
Property: "BatchID"
, SimpleOperator: "equals"
, Value: PARAMS.batchID
}
, AdditionalOperand: {
Property: "SubscriberID"
, SimpleOperator: "equals"
, Value: PARAMS.subscriberID
}
};
data = proxy.retrieve("SentEvent", props, filter);
if (data.Results.length > 0) {
PARAMS.sendDate = data.Results[0].EventDate;
Variable.SetValue("@sendDate", PARAMS.sendDate);
}
</script>
%%[
SET @contactKey = _subscriberkey
SET @crmUpdated = true
/* prepare unsubscribe event dates for the log */
SET @eventDate = NOW()
SET @localEventDate = SystemDateToLocalDate(@eventDate)
SET @localSendDate = SystemDateToLocalDate(@sendDate)
/* ensure contact is SF Contact */
IF NOT (Substring(@contactKey, 1, 3) == "003" AND Substring(@preferenceField, Subtract(Length(@preferenceField),2)) == "__c") THEN
/* ERROR - log that we can't update CRM */
SET @crmUpdated = false
ELSE
/* get the Communication Preferences Object */
SET @rs = RetrieveSalesforceObjects('Contact', 'AfP_Communication_Preference__c', 'Id', '=', @contactKey)
IF (ROWCOUNT(@rs) > 0) THEN
SET @cpID = FIELD(ROW(@rs, 1), 'AfP_Communication_Preference__c')
ELSE
SET @rs = RetrieveSalesforceObjects('Communication_Preference__c', 'Id', 'Contact__c', '=', @contactKey, 'Business_Unit__c', '=', 'Act for Peace')
IF (ROWCOUNT(@rs) > 0) THEN
SET @cpID = FIELD(ROW(@rs, 1), 'Id')
ELSE
/* ERROR - log that we can't update CRM */
SET @crmUpdated = false
ENDIF
ENDIF
ENDIF
/* check we have communication preference ID */
IF NOT EMPTY(@cpID) THEN
SET @updateRecord = UpdateSingleSalesforceObject (
"Communication_Preference__c", @cpID
, @preferenceField, "Yes"
)
IF @updateRecord < 1 THEN
SET @crmUpdated = false
ENDIF
ENDIF
]%%
<script runat="server">
} catch (e) {
/* might need to log it in some way */
errorMessage += e.message + " \n<br>\n<br> " + e.description;
Variable.SetValue("@errorMessage", errorMessage);
}
/* pull out additional parameters from AMPscript context */
PARAMS.crmUpdated = Variable.GetValue("@crmUpdated");
PARAMS.eventDate = Variable.GetValue("@eventDate");
PARAMS.localEventDate = Variable.GetValue("@localEventDate");
PARAMS.localSendDate = Variable.GetValue("@localSendDate");
/* log event and add record to the unsub log */
try {
var props = [
{ Name: "SubscriberID", Value: PARAMS.subscriberID }
, { Name: "JobID", Value: PARAMS.jobID }
, { Name: "ListID", Value: PARAMS.listID }
, { Name: "BatchID", Value: PARAMS.batchID }
, { Name: "Reason", Value: PARAMS.emailName }
];
data = proxy.execute(props, "LogUnsubEvent");
Variable.SetValue("@resultLength", data.Results.length);
Variable.SetValue("@resultStatus", data.Results[0].StatusCode);
/* retrieve request ID for the record if event processed successfully*/
if (data.Results.length > 0 && data.Results[0].StatusCode == "OK") {
PARAMS.logUnsubEventRequestID = data.RequestID;
} else {
if (data.Results.length > 0 && data.Results[0].StatusCode == "Error" && data.Results[0].ErrorCode == 401) {
/* the subscriber is already unsubscribed */
PARAMS.logUnsubEventRequestID = "Alredy Unsubscribed";
} else {
/* some error occurred on API layer - this one is managed */
PARAMS.logUnsubEventRequestID = "API ERROR | " + data.RequestID;
error = true;
}
}
/* add record to the event log DE */
var unsubRecord = {
CustomerKey: PARAMS.unsubLogDEKey
, Properties : [
{ Name: "Contact Key", Value: PARAMS.contactKey }
, { Name: "Email Address", Value: PARAMS.emailAddress }
, { Name: "Publication List", Value: PARAMS.listName }
, { Name: "List ID", Value: PARAMS.listID }
, { Name: "Batch ID", Value: PARAMS.batchID }
, { Name: "Job ID", Value: PARAMS.jobID }
, { Name: "Email ID", Value: PARAMS.emailID }
, { Name: "Email Name", Value: PARAMS.emailName }
, { Name: "Event Date", Value: PARAMS.eventDate }
, { Name: "Local Event Date", Value: PARAMS.localEventDate }
, { Name: "Send Date", Value: PARAMS.sendDate }
, { Name: "Local Send Date", Value: PARAMS.localSendDate }
, { Name: "CRM Updated", Value: PARAMS.crmUpdated }
, { Name: "Preference Field", Value: PARAMS.preferenceField }
, { Name: "LogUnsubEvent Request ID", Value: PARAMS.logUnsubEventRequestID }
]
};
data = proxy.createItem("DataExtensionObject", unsubRecord, null);
} catch (e) {
errorMessage += e.message + " \n<br>\n<br> " + e.description;
Variable.SetValue("@errorMessage", errorMessage);
/* flag that processing error occurred - this one is unmanaged */
error = true;
}
} else {
/* flag error - this is when contact key and email were not supplied */
error = true;
}
/* set the flag for the content to display success/error message */
Variable.SetValue("@error", (error) ? 1 : 0);
</script>
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.