0
Platform.Load("core","1");
var subs = DataExtension.Init('cambio_estado');
var data = subs.Rows.Retrieve();

try {for (var i = 0 ; i < data.length ; i ++) 
      {
        var statusUpdate = subs.Update({EmailAddress: data[i].EmailAddress, SubscriberKey: data[i].SubscriberKey},[Status],["Unsubcribe"]);
          
      } 
    
    } 
catch (e) {action = "error";

}
</script>```
2
  • 1
    What's the context? What are you trying to do in terms of the bigger picture? Is this running in a CloudPage or a Script Activity? Why SSJS and not SQL? Are you getting any errors you can share? What do you get when you Write(Stringify(e)) within your catch block?
    – Macca
    Jul 27 '20 at 23:54
  • Thanks for the support the context is as follows: this is a script activity in a marketing cloud automation I have a data extension with the fields email_address, subscriber_key and Status, the status of these records is active, what I want to do is change the Status to Unsubscribed but when running the automation the script works fine, it does not generate any error but neither does it change the Status of the records. Jul 29 '20 at 12:28
1

First of all, your question is not very well described as Macca pointed out. It is very unclear what you try to do within your code.

Your code tries to retrieve all records (capped at 2500) and update each record where Status is Unsubscribe with the same EmailAddress and SubscriberKey.

This in itself makes no sense and has many flaws and you should re-think your approach.

Regardless of this, your code has issues as well.


Rows.Update

var statusUpdate = subs.Update({EmailAddress: data[i].EmailAddress, SubscriberKey: data[i].SubscriberKey},[Status],["Unsubcribe"]);

As per documentation the DataExtension object itself does not have an Update method. You are missing the Rows method.

var statusUpdate = subs.Rows.Update({EmailAddress: data[i].EmailAddress, SubscriberKey: data[i].SubscriberKey},[Status],["Unsubcribe"]);

Typo

[Status],["Unsubcribe"]

As you are searching for Status is equal to Unsubcribe it is important that your value is an exact match.

Unless your actual value is Unsubcribe you should change this to Unsubscribe


As you have not explained what you try to achieve, I can only guess that you would like to update the status column of the first 2500 records within the DataExtension cambio_estado to Unsubscribe, the following code will do it for you.

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

    var subs = DataExtension.Init('cambio_estado');
    var data = subs.Rows.Retrieve();

    try {
        for (var i=0; i<data.length; i++) {
            var statusUpdate = subs.Rows.Update({Status:"Unsubscribe"}, [EmailAddress, SubscriberKey], [data[i].EmailAddress, data[i].SubscriberKey]);
        }

    } catch (e) {
        Write(Stringify(e));
    }
</script>

If you want to do this for all records exceeding the limit of 2500, you would need to use WSProxy. However, this becomes very inefficient and you should consider to do an import definition inside the automation based on a SQL driven code.

2
  • Thanks for the support the context is as follows: this is a script activity in a cloud marketing automation I have a data extension with the fields email_address, subscriber_key and Status, the status of these records is active, what I want to do is change the Status from active to Unsubscribed but when running the automation the script works fine, it doesn't generate any error but it doesn't change the Status of the records either, try the script I leave but it doesn't do anything Jul 29 '20 at 12:31
  • I achieved it by doing this: ´´´<script runat=server language="JavaScript" executioncontexttype="get"> Platform.Load("Core","1.1"); var TestDE = DataExtension.Init("cambio_estado"); var result = TestDE.Rows.Update({"Status":"active"}, ["Status"],["Unsubscribed"]); Write('<b>Output:</b>'+result); </script>``` but on the condition that the status is "active", but if the status is written differently "Active" I would not update it Jul 29 '20 at 15:02
0

I achieved it by doing this:

<script runat=server language="JavaScript" executioncontexttype="get"> Platform.Load("Core","1.1"); var TestDE = DataExtension.Init("cambio_estado"); var result = TestDE.Rows.Update({"Status":"active"}, ["Status"],"Unsubscribed"]); </script>

but on the condition that the status is "active", but if the status is written differently "Active" I would not update it

3
  • Keep in mind this is limited to 2500 records only and becomes very slow. If you data is between 2500 and 100k records use WSProxy otherwise use file import
    – shd.lux
    Jul 29 '20 at 22:55
  • It would be far more appropriate to do what you're doing with an SQL Query Activity.
    – Macca
    Jul 30 '20 at 1:11
  • Gracias por tu ayuda, lo hice con una consulta sql como mencionaste, ya que debo cambiar el estado de todos los registros que están en esa extensión de datos, la consulta es la siguiente para que le eches un vistazo SELECT subscriber_key,email_address,'Unsubscribed' as Status FROM cambio_estado Jul 31 '20 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.