0

I'm trying to update SF object (Lead) with 3 values in Script Activity in Automation Studio and also logging errors into DE but my code constantly fails. I added Catch/Try blocks but it didn't help and of course there is no changes on SF end.

Moreover - I got info from Support that I cannot update converted Lead record. Whether the Try/Catch block help me process the code or only detects and log the error?

Code:

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

var sfUpdateString;
var targetDE = DataExtension.Init('Contact_Deletion_Lead_UAT').Rows.Retrieve();
 
   try{ 
   
  for (var i = 0; i < targetDE.length; i++)
  {
  
    var Field1 = targetDE[i].ID;
    var Field2 = targetDE[i].EmailBouncedReason;
    var Field3 = targetDE[i].EmailBouncedDate;
    
var sfObj = "Lead";

sfUpdateString = '';
sfUpdateString = '%'+'%[SET @uso = UpdateSingleSalesforceObject("'+ sfObj +'","'+Field1+'"';
sfUpdateString += ',"EmailBouncedReason","'+Field2+'","RFMC__c","True","EmailBouncedDate", FormatDate("' + Field3 + '", "iso"))]%'+'%';
Platform.Function.TreatAsContent(sfUpdateString);
Write(Platform.Variable.GetValue("@uso"));
  }

catch(e)
{
     var logDE = DataExtension.Init("DeletionErrorLog");
     var errorMsg = Stringify(e.message);
     var errorDsp = Stringify(e.description);
     logDE.Rows.Add({Id:Field1,Message:errorMsg, Description:errorDsp});
}

}

2 Answers 2

1

Your code has some small typos - especially the placement of the try-catch brackets could be the reason for your code failing, which is unconnected to your systems' setups.

See comments here:

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

var sfUpdateString;
var targetDE = DataExtension.Init('Contact_Deletion_Lead_UAT').Rows.Retrieve();
 
try { 
   
  for (var i = 0; i < targetDE.length; i++) {
  
    var Field1 = targetDE[i].ID;
    var Field2 = targetDE[i].EmailBouncedReason;
    var Field3 = targetDE[i].EmailBouncedDate;
    
    var sfObj = "Lead";

    sfUpdateString = '';
    sfUpdateString = '%'+'%[SET @uso = UpdateSingleSalesforceObject("'+ sfObj +'","'+Field1+'"';
    sfUpdateString += ',"EmailBouncedReason","'+Field2+'","RFMC__c","True","EmailBouncedDate", FormatDate("' + Field3 + '", "iso"))]%'+'%';
    Platform.Function.TreatAsContent(sfUpdateString);
    Write(Variable.GetValue("@uso")); // removed "platform" - Variable.GetValue is not a Platform function.
    // https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_utilitiesVariableGetValue.html
  }
} /* moved bracket to here... */
catch(e) {
  var logDE = DataExtension.Init("DeletionErrorLog");
  var errorMsg = Stringify(e.message);
  var errorDsp = Stringify(e.description);
  logDE.Rows.Add({Id:Field1,Message:errorMsg, Description:errorDsp});
}

/* ...from here */
/* added closing tag */
</script>

Small comment - I do not see an error, but a "remark":

I am not a fan of the style of writing the AMPScript as a "sliced" SET statement with treatAsContent, but that is likely personal taste. I find it hard to read, but it does look correct.

What I would do is entering the plain AMPScript into a Code Snippet ContentBlock and calling it via "Platform.Function.ContentBlockBykey("myAMPScript"). That makes the AMPscript itself more straightforward, but of course you will have it outside of the code proper. Which I admit also isn't super practical, but still I'd prefer plain to "sliced" AMPScript for debugging.

3
  • thanks you for your support and useful comments! I updated the script and run. No more errors but the code doesn't work. No updates on in SF or error logged into DE. I verified maybe there is a mistake in the DE name DE or fields but everything is correct. I wanted to know is updating SF using a pure AMPScript working in general so performed a test in Cloud page using only: SET @SFStatus = UpdateSingleSalesforceObject("Lead","id_of_lead_here","Removed_From_Marketing_Cloud__c","True")"); (in ampscript brakets ofc) and it worked. Something is not working in the code in the script activity.
    – maria
    Commented Oct 17, 2022 at 20:22
  • *The name of field "RFMC__c" not, "Removed_from_Marketing_Cloud".
    – maria
    Commented Oct 17, 2022 at 20:36
  • This is hard to assist from the outside. I can give general advice - you need to isolate the problem in iterations. e.g. If you run the above code without errors or log entry, it seems it's not going to the "catch" section -> so insert another DE logging mechanism in the "try" to confirm. Put one logDE.rows.add before, one after the Update call to see what the code ends up executing and if it runs through. if yes, remove field by field from the update call to see if a specific field blocks the SF update. Commented Oct 18, 2022 at 9:08
0

I got help from SF support Changes:

  1. Changing from DE name to DE External Key
  2. Replacing syntax for variables from var Field1 = targetDE[i].ID; to var Field1 = targetDE[i]["Id"];
  3. Putting try under the loop
  4. Adding and defining variable for error logging errorId = targetDE[i]["Id"];
  5. I think the game changer for replacing ID with Id (since SSJS is case-sensitive) because Id is the value I used in my DEs
  6. Replaceing var errorDsp = Stringify(e.description); with var errorDsp = Stringify(e)

The final (working!) code:

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

var sfUpdateString;
var targetDE = DataExtension.Init('685AFEE2-C1DD-4D3B-ADA4-E1CAFB998057').Rows.Retrieve();
   
  for (var i = 0; i < targetDE.length; i++) {
  var errorId;
  
  try { 
  
    var Field1 = targetDE[i]["Id"];
    var Field2 = targetDE[i]["EmailBouncedReason"];
    var Field3 = targetDE[i]["EmailBouncedDate"];
    errorId = targetDE[i]["Id"];
    
    var sfObj = "Lead";

    sfUpdateString = '';
    sfUpdateString = '%'+'%[SET @uso = UpdateSingleSalesforceObject("'+ sfObj +'","'+Field1+'"';
    sfUpdateString += ',"EmailBouncedReason","'+Field2+'","Removed_From_Marketing_Cloud__c","True","EmailBouncedDate", FormatDate("' + Field3 + '", "iso"))]%'+'%';
    Platform.Function.TreatAsContent(sfUpdateString);
    Write(Variable.GetValue("@uso")); 
  }
  catch(e) {
  var logDE = DataExtension.Init("B60EF294-9CB5-481C-890A-1C98FE01B5AA");
  var errorMsg = Stringify(e.message);
  var errorDsp = Stringify(e);
  logDE.Rows.Add({Id:errorId,Message:errorMsg, Description:errorDsp});
    }
  } 


</script>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .