Error message: Cannot read properties of null (reading 'indexOf')

534 views
Skip to first unread message

Daan Gröniger

unread,
Apr 11, 2023, 8:20:50 AM4/11/23
to Google Ads Scripts Forum
I want to change our delivery times in our ad texts using a script. The following script was made by ChatGPT. 

function main() {
  var today = new Date();
  var dayOfWeek = today.getDay();
  var adsIterator = AdsApp.ads().get();
  while (adsIterator.hasNext()) {
    var ad = adsIterator.next();
    var adText = ad.getHeadline();
    if (dayOfWeek == 0 && adText.indexOf("Voor 22:00 besteld, morgen in huis") > -1) {
      adText = adText.replace("Voor 22:00 besteld, morgen in huis", "Voor 15:00 besteld, morgen in huis");
      ad.setHeadline(adText);
    } else if (dayOfWeek == 6 && adText.indexOf("Voor 22:00 besteld, morgen in huis") > -1) {
      adText = adText.replace("Voor 22:00 besteld, morgen in huis", "Vandaag besteld, maandag in huis");
      ad.setHeadline(adText);
    }
  }
}


Now I get the following error message, but I do not know how to fix it, and neither does ChatGPT... 

TypeError: Cannot read properties of null (reading 'indexOf') at main (Code:8:34) at Object.<anonymous> (adsapp_compiled:19646:54)


 Anybody who could help me out? Thanks in advance!  

Google Ads Scripts Forum

unread,
Apr 12, 2023, 3:58:27 AM4/12/23
to Google Ads Scripts Forum

Reposting the last inquiry (https://groups.google.com/g/adwords-scripts/c/nvuruPMn2tE) from the forum as it wasn't routed to our support queue.


Regards,

Google Ads Scripts Team

Google Ads Scripts Forum

unread,
Apr 12, 2023, 6:41:37 AM4/12/23
to Google Ads Scripts Forum

Hi,


Thank you for raising your concern to the Google Ads Scripts forum.


For us to check further, kindly provide the following information:

  • Google Ads account MCID

  • Name of the script on your account


Looking forward to your response.

 

Regards,

Google Ads Scripts Team

Daan Gröniger

unread,
Apr 13, 2023, 3:44:27 PM4/13/23
to Google Ads Scripts Forum
Hello,

I'm not sure if we have an MCID but our normal account ID is 932-432-4625
Name of the script: "Levertijden aanpassen"

Kind regards,
Daan Gröniger

Op wo 12 apr 2023 om 12:41 schreef Google Ads Scripts Forum <adwords...@googlegroups.com>:
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to a topic in the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/adwords-scripts/nvuruPMn2tE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scrip...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/e0285d53-e897-4cce-ad75-395b6114c5bbn%40googlegroups.com.

Google Ads Scripts Forum

unread,
Apr 18, 2023, 6:20:31 AM4/18/23
to Google Ads Scripts Forum

Reposting the last inquiry (https://groups.google.com/g/adwords-scripts/c/nvuruPMn2tE) from the forum as it wasn't routed to our support queue.

Regards,
Google Ads Scripts Team


Google Ads Scripts Forum Advisor

unread,
Apr 18, 2023, 8:53:03 AM4/18/23
to adwords...@googlegroups.com

Hi Daan,

 

Please excuse us for only getting back now as your message failed to be routed to our support queue.

 

I was able to check the script in the account and investigate further. I've observed that, upon retrieving the ads from the account and using the method getHeadline(), it appears that the ads in the account have no headlines as I was able to only retrieve a null value. This is likely why the error was encountered. The getHeadline() method would return a null value if the ad is not a text ad.

 

With this, I would recommend placing a condition which would filter specifically on a TEXT_AD or placing a condition wherein your script would execute when getHeadline() does not return a null value.

 

Lastly, I'm afraid that there isn't a method upon accessing an ad which lets you set it's headline using Google Ads Scripts methods. If you are building an ad, then you can supply a headline. You can check our documentation for the available ad builders currently for Google Ads Scripts.

 

Let us know if you have any clarifications.

 

Relevant links:

 

Best regards,

 

Google Logo Google Ads Scripts Team


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