AdWords
2K members online now
2K members online now
Improve your AdWords performance and boost your ROI, CTR, and Quality Score
Guide Me
star_border
Reply

Cannot retrieve the next object: iterator has reached the end.

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

 Hei ,I have the following problem, if someone can help me.Thanks.

Cannot retrieve the next object: iterator has reached the end.

  var label = AdWordsApp.labels().withCondition("Name = '"+CAMPAIGN_LABEL+"'").get().next();

1 Expert replyverified_user

Re: Cannot retrieve the next object: iterator has reached the end.

Badged Google Partner
# 2
Badged Google Partner
Hi Cristian,

The best place to get help with the API would be the AdWords API Forum, you can find it here: https://groups.google.com/forum/#!forum/adwords-api

Re: Cannot retrieve the next object: iterator has reached the end.

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Well, its a problem more focused on the adwords scripts then api. I posted there but no expert followed with a response until now. Thanks!

Re: Cannot retrieve the next object: iterator has reached the end.

Badged Google Partner
# 4
Badged Google Partner
Oops my bad, scripts aren't my forte but did create an iterator and enclosed the above lines in a while / hasNext loop? (https://google-developers.appspot.com/adwords/scripts/docs/examples/labels?hl=nb)


Ken

Re: Cannot retrieve the next object: iterator has reached the end.

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
var URL_LEVEL = 'Keyword';
var ONLY_ACTIVE = true;
var CAMPAIGN_LABEL = 'VERIFICA';
var STRIP_QUERY_STRING = false;
var WRAPPED_URLS = false;
var OUT_OF_STOCK_TEXT = 'Acest produs nu mai este disponibil.';

function main() {
var alreadyCheckedUrls = {};
var iter = buildSelector().get();
while(iter.hasNext()) {
var entity = iter.next();
var url = entity.getDestinationUrl();
if(alreadyCheckedUrls[url]) {
if(alreadyCheckedUrls[url] === 'out of stock') {
entity.applyLabel('STOC EPUIZAT');
} else {
entity.applyLabel('Stoc OK');
}
} else {
var htmlCode;
try {
htmlCode = UrlFetchApp.fetch(url).getContentText();
} catch(e) {
Logger.log('There was an issue checking:'+url+', Skipping.');
continue;
}
if(htmlCode.indexOf(OUT_OF_STOCK_TEXT) >= 0) {
alreadyCheckedUrls[url] = 'out of stock';
entity.applyLabel('STOC EPUIZAT');
} else {
alreadyCheckedUrls[url] = 'in stock';
entity.applyLabel('Stoc OK');
}
}
Logger.log('Url: '+url+' is '+alreadyCheckedUrls[url]);
}
}


function buildSelector() {
var selector = (URL_LEVEL === 'Keyword') ? AdWordsApp.ads() : AdWordsApp.keywords();
selector = selector.withCondition('CampaignStatus != DELETED').withCondition('AdGroupStatus != DELETED');
if(ONLY_ACTIVE) {
selector = selector.withCondition('CampaignStatus = ENABLED').withCondition('Status = ENABLED');
if(URL_LEVEL !== 'Ad') {
selector = selector.withCondition('AdGroupStatus = ENABLED');
}
}
if(CAMPAIGN_LABEL) {
var label = AdWordsApp.labels().withCondition("Name = '"+CAMPAIGN_LABEL+"'").get().next();
var campIter = label.campaigns().get();
var campaignNames = [];
while(campIter.hasNext()) {
campaignNames.push(campIter.next().getName());
}
selector = selector.withCondition("CampaignName IN ['"+campaignNames.join("','")+"']");
}
return selector;
}

So,this is the whole code. I wanted to pause keywords with destination url containing "out of stock" products.
Thanks!

Re: Cannot retrieve the next object: iterator has reached the end.

Top Contributor
# 6
Top Contributor

Hi Cristian, is this your own script you've developed or one from the library?

 

As you may know, the error you're seeing comes when you try to fetch the next object in an iterator and there are no more left to choose.  The most common reason for seeing this is when the script does not include a check for the number of elements in the iterator and/or whether the next item exists.


Typically the best solution is not to iterate directly but to do so within a conditional loop.  So, for example, you might have the following code:

 

var campaignsIter = AdWordsApp.Campaigns.get();

while(campaignIter.hasNext()) {

  thisCampaign = campaignIter.next();

  //action code here

}

 

This will ensure the code in the action bit only happens when there is a "next".

 

In your specific case though, I'm not sure what you're trying to do - Labels are identified by their text so your line as it stands doesn't really do anything - if it worked it would simply put the same value into your var label as you already have in CAMPAIGN_LABEL.  In other words, you're asking for something you already have - the text of the label.

 

Can you explain what you want to do?


Jon

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Cannot retrieve the next object: iterator has reached the end.

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭
So i want to iterate through all the keyword urls,check if the urls that corespond to a landing page contain the " in or out of stock" for various products. If the landing page has the sintagm "out of stock" label it corespondingly and pause the keyword. After that send an email to an adress that the url of the keyword has been paused because the landing page for the product is out of stock. Something like that. I tried to use the following script and give it a little change (http://www.freeadwordsscripts.com/2013/10/disable-ads-and-keywords-for-out-of.html) Thanks!

Re: Cannot retrieve the next object: iterator has reached the end.

Top Contributor
# 8
Top Contributor

Hi Cristian, we cross-posted!

 

It looks to me like you're using the wrong selector.  If I'm reading your code correctly, you want the buildSelector function to return a formatted list of campaign names, and the better way to do this is to use LabelNames.

 

var campIter = AdWordsApp.campaigns()

  .withCondition("LabelNames CONTAINS_ANY ['" + CAMPAIGN_LABEL + "'")

  .get();

while (campIter.hasNext()) {

 

and so on.

 

Try that.

 

Jon

 

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Cannot retrieve the next object: iterator has reached the end.

Visitor ✭ ✭ ✭
# 9
Visitor ✭ ✭ ✭
Wasn't my intention! I have all my respect for the community and its rules!

So it seems that the problem persists!
I receive this error regarding the label variable:
One of the conditions in the query is invalid.

Thanks!

Re: Cannot retrieve the next object: iterator has reached the end.

Top Contributor
# 10
Top Contributor

Sorry Cristian, my bad, I dashed the code off without testing it, I'm missing a closing ] in the condition.  It should be:

 

var campIter = AdWordsApp.campaigns()

  .withCondition("LabelNames CONTAINS_ANY ['" + CAMPAIGN_LABEL + "']")

  .get();

 

Not sure what you meant about the community guidelines, I can't see anything you've done wrong there!

Jon

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits