0

I am trying to Create Unique Ticket no. after each Form submission but getting following error.

TypeError: Cannot read property "range" from undefined. (line 3, file "Code")

Here is the Complete Script.

function myFunction() {
  // Generates a unique ticket number for every form submission.
  function onFormSubmit(e) {
    var targetCell = e.range.offset(0, e.range.getNumColumns(), 1, 1);

    // Get a public lock on this script, because we're about to modify a shared resource.
    var lock = LockService.getPublicLock();
    // Wait for up to 30 seconds for other processes to finish.
    lock.waitLock(30000);

    var ticketNumber = Number(ScriptProperties.getProperty('lastTicketNumber')) + 1;
    ScriptProperties.setProperty('lastTicketNumber', ticketNumber);

    // Release the lock so that other processes can continue.
    lock.releaseLock();

    targetCell.setValue(ticketNumber);
  }
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275

1 Answers1

0

You are receiving that error because the event object, e, isn't defined when you've called onFormSubmit(e). This is typical when you are debugging, and have invoked a parameterized function from the editor/debugger.

For your testing, simulate the event using the technique from How can I test a trigger function in GAS?

(Did you intend to wrap onFormSubmit(e) inside myFunction()? I would think not.)

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275