Global variable timestamp not working as expected

8 views
Skip to first unread message

Aaron Weiner

unread,
Jan 24, 2020, 7:10:37 PM1/24/20
to Google Ads Scripts Forum
I'm trying to set a timestamp that will be used at the beginning of my script and at the very end of my script. 

My code looks something like this:

var timestamp = Utilities.formatDate(new Date(), 'America/Chicago', 'yyyy-MM-dd-HH:mm:ss').toString();

function main() {
    Logger.log('Before we start - timestamp = ' + timestamp);

  var accountSelector = MccApp.accounts();
  accountSelector.executeInParallel('processClientAccount','finalCleanUp');
}

function processClientAccount() {


}

function finalCleanUp() {
  Logger.log('My timestamp = ' + timestamp);
}

The problem is that the timestamp at the start is different than the timestamp at the end - why is that happening and what can I do to prevent that?

Thanks and I look forward to your reply.

Aaron

Google Ads Scripts Forum Advisor

unread,
Jan 27, 2020, 4:15:00 AM1/27/20
to adwords-scripts+apn2wqdgfhprxzen...@googlegroups.com, adwords-scripts+apn2wqdgfhprxzen...@googlegroups.co, adwords...@googlegroups.com

Hi Aaron,

 

Thank you for raising this to us.

 

I was able to recreate the scenario you've mentioned. In addition, I was able to match the timestamp (start and end) by commenting out the statement with the executeInParallel method and just calling the finalCleanUp method after the 1st logger statement.

 

As an aside, allow me to raise this with the rest of the team to gather insights regarding on this. I'll get back to you once I receive any updates from them.

 

Regards,

Markie

Google Ads Scripts Team



ref:_00D1U1174p._5001USwyir:ref

Aaron Weiner

unread,
Jan 27, 2020, 8:20:41 AM1/27/20
to Google Ads Scripts Forum
The approach that you described won't work. 

I need this to execute in parallel.

The only way I have been able to solve this is by passing the timestamp from function to function like so:

var timestamp = Utilities.formatDate(new Date(), 'America/Chicago', 'yyyy-MM-dd-HH:mm:ss').toString();

function main() {
    Logger.log('Before we start - timestamp = ' + timestamp);

  var accountSelector = MccApp.accounts();
  accountSelector.executeInParallel('processClientAccount','finalCleanUp',timestamp);
}

function processClientAccount(timestamp) {


  return JSON.stringify({timestamp:timestamp}); 
}

function finalCleanUp(results) {
  if (results.length > 0) {
    var obj = JSON.parse(results[0].getReturnValue());
    
    Logger.log('Cleaning up the mess - timestamp = ' + obj.timestamp);
    
  } 
}

I am doing this to solve the problem but that seems like a lot of extra steps. 

Thanks and I look forward to your reply.

Aaron




Reply all
Reply to author
Forward
0 new messages