I need help with my code snippet. It's part of an automation that first of all use a query to get the emails from contacts that I need to update in Salesforce CRM. Next to get the emails, I execute a Script activity that calls a Code Snippet in Content Builder. The Script only works when the email from the query has one suscription to update. If the email has more than one suscription asociated, the script fails. Could you give some help with this?

I did some test and I think the problem is with the function "UpdateSingleSalesforceObject" because if I execute the code in a landing page repeatedly finally I get that all my records had been updated. But I need to do this repeatedly, and obviously in a Script activity I can't do this. So, anyone has any idea about the solution?

Thanks for all, this is the code:

<script runat="server">


var @rows, @rowCount

set @rows = LookupRows('MyDataExtension','Motivo_baja', 'Baja mediante el servidor de correo')
set @rowCount = rowcount(@rows)

if @rowCount > 0 then
   var @row, @emailAddress, @status, @dateUnsubscribed, @rsoSuscripciones, @suscripcionesCount
   for @I=1 to @rowCount do   
      set @row = row(@rows, @I)
      set @emailAddress = field(@row,"emailAddress")
      set @subkey = field(@row,"Subscriberkey")
      set @status = 'unsubscribed'
      set @dateUnsubscribed = FormatDate(field(@row,"UnsubDate"),"iso")
     set @rsoSuscripciones = RetrieveSalesforceObjects(
        "email__c", "=", @emailaddress,
     set @suscripcionesCount = RowCount(@rsoSuscripciones)       
 if @suscripcionesCount > 0 then
       var @suscripcionesRow, @idSuscripcion, @updateSF, @unsubscribedError,@rowsSub, @rowCountSub
      for @J=1 to @suscripcionesCount do
         set @suscripcionesRow = Row(@rsoSuscripciones, @j)
         set @idSuscripcion = Field(@suscripcionesRow, 'Id')
         set @updateSF =  UpdateSingleSalesforceObject('suscriptionObjectinSF', @idSuscripcion, "suscrito__c", "false", "fechaBaja__c",   @dateUnsubscribed, "motivoBaja__c", @motivoBaja)

  next @j
   UPDATEDE('LogDataExtension', 2, 'EmailAddress', @emailAddress, 'procesado', 'false', 'procesado', 'true')
  next @I

<script runat="server">
catch(e) {
 Variable.SetValue("errorMessage", Stringify(e.description));
  • 2
    What exactly happens, when "the script fails"? Silently? Error message? Oct 11, 2022 at 6:52
  • 2
    You can't use Ampscript like you did, in a Script Activity. However, following this approach can help you: Using AMPscript in a Script Activity Oct 11, 2022 at 7:24
  • @FelixvanHove the Script gives this error in automation studio: "A runtime error occurred while the SSJS Activity executed the SSJS script. Please review the SSJS activity to ensure that errors are handled properly." Support says that is an error in the code. But they can't give me more information.
    – pvpolo
    Oct 11, 2022 at 8:17
  • @LukasLunow I don't using Ampscript in the Script Activity. My Script has a call to the code snippet that is using the Ampscript. In my Script there is this code: <script runat="server"> var ampscriptCode = Platform.Function.ContentBlockByID("XXXX"); </script>
    – pvpolo
    Oct 11, 2022 at 8:19
  • @pvpolo the @motivoBaja variable is not set anywhere. Not sure if this is what is causing the problem, because it should also fail if you run it on a CP..
    – zuzannamj
    Oct 11, 2022 at 9:08


You must log in to answer this question.

Browse other questions tagged .