withSuccessHandler seems to be disabled

1,193 views
Skip to first unread message

Pieter le Roux

unread,
Jul 3, 2022, 1:08:52 PM7/3/22
to Google Apps Script Community
I have a function that calls the withSuccessHandler method but it's not blue as it's supposed to be, it's black like the rest of the line of code. This function is not firing when I click the HTML button so I assume that withSuccessHandler is not enabled for some reason. The script uses a bootstrap package and I'm not sure if that's relevant but I copied it as well below. 

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
<link rel="stylesheet" 
<style>
.narrow
{
  margin-bottom: 0.5rem;
}
</style>
<script>
function SubmitRecord()
{
  document.getElementById("displayReturn").innerHTML = "";
  var first_name = document.getElementById("first_name").value = '';
  google.script.run.withSuccessHandler(returnBack);
  .AddRecord(first_name);
}

This comes from a bootstrap sidebar that I copied from here but as I mentioned, my withSuccessHandler is black where his is blue: https://codewithcurt.com/how-to-create-a-bootstrap-sidebar-form-on-google-sheets/

Also, please note that my sidebar runs on a Google Doc and not a Sheet and I see that a part of the above code refers to stylesheet:

<link rel="stylesheet"

Does this need to be changed to a document version?

Edward Ulle

unread,
Jul 3, 2022, 1:28:49 PM7/3/22
to Google Apps Script Community
First, the <script> is Html so its not highlighted since the editor knows nothing about functions in Html files, it only seems to know about tags.  Second, where is the callback function "returnBack" and there shouldn't be a ";" at the end of that line.  It breaks the code and .AddRecord() is not a part of the google.script.run command line.  Your <script> function is most likely failing because of typos.

Pieter le Roux

unread,
Jul 3, 2022, 1:35:32 PM7/3/22
to Google Apps Script Community
I removed the ";" it still doesn't work though. Here is the returnBack as well:

function SubmitRecord()
{
  document.getElementById("displayReturn").innerHTML = "";
  var first_name = document.getElementById("first_name").value = '';
  google.script.run.withSuccessHandler(returnBack)
  .AddRecord(first_name);
}

function returnBack(stringBack)
{
  document.getElementById("displayReturn").innerHTML = stringBack;
  document.getElementById("first_name").value = '';
  
};

Edward Ulle

unread,
Jul 3, 2022, 1:44:51 PM7/3/22
to Google Apps Script Community
And it looks like the end of “var first_name” is incorrect. Are those single or double quote? And are you sure AddRecord is successful?

Edward Ulle

unread,
Jul 3, 2022, 1:48:19 PM7/3/22
to Google Apps Script Community

And where is element with id “first_name”

Pieter le Roux

unread,
Jul 3, 2022, 1:58:37 PM7/3/22
to Google Apps Script Community
These are single quotes at the end of this line   var first_name = document.getElementById("first_name").value = '';

I can run addRecord function successfully but when I click the button it doesn't give any response.

The element with id "first_name" is included in the rest of the HTML code:

</script>
  </head>
  <body>
  <form>
  <div style="padding: 10px" >
  <div class="form-row" >
  <div class="form-group col-md-6 narrow" >
  <label for="first_name" style="margin-bottom: 0rem" >Agent Email Address</label>
  <input type="email" id="first_name" class="form-control" />
  </div> 
  </div>
  
  <div class="form-row">
  <div class="form-group col-md-6 narrow" >
  <input type="button" value="Submit" onclick="SubmitRecord()" class="btn btn-primary" />
  </div> 
  </div>
  </form>
  <div id="displayReturn" ></div>
  </div>
  
  </body>
</html>

Pieter le Roux

unread,
Jul 3, 2022, 2:01:46 PM7/3/22
to Google Apps Script Community
Message has been deleted

Pieter le Roux

unread,
Jul 3, 2022, 2:47:02 PM7/3/22
to Google Apps Script Community
For some reason the copy works but it only captures the date and not the input into sheets. Here is the AddRecord function:

function AddRecord(first_name) {
  var ss = SpreadsheetApp.openById('1-YSbTK8RrN2JjiOy10hjuFZupeCEEAaepjjNGeCQaEI');
  var dataSheet = ss.getSheetByName("SUBMIT");
  
  
    dataSheet.appendRow([first_name, new Date()]);
    return "<span style=\"font-weight: bold\" >Record Saved</span>";
  
}

Edward Ulle

unread,
Jul 3, 2022, 3:13:19 PM7/3/22
to Google Apps Script Community
You need to correct this line:

var first_name = document.getElementById("first_name").value;

Pieter le Roux

unread,
Jul 4, 2022, 2:30:24 AM7/4/22
to Google Apps Script Community
Thank you so much. I made the change in the submitRecord function as well as returnback function and it works now. Thanks again
Reply all
Reply to author
Forward
0 new messages