Google Form API – Error when Create, Update Item

Issue

I try to update my existing form using google form API. I filled location field in request but still received error message from server create_item.location.index is invalid or was not provided

Create Request Code

func UpdateForm(formId string, form *forms.Form) *forms.Form {
   var requestElements []*forms.Request
   // Update form info
   requestElements = append(requestElements, &forms.Request{
      UpdateFormInfo: &forms.UpdateFormInfoRequest{
         Info:       form.Info,
         UpdateMask: "*",
      },
   })
   // Add items
   for i, item := range form.Items {
      element := &forms.Request{
         CreateItem: &forms.CreateItemRequest{
            Item:     item,
            Location: &forms.Location{Index: int64(i)},
         },
      }
      requestElements = append(requestElements, element)
   }

   request := forms.BatchUpdateFormRequest{
      IncludeFormInResponse: true,
      Requests:              requestElements,
   }

   response, err := formService.Forms.
      BatchUpdate(formId, &request).
      Context(context.TODO()).
      Do()
   if err != nil {
      panic(err)
   }

   return response.Form
}

Note: I’m using form api v1

Solution

Finally i found the issue. When add a new item into form we must start with index 0, but 0 is default value of int in protobuf so it will be ignore when request send.
Solution: Force send field Index

// Add items
for i, item := range form.Items {
    element := &forms.Request{
        CreateItem: &forms.CreateItemRequest{
            Item: item,
            Location: &forms.Location{
                Index:           int64(i),
                ForceSendFields: []string{"Index"},
            },
        },
    }
    requestElements = append(requestElements, element)
}

Answered By – Đỗ Xuân Anh

Answer Checked By – Marilyn (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.