Issue with recurring Window Alerts in Google Apps Script

I'm currently facing an issue with an alert appearing multiple times whenever a function is executed within a Google Apps Script written for a spreadsheet. I believe I've identified the root cause of the problem, but I'm uncertain about the correct solution... The issue seems to stem from having the alert placed inside a for loop, resulting in multiple alerts based on the data structure's length. I'm struggling to figure out how to structure it so that the alert displays only once without removing it from the "else if" loop.

Just to provide some context on the code, it's essentially going through a spreadsheet, searching for values based on the opportunityID variable, and updating the row's values accordingly. The update script requires all fields to be filled in to successfully run.

Any assistance on this matter would be greatly appreciated!

Feel free to ask if you need any further details.

Here's a snippet of my code:


function updateOpportunity() {

  // Get active spreadsheets and sheets
  var updateSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Search & Create New Records');
  var OppsAndContracts = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Opportunities & Contracts');
  var opportunityUpdateCopy = updateSheet.getRange('A8:P8').getValues();
  Logger.log(opportunityUpdateCopy);
  var ui = SpreadsheetApp.getUi();

  // Set variables to check for empty values
  var OppID = updateSheet.getRange("H14");
  var OpportunityName = updateSheet.getRange("H15");
  var AssociatedAccountID = updateSheet.getRange("H16");
  var AssociatedAccountName = updateSheet.getRange("H17");
  var OpportunityOwner = updateSheet.getRange("H18");
  var LeadSource = updateSheet.getRange("H19");
  var Type = updateSheet.getRange("H20");
  var CloseDate = updateSheet.getRange("H21");
  var Amount = updateSheet.getRange("H22");
  var ProposalOwner = updateSheet.getRange("H23");
  var Stage = updateSheet.getRange("H24");
  var AeroServicesProducts = updateSheet.getRange("H25");
  var MechServicesProducts = updateSheet.getRange("H26");
  var ProjectStatus = updateSheet.getRange("H27");
  var ProposalNumber = updateSheet.getRange("H28");
  var ContractNumber = updateSheet.getRange("H29");

  // Search for Opportunities using OpportunityID
  var last = OppsAndContracts.getLastRow();
  var data = OppsAndContracts.getRange(1, 1, last, 16).getValues();
  var opportunityID = updateSheet.getRange("A8").getValue();
  Logger.log(opportunityID);

  for (nn = 0; nn < data.length; ++nn) {

    if (OppID.isBlank() || OpportunityName.isBlank() || AssociatedAccountID.isBlank() || AssociatedAccountName.isBlank()
    || OpportunityOwner.isBlank() || LeadSource.isBlank() || Type.isBlank() || CloseDate.isBlank() || Amount.isBlank() || ProposalOwner.isBlank()
    || Stage.isBlank() || AeroServicesProducts.isBlank() || MechServicesProducts.isBlank() || ProjectStatus.isBlank() || ProposalNumber.isBlank()
    || ContractNumber.isBlank()) {
      ui.alert("You must fill in all fields to update an Opportunity");
    } 

    else if (data[nn][0] == opportunityID) {
      OppsAndContracts.getRange(nn + 1, 1, 1, 16).setValues(opportunityUpdateCopy);
    }
  }
}

Answer №1

When you have the if statement inside the for loop checking for blank cells, it gets executed data.length times. Since the results of those checks remain the same, you end up getting repeated alerts.

A better approach would be to place the if statement around the for loop instead of inside it.

if (OppID.isBlank() || OpportunityName.isBlank() || AssociatedAccountID.isBlank() || AssociatedAccountName.isBlank() ||
  OpportunityOwner.isBlank() || LeadSource.isBlank() || Type.isBlank() || CloseDate.isBlank() || Amount.isBlank() || ProposalOwner.isBlank() ||
  Stage.isBlank() || AeroServicesProducts.isBlank() || MechServicesProducts.isBlank() || ProjectStatus.isBlank() || ProposalNumber.isBlank() ||
  ContractNumber.isBlank()) {
  ui.alert("You must fill in all fields to update an Opportunity");
} else {
  for (nn = 0; nn < data.length; ++nn) {
    if (data[nn][0] == opportunityID) {
      OppsAndContracts.getRange(nn + 1, 1, 1, 16).setValues(opportunityUpdateCopy);
    }
  }
}

Answer №2

If you encounter a situation where you need to exit a loop prematurely after an alert is triggered, you can utilize the break statement within a for loop. Here's an example:

for(i=0;i<array.length;++i){
    if (condition1 || condition2 || condition3
    || condition4 || condition5 || condition6 || condition7 || condition8 || condition9 || condition10
    || condition11 || condition12 || condition13 || condition14 || condition15 || condition16){
      ui.alert("You must fill in all fields to update an item");
      break;
     }


    else if (array[i][0]==itemID) {
      updateValuesBasedOnID();}
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with scrollTop not functioning when using onclick on an <a> tag within a div

After various attempts and research online, I am still experiencing erratic scrolling behavior with the scrollTop function. My goal is to scroll within a specific div when clicking on links. The content of my div: <div id="test" style="height:400px; o ...

Top method for extracting mesh vertices in three.js

Being new to Three.js, I may not be approaching this in the most optimal way, I start by creating geometry like this: const geometry = new THREE.PlaneBufferGeometry(10,0); Next, I apply a rotation: geometry.applyMatrix( new THREE.Matrix4().makeRotation ...

Highlight the active menu item using jQuery

Check out my menu example here: http://jsfiddle.net/hu5x3hL1/3/ Here is the HTML code: <ul id="menu" class="sidebar"> <li> <a href="#" class="clickme">Menu</a> <ul id="menu1"> <li><a class="dropdown-clas ...

Exploring Passportjs Callbacks and parsing arguments

I'm struggling to grasp the concept behind the custom callback in Passport.js. I'm not sure why it requires (req, res, next) at the end. Shouldn't these values be obtained from closure? app.get('/login', function(req, res, next) { ...

Assign a value to a HubSpot hidden form field by utilizing Javascript

I have a snippet of JavaScript code placed in the head section of my website: <!-- Form ID Setter --> <script> document.querySelector('input[name=form_submission_id]').value = new Date(); </script> My objective is to automat ...

Ways to automatically close browser tab upon successful submission of a form

I have an old file that has been updated. One of the requirements for this file/project modification is to have the current browser window close when the user clicks OK to submit the form. I am curious if this can be done using plain/vanilla JavaScript? ...

The real-time updates on an Angular 2 website can be seen across multiple devices simultaneously

Just getting started with Angular 2 and ran into an interesting issue. When setting up my website, NPM defaults the server to http://localhost:3000. To test the site on another computer, I tried accessing it using my IP address http://10.x.x.x:3000 and eve ...

How do I remove all elements from the Canvas in R3F?

I need a way to clear all models from the Canvas with just one click and then switch over to a new Canvas. I want to make sure that all items are removed from memory before making the change. Is there a way to accomplish this? return ( <div clas ...

Struggling to update local state with response data when utilizing hooks in React

I am a beginner using Functional components and encountering an issue with setting the response from an API to a local useState variable. Despite receiving the response successfully, the variable remains empty and I am struggling to figure out how to resol ...

Tips for testing the onEnter and resolve functions of a ui-router state

I need help testing an Angular UI Bootstrap modal that is triggered from the onEnter function in the state below: .state("profile.index.edit.services", { url: "/edit/services/:serviceCode", parent:"profile.index", onEnter: ['$ ...

Is using $timeout still considered the most efficient method for waiting on an Angular directive template to load?

When it comes to waiting for a directive's template to render, our team has been following the approach of enclosing our DOM manipulation code in a $timeout within the directive's link function. This method was commonly used in the past, but I&ap ...

Utilizing nodejs to interact with a web service

Recently diving into Node.js and currently exploring how to utilize services with NodeJS. Seeking guidance on the NodeJS equivalent of the code snippet provided below: $.ajax({ type: "POST", url: "/WebServiceUtility.aspx/CustomOrderService", data: " ...

Expo BarCodeScanner becomes unresponsive (specifically, the camera) upon exiting the application

I am using Expo's BarCodeScanner component within a tab: return ( <View style={{ flex: 1, flexDirection: "column", justifyContent: "flex-end", }} > <BarCodeScanner onBarCodeScanned={s ...

Utilizing jQuery for AJAX requests on a timed loop

I'm puzzled by a situation involving an AJAX call within an interval. It seems that the code doesn't work as expected, and I'm wondering why. Initially, I had this code snippet which wasn't functioning properly: setInterval($.ajax({ ...

Utilizing Media Queries with Dynamic Vue Properties

On one of my website pages, I always have a Div element with a Background Image that changes based on Media Queries (for example, I fetch a smaller resolution from my CDN on mobile phones). However, since I retrieve the Image URL on Page Load, I need to s ...

Node.js Conditional Logic using Javascript

Can a check be implemented for the following scenario? A continuous loop of numbers, such as 1 2 3 4, is being sent to the server. However, I only want each number to be accepted once. Here is my current approach. I believe I am missing one additional c ...

Testing actual HTTP requests in unit and integration tests with AngularJS

Attempting a request that was not mocked using $httpBackend.when in an Angular 1.x unit/integration test will lead to an error: Error: Unexpected request: GET /real-request Is there a way to perform actual HTTP requests with ngMock and the Karma+Jasmin ...

Tips for swapping hosts in Postman and JavaScript

Is there a simple way to allow the front-end and testing teams to easily override the host header to match {tenant}.mydomain.com while working locally? I'm looking for a solution that doesn't involve constant changes. Any ideas on how I can achie ...

Verify that all elements sharing a common class are concealed

I'm facing a simple issue where I have a list with identical class names. When one of them is clicked, it animates out of the container and then redirects you. The list also includes a hide button. My goal is to receive a browser alert when all items ...

There are no leaks displayed in Chrome Dev Tools, however, the Task Manager will show them until Chrome eventually crashes

Do you have a CPU-intensive application that you're working on? Check it out here: https://codepen.io/team/amcharts/pen/47c41af971fe467b8b41f29be7ed1880 It involves a Canvas on which various elements are constantly being drawn. Here's the HTML ...