Cannot retrieve the next object: iterator has reached the end. (line 73)

3,242 views
Skip to first unread message

Serena Manzoli

unread,
Mar 22, 2017, 3:47:44 PM3/22/17
to AdWords Scripts Forum
Hello everybody,
I'm trying to run 2 scripts: one that enable a campaign when it rains and another script that pauses it when it's sunny. The "sunny" script works fine, but for the rain one I keep on having this error "Cannot retrieve the next object: iterator has reached the end. (line 73)".

I'm using the Adwords weather script

if (nyWeather.weather.status.summary === 'Rain') {
    var campaign = AdWordsApp.campaigns()
      .withCondition("CampaignName = 'londonweather'")
      .get()
      .next();
  campaign.enable();

Can anybody help?

Thanks 

Trevor

unread,
Mar 22, 2017, 5:28:07 PM3/22/17
to AdWords Scripts Forum
It would imply that the iterator isn't able to find a campaign that fits the conditions you've set (the error message you're receiving occurs when you call .next() on an iterator but there's nothing to retrieve, it's why we generally wrap everything in a while (iterator.hasNext()) loop)

Your CampaignName condition needs to be written using Name instead of CampaignName (when you're using it in a campaign iterator) like:

if (nyWeather.weather.status.summary === 'Rain') {
   var campaign = AdWordsApp.campaigns()
     .withCondition("Name = 'londonweather'")
     .get()
     .next();
 campaign.enable();
}

Here's some more info on campaign selectors/iterators: https://developers.google.com/adwords/scripts/docs/reference/adwordsapp/adwordsapp_campaignselector#withCondition_1

If you need to debug after that point you could always separate out the .next() call from the iterator and log the results of campaignIterator.totalNumEntitities() or campaignIterator.hasNext() (for example) to see whether it's retrieving anything, like:

if (nyWeather.weather.status.summary === 'Rain') {
   var campaignIterator = AdWordsApp.campaigns()
     .withCondition("Name = 'londonweather'")
     .get();

 
Logger.log("Did we retrieve anything? %s | How many things did we retrieve? %s",campaignIterator.hasNext(),campaignIterator.totalNumEntities();
 
var campaign = campaignIterator.next();
   campaign
.enable();
}


Paul Justine de Honor

unread,
Mar 22, 2017, 10:43:07 PM3/22/17
to AdWords Scripts Forum
Thank you Trevor for your answer!

Hi Serena,

Trevor is correct. You are getting the error because you called the next function but there is no item to be retrieved. To avoid this error, you can use the script below that would check first if there is an available item that suffice with your condition using hasNext function. Also, you have to change the filter to Name instead of CampaignName.

if (nyWeather.weather.status.summary === 'Rain') {
   
var campaign = AdWordsApp.campaigns()

     
.withCondition("Name = 'londonweather'")

     
.get();
 
if(campaign.hasNext()){
    campaign
.next().enable();
 
}
}

Additionally, you may check the valid fields for withCondition here.

Thanks,
Paul Justine De Honor
AdWords Scripts Team
Reply all
Reply to author
Forward
0 new messages