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">
Platform.Load("Core","1.1.1");
try{
</script>
%%[
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(
"suscriptionObjectinSF",
"id",
"email__c", "=", @emailaddress,
"suscrito__c","=","true")
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
endif
UPDATEDE('LogDataExtension', 2, 'EmailAddress', @emailAddress, 'procesado', 'false', 'procesado', 'true')
next @I
endif
]%%
<script runat="server">
}
catch(e) {
Variable.SetValue("errorMessage", Stringify(e.description));
}
</script>
@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..