Removing snippets, why doesn't it work?

239 views
Skip to first unread message

Никита Джусь

unread,
Jun 9, 2021, 9:31:02 AM6/9/21
to Google Ads Scripts Forum
I have a code to remove sitelinks and it works

var campaignSitelinkIterator = campaign.extensions (). sitelinks (). get ();
var campaignSitelink = campaignSitelinkIterator.next ();

      campaign.removeSitelink (campaignSitelink); 

But when I try to use it to remove snippets. It throws the error "Cannot retrieve the next object: iterator has reached the end."

My code: 
var campaignSnippetIterator = campaign.extensions (). Snippets (). Get ();
var campaignSnippet = campaignSnippetIterator.next ();

campaign.removeSnippet (campaignSnippet);

What's wrong?

Sigurd Fabrin

unread,
Jun 9, 2021, 11:19:34 AM6/9/21
to Google Ads Scripts Forum
Hi, 

My guess is that you probably don't have snippets on campaign level but rather on account or adGroup level.
And the error is because you assign value to a variable without making sure that the iterator returns any values.

Btw: You should rather get a typeError because of whitespace and wrong casing in your code.

The code below will log where in your account you have snippet extensions:
..and you can modify it to delete the snippets you don't like

function main() { 
  var iter = AdsApp.currentAccount().extensions().snippets().get();
   while (iter.hasNext()) {
     var extension = iter.next();
     Logger.log(extension.getEntityType())
     Logger.log(extension.getValues()+'\n\n')     
   }
  Logger.log('\n\n----\n\n');

  var iter = AdsApp.campaigns()
  .withCondition('CampaignStatus = ENABLED')
  .get();
  while (iter.hasNext()) {
    var entity = iter.next();
    var extIter = entity.extensions().snippets().get();
    while (extIter.hasNext()) {
     var extension = extIter.next();
      Logger.log(extension.getEntityType())
      Logger.log(extension.getCampaign().getName())
      Logger.log(extension.getValues()+'\n\n')
    }
  }
  Logger.log('\n\n----\n\n');

  var iter = AdsApp.adGroups()
  .withCondition('CampaignStatus = ENABLED')
  .get();
  while (iter.hasNext()) {
    var entity = iter.next();
    var extIter = entity.extensions().snippets().get();
    while (extIter.hasNext()) {
     var extension = extIter.next();
      Logger.log(extension.getEntityType())
      Logger.log(extension.getAdGroup().getName())
      Logger.log(extension.getValues()+'\n\n')
    }
  } 
}


 Sigurd

Google Ads Scripts Forum Advisor

unread,
Jun 9, 2021, 11:42:12 PM6/9/21
to adwords...@googlegroups.com
Hello,

Harry here, from the Google Ads Scripts Team. Allow me to also provide you some insights here.

Kindly try to remove the extra white spaces between the methods and use proper casing (Ex. "campaign.extensions ().snippets().get()" ), then try to execute the script again. Otherwise, this will likely result into an error as described by Sigurd. In addition, I can also confirm that you probably don't have snippets on the selected campaign; hence the iterator related error. You may retrieve snippets for a specific campaign like in this link. or refer to the script provided by Sigurd to log snippet attributes when the selector has entities.

@Sigurd: Thanks for the concise guidance and script.

Let us know if you would need further assistance.

Thanks,
Google Logo
Harry Cliford Rivera
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2I1PMH:ref
Reply all
Reply to author
Forward
0 new messages