What could be causing the error message "userID is not defined" to appear?

Can someone assist me with this script issue? I keep receiving the error message "userID is not defined."

I'm attempting to bulk modify licenses following a tutorial on https://www.youtube.com/watch?v=e4KTAytOeyA

function updateLicenses() {
    var updateSheet = SpreadsheetApp.getActive() .getSheetByName("Update Licenses")
    var data = updateSheet.getRange(2, 1, updateSheet.getLastRow() - 1, 3).getValues()
  var fileArray = [
        ["License Update Status"]
    ]


    // Iterate through each row in the provided Google Sheet and make API calls

    data.forEach(function(item) {
        var status = "License Assigned Successfully"
        try {
            var userId = item[0]
            var apiCall = AdminLicenseManager.LicenseAssignments.patch({
          
            // Enter new license details here
                "productID": "Google-Apps",
                "skuID": "1010020027",
                "userID": userId
           }, "Google-Apps", "1010020028", userId)  // Enter existing/assigned license details here
              
      } catch (e) {
          status = e.message
      }
      fileArray.push([status])
    })
      
    updateSheet.getRange(1, 2, fileArray.length, fileArray[0].length).setValues(fileArray)

    //AdminLicenseManager.LicenseAssignments.patch()
}

Answer №1

After watching the video, have you updated the tap on the Sheet to Update Licenses?

https://i.sstatic.net/5e4PI.png

Are the user IDs still in column A, as demonstrated in the video, or have they been moved to a different column? (If the column for emails has been changed, is the variable data now pointing to that column?)

I checked out the page of the video owner, and it seems that he has updated his code:

function assignLicenses() {

    // Add SKU details here, below ones are for frontline worker
    var productId = "Google-Apps"
    var skuId = 1010020030

    // Get Google Sheet and read the user emails from it, we'll use these to assign licenses

    var assignmentSheet = SpreadsheetApp.getActive().getSheetByName("Assign Frontline Licenses")
    var data = assignmentSheet.getRange(2, 1, assignmentSheet.getLastRow() - 1, 2).getValues()

    // Create an array to store the results from API, instead of appendRow, we'll rather save output to this array, and then write back to sheet at once
    var fileArray = [
        ["License Assignment Status"]
    ]

    // Loop through the google sheet values, and call License API to assign the given license
    data.forEach(function(item) {
        var status = "Licese Assigned Successfully"
        try {
            //var productId = getSkuDetails(item[1])
            var apiCall = AdminLicenseManager.LicenseAssignments.insert({
                "userId": item[0]
            }, productId, skuId)
        }

        // Catch Errors (if any)
        catch (e) {
            var status = e.message
        }

        // Write the output data back to our array
        fileArray.push([status])
    })


    // Write the results from array to our google sheet
    assignmentSheet.getRange(1, 2, fileArray.length, fileArray[0].length).setValues(fileArray)
}

If you have any questions, feel free to ask.

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

Is there a Page Views tracker in sinatra?

Help needed with implementing a page views counter using Sinatra and Ruby. I attempted using the @@ variables, but they keep resetting to zero every time the page is reloaded... Here's an example: Appreciate any advice! ...

Invoking a Directive within another Directive

Feel free to check out this demo on Plunkr. I've set up a basic structure: <body ng-app="myApp"> <div ng-controller="myController"> <parent-directive></parent-directive> <child-directive></child-direc ...

What is the best way to show a message of success once the user has been redirected to the homepage?

Currently, I have a registration form utilizing AJAX and PHP for validation. Error messages can be displayed on the registration page if the user does not correctly fill out the form. Upon successful registration, the user is redirected back to the home pa ...

This TypeScript error occurs when the props are not compatible and cannot be assigned to

Hello fellow Internet dwellers! I am a novice in the world of coding and TypeScript, seeking some assistance here. I am attempting to extract an array of objects from props, transform it into a new object with specific information, and then return it - ho ...

When clicking on an element, all elements will change, not just the specific one

I have been experimenting with basic Jquery and noticed something peculiar. On my page, I have an h1 heading and a generic link. When I click the link, I expect the text to change to "This text has now changed". However, what actually happens is either the ...

Guide to fetching and displaying a complex array API using JavaScript's fetch method

Recently, I've been delving into the world of APIs and making sure I call them correctly. Using the fetch method has been my go-to so far, and here's a snippet of the code where I reference an API: fetch('https://datausa.io/api/data?d ...

Using JavaScript to swap between multiple images can be a powerful tool, but sometimes the wrong image

I'm having an issue with duplicating an image swap function. The problem is that the mouseout event is triggering image 3 on each of the three swaps, when I actually want it to trigger only on the last one. Can anyone provide some guidance on how to m ...

Converting the 'require' call to an import may be a more efficient method when importing package.json in a typescript file

In my current project, I am creating a class where I am directly accessing the package version number like this: const pkg = require('../package.json') export class MyClass() { constructor() { // Set the base version from package.jso ...

What sets Angular Material apart from AngularJS Material when it comes to responsiveness?

I am facing a dilemma in choosing a framework for my application. In my search, I came across Angular Material and AngularJS Material. Although both frameworks are developed by Google (correct me if I'm wrong), they seem to serve the same purpose of r ...

The fancybox's excess content is concealed

I've recently integrated fancybox 2 into my project and have encountered an issue when appending HTML content to the modal. I disabled scrolling, but now any overflowing content is being hidden. Could this be related to the max-height of the modal? Ho ...

The console displays "undefined" when formatting API data

I am attempting to format the data retrieved from an API since there is a lot of unnecessary information. However, when I try to display the formatted data in the console, it always shows as "undefined" or "null." I am importing and calling the fetch API ...

Error encountered: Unexpected token '"', expecting "}". Perspective from React : It is an unfortunate occurrence which h

I'm a newbie when it comes to React and I'm currently following a tutorial. Unfortunately, I encountered an error message while the script was trying to compile: Parsing error: Unexpected token, expected "}" The issue seems to be with the &apos ...

I'm facing an issue with this error message - DiscordAPIError: Unable to send a message that is empty. How can I

What's the solution to this issue? Error: UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message Code: let y = process.openStdin() y.addListener('data', res => { let x = res.toString().trim().split(/ +/g) ...

A method for displaying monthly data in a single row

Hey there! I'm currently dealing with a data list stored in an array. The |arraycontains various objects` each with their own properties such as name, created-at, and more. My goal is to display all users who were created within a specific month in on ...

Vue.js - The error message "$slots have 'el' is null" indicates a problem with the element in

When trying to access the Vuejs $slots instance, I encounter el = null, but type = "div" Here is the template: <slot name="head"> <h1> {{ text }} </h1> </slot> And in the script section: ... ...

Mysterious line appearing beneath alternate rows in Table with border-collapse and border-spacing applied. Unable to pinpoint the culprit property causing the issue

After searching for a solution to add space between the rows in my table, I finally found the answer on Stack Overflow. The implementation seemed to work fine at first glance, but upon closer inspection, I noticed an issue depicted in the screenshot below: ...

Trouble with passing a nodejs variable into a nested function

I've been working on the code below but I'm having trouble getting the inner function .each to successfully call res.write with the results from the MongoDB query. At first, I thought the issue might be related to the variable res not being glob ...

How to send parameters to the jQuery delete button click event handler

Here is the jQuery code I am working with: $('#btnDelete').click( function() {//Do the delete here via jquery post}); In my table, each row has a delete button like this: <a id="btnDelete">Delete</a> I need to pass parameters to t ...

Stop Code Execution || Lock Screen

Is there a way to address the "challenge" I'm facing? I'm an avid gamer who enjoys customizing my game using JavaScript/jQuery with Greasemonkey/Firefox. There are numerous scripts that alter the DOM and input values. In my custom script, I hav ...

I am trying to access the id of the button that was selected, but I am unsure of how to retrieve the id from it. The method of using

I am trying to retrieve the id of a selected button for deletion purposes. However, I am unable to get the id from it using 'this.id'. Is there another method I should be using? Below is the code where I create the button: var deleteEmployer= d ...