0

I have a SSJS script that updates a field on the campaign member object.

I now need to update one more field, which is a datetime field. Thing is, I don't know how to correctly parse the date I have to send to Salesforce. I tried a couple things but it alwyas fails.

Here's the script (I have put a comment "new script line" on what I have added to the functioning script):

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

var debug = 1;
var records = DataExtension.Init("unsubscribesFromQuickLink");
var data = records.Rows.Retrieve({Property:"updatedFlag",SimpleOperator:"equals",Value:0});
var datalength = data.length;
var dateToUpdate = new Date().toISOString(); // new script line


for (var i = 0 ; i < data.length ; i ++) {
    var sf_fieldUpdateString = [];
    //sf_fieldUpdateString.push('Id');
    //sf_fieldUpdateString.push(data[i].CampaignMemberId);
    sf_fieldUpdateString.push('HasOptOutOfTalent__c');
    sf_fieldUpdateString.push(data[i].HasOptOutOfTalent__c);
    sf_fieldUpdateString.push('LastUpdateHasOptOutOfTalent__c');  //new script line 
    sf_fieldUpdateString.push(dateToUpdate); // new script line

    var updateSFObject = "";
    updateSFObject += "\%\%[ ";
    updateSFObject += "set @salesforceFields = UpdateSingleSalesforceObject('CampaignMember',";
    updateSFObject += "'" + data[i].CampaignMemberId + "','" + sf_fieldUpdateString.join("','") + "'";
    updateSFObject += ") ";
    updateSFObject += "output(concat(@salesforceFields)) ";
    updateSFObject += "]\%\%";


   // var idToUpdate = data[i].SubscriberKey;

    try {

      results = Platform.Function.TreatAsContent(updateSFObject);

     // var processing = records.Rows.Update({flag:'1'}, ['SubscriberKey'], [idToUpdate]);

    } catch (e) {

      if (debug) { Write("<br>updateSF error: " + Stringify(e)); }

    }

}
</script>

The script returns an error when I run it, and it's due to the date I am trying to send to SFDC.

I have a process that updates that exact same field in a cloudpage with ampscript, and this is how I set the date there:

set @LastUpdateDate = Format(DateParse(now(),1), "yyyy-MM-ddTHH:mm:ss.000") 

How do I achieve this in SSJS? How can I correctly send a value to a datetime field of a Salesforce object?

1
  • It has to be ISO format, you can format it using AMPscript, not sure if SSJS has this option
    – zuzannamj
    Jul 28 at 15:57

2 Answers 2

1

I have this in my notes - I don't think you need ms, but you need the offset.

SSJS:

/* date formatting */
var time = Platform.Function.Now();
 
var year = time.getFullYear();
var month = (time.getMonth()+1);
var day = time.getDate();
if(month<10) month = '0'+month;
var h = time.getHours();
var m = time.getMinutes();
var s = time.getSeconds();
var ms = time.getMilliseconds();

if(h<10) h = '0'+h;
if(m<10) m = '0'+m;
if(s<10) s = '0'+s;
var sfmcOffsetFromUTC = "-06:00" 

/* datetime */
var formattedTimestamp = year + "-" + month + '-' + day + 'T' + h + ':' + m + ":" + s + sfmcOffsetFromUTC; 

/* date */
var formattedDate = year + "-" + month + '-' + day;

AMPScript is easier:

FormatDate(Now(),"iso")
1
  • Worked like a charm. Thanks.
    – robruf
    Jul 29 at 12:00
1

As the others have indicated ISO time is required.

Use this ready to go line straight into your code:

var isoTime = Platform.Function.TreatAsContent("\%\%=FormatDate(Now(),'iso')=\%\%");

1
  • Thanks, I went for @Jonas code but I guess this gives the expected result as well. Will definitely keep this in my notes and try it out next time.
    – robruf
    Jul 29 at 12:01

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.