error: Another task is also trying to change this entity. Please try again later.

597 views
Skip to first unread message

Wilson Carugatti

unread,
Nov 22, 2018, 1:28:23 AM11/22/18
to AdWords Scripts Forum
Hello everyone,

i'm getting this error "Another task is also trying to change this entity. Please try again later." from adwords scripts when aplying multiples labels or pausing many ads sometimes, and I want to know if there is a better way for the script to wait for the changes in the sheet upload to have been applied.

thanks.

function main(){

  var ss2 = SpreadsheetApp.openById("1O4oOarrGuLtKFuvR_flPqMK-fhT0T8tiNTgLXf3ee_A");
  var sh2 = ss2.getSheetByName("SearchPromo");
  var clid = sh2.getRange("C3").getValue();

  var mccAccount = AdWordsApp.currentAccount();
  var childAccount = MccApp.accounts().withIds([clid]).get().next();
  MccApp.select(childAccount);

  SearchPromo();


  /*
  */


  MccApp.select(mccAccount);

}


function SearchPromo(){

  var ss = SpreadsheetApp.openById("1O4oOarrGuLtKFuvR_flPqMK-fhT0T8tiNTgLXf3ee_A");
  var sh = ss.getSheetByName("SearchPromo");
  
  SpreadsheetApp.flush();

  var ctcamp = sh.getRange("G1").getValue();
  var camps = sh.getRange(3,7,ctcamp,2).getValues();

  /*for(var i = 0;i < camps.length;i++){
  Logger.log(camps[i]);
  }*/


  var hl2c = sh.getRange("C5").getValue();
  var hl2v = sh.getRange("C6").getValue();
  var hl3 = sh.getRange("C7").getValue();
  var d1 = sh.getRange("C8").getValue();
  var d2 = sh.getRange("C9").getValue();
  var datainicio = sh.getRange("C14").getValue();
  var rotpromo = sh.getRange("C16").getValue();
  var rotpad = sh.getRange("C17").getValue();

  if(AdWordsApp.labels().withCondition("Name = '"+rotpromo+"'").get().hasNext() == false){
    AdWordsApp.createLabel(rotpromo);
  }

  if(AdWordsApp.labels().withCondition("Name = '"+rotpad+"'").get().hasNext() == false){
    AdWordsApp.createLabel(rotpad);
  }

  var ssap = SpreadsheetApp.openById("1O4oOarrGuLtKFuvR_flPqMK-fhT0T8tiNTgLXf3ee_A");
  var ssh2 = ssap.getSheetByName("Sheet1");

  ssh2.getRange(2,1,ssh2.getLastRow(),ssh2.getLastColumn()).clear();
  var vectot = [];
  var ct1 = 0;
  
  var vecids = [];
  

  for(var i = 0; i < camps.length;i++){

    if(camps[i][1]){
      
      var campnome = camps[i][0];
      var addIte = AdWordsApp.ads().withCondition('Type = EXPANDED_TEXT_AD').withCondition("Status = ENABLED").withCondition("AdGroupStatus = ENABLED").withCondition("CampaignName = '"+campnome+"'").get();
      
      
      while(addIte.hasNext()){

        var add = addIte.next();
        add.applyLabel(rotpad);

        var adex = add.asType().expandedTextAd();

        vectot.push([]);
        vectot[ct1].push(add.urls().getFinalUrl());
        vectot[ct1].push(adex.getHeadlinePart1());
        vectot[ct1].push(adex.getHeadlinePart2());
        vectot[ct1].push(adex.getHeadlinePart3());
        vectot[ct1].push(adex.getDescription1());
        vectot[ct1].push(adex.getDescription2());
        vectot[ct1].push(adex.getCampaign().getName());
        vectot[ct1].push(adex.getAdGroup().getName());
        vectot[ct1].push(adex.getId());
        
        vecids.push(adex.getAdGroup().getId());

        ct1++;

      }
    }
  }

  var vsize = vectot.length;
  
  ssh2.getRange(2,2,vsize,vectot[0].length).setValues(vectot);
  ssh2.getRange(2,1,vsize,1).setValue("Pausado");
  ssh2.getRange(2,11,vsize,1).setValue("set");
  ssh2.getRange(2,12,vsize,1).setValue("Anúncio de texto expandido");

  SpreadsheetApp.flush();


  var ssh3 = ssap.getSheetByName("Output Final");
  var ssh4 = ssap.getSheetByName("Muleta2");
  
  

  Utilities.sleep(3000);
  var denovo = true;

  while(denovo){


    var counta1 = ssh4.getRange("E2").getValue();
    var counta2 = ssh4.getRange("F2").getValue();

    Logger.log(counta1+" "+counta2+" "+vsize);

    if(2*counta1 + vsize == counta2){ 

      denovo = false;
      AdWordsApp.bulkUploads().newFileUpload(ssh3).apply();

    }else{
      
      SpreadsheetApp.flush();
      Utilities.sleep(1000);

    }

  }

  

  
  var adgIdTeste = vecids[vecids.length-1];
  var idTeste = ssh3.getRange(vsize + 1,10).getValue();
  var hl2Teste = ssh3.getRange(ssh3.getLastRow(),4).getValue();
  var adgTeste = ssh3.getRange(ssh3.getLastRow(),9).getValue();
  
  Utilities.sleep(3000);
  
  var aindaEspera = true;
  
  var contaErros = 0;

  while(aindaEspera){
    try{
      Utilities.sleep(5000);
      var testeSel1 = AdWordsApp.ads().withCondition("Status = PAUSED").withIds([[adgIdTeste,idTeste]]).get().totalNumEntities();
      Logger.log(testeSel1);
      
      /*
      var testeSel2 = AdWordsApp.ads().withCondition("AdGroupName CONTAINS '"+adgTeste+"'").withCondition('Type = EXPANDED_TEXT_AD').withCondition("Status = ENABLED").get();
      var teste2Bool = true;
      
      while(testeSel2.hasNext() && teste2Bool){
var add = testeSel2.next();
var hl2Compara = add.asType().expandedTextAd().getHeadlinePart2();
        if(hl2Compara == hl2Teste){
        
          teste2Bool = false;
        
        }
      }*/
      
      //if(testeSel1 > 0 && !teste2Bool){
      
      if(testeSel1 > 0){
        aindaEspera = false;
      }
      
    }catch(e){

      Logger.log(e);
      aindaEspera = true;
      contaErros++;
      
      if(contaErros>1){
      
        throw "2 erros";
      
      }
      

    }
  }
  
  
    
  //Utilities.sleep(10000);
  var contadorAds = counta2 - vsize;
  while(contadorAds >500){
    Logger.log(contadorAds);
    Utilities.sleep(4000);
    contadorAds -= 200;
  }
  
  
  
  Utilities.sleep(5000);
  
  
  

  for(var i = 0; i < camps.length;i++){

    if(camps[i][1]){
      var campnome = camps[i][0];
      var addIte2 = AdWordsApp.ads().withCondition('Type = EXPANDED_TEXT_AD').withCondition("Status = ENABLED").withCondition("AdGroupStatus = ENABLED").withCondition("CampaignName = '"+campnome+"'").get();

      while(addIte2.hasNext()){
        var add2 = addIte2.next();
        add2.applyLabel(rotpromo);

      }
    }
  }

  Utilities.sleep(3000);
  
  
  
  var hoje = new Date();
  if(datainicio != ""){
    if(datainicio>hoje){
      
      var listaPausa = [];
      var listaAtiva = [];
      
      Utilities.sleep(1000);
      var revselpromo = AdWordsApp.ads().withCondition('Type = EXPANDED_TEXT_AD').withCondition("LabelNames CONTAINS_ANY ['"+rotpromo+"']").withCondition("Status = ENABLED").get();
      while(revselpromo.hasNext()){
        var adite = revselpromo.next();
        listaPausa.push(adite);
      }
      
      for (var i = 0; i < listaPausa.length; i++) {
          listaPausa[i].pause();
        }
      
      var revselpad = AdWordsApp.ads().withCondition('Type = EXPANDED_TEXT_AD').withCondition("LabelNames CONTAINS_ANY ['"+rotpad+"']").withCondition("Status = PAUSED").get();
      while(revselpad.hasNext()){
        var adite = revselpad.next();
        listaAtiva.push(adite);
      }
      
      for (var i = 0; i < listaAtiva.length; i++) {
        listaAtiva[i].enable();
      }
      
      
    }
  }
}


Screenshot_label&pausing.png
Screenshot_labels.png

Hiroyuki Miyauchi (AdWords Scripts Team)

unread,
Nov 22, 2018, 3:53:26 AM11/22/18
to AdWords Scripts Forum
Hello Wilson,

Could you please provide your CID and script name as well as access to the spreadsheet being used in the script via Reply privately to author so I can further investigate?

Regards,
Hiroyuki
AdWords Scripts Team
Reply all
Reply to author
Forward
0 new messages