Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

applyUserActions AddRecord does not return new record id #1416

Closed
1 of 2 tasks
emanuelegissi opened this issue Feb 3, 2025 · 2 comments
Closed
1 of 2 tasks

applyUserActions AddRecord does not return new record id #1416

emanuelegissi opened this issue Feb 3, 2025 · 2 comments

Comments

@emanuelegissi
Copy link

Describe the current behavior

While developing a custom widget, when calling:

const response = await grist.docApi.applyUserActions([
  ["AddRecord", tableId, null, {}] // Create an empty record
]);

the response is empty.

Steps to reproduce

  1. Create a new custom widget
  2. Insert the following html and js code:
<html>
<head>
  <script src="https://docs.getgrist.com/grist-plugin-api.js"></script>
  <style>
    .div {
      display: flex;
      flex-direction: row;
      gap: 10px;
      padding: 6px;
    }
    .button {
      display: block;
      color: #FFFFFF;
      background-color: #3B82F6;
      border: 1px solid;
      padding: 4px 8px;
      font-size: 14px;
      border-radius: 4px;
      cursor: pointer;
    }
    .button:hover {border: 1px solid #494949;}
    .button:active {background-color: #494949;}
    .warning {background-color: #dd962c;}
    .info {background-color: #11B683;}

  </style>
</head>
<body>
  <div id="container" class="div"></div>
</body>
</html>
grist.ready({ requiredAccess: 'full', allowSelectBy: true});

const tableId = "Authors"; // Insert target table

// Record functions

const addRecord = async (tableId, fs) => {
  if (!tableId) {
    alert(`Empty tableId!`);
    return;
  }
  if (!fs) { fs = {}; }
  try {
    const actions = [["AddRecord", tableId, null, fs]]
    const response = await grist.docApi.applyUserActions(actions);
    const rowId = response[0][0]
    grist.setCursorPos({rowId: rowId});
    return rowId;
  } catch (error) {
    alert(`Cannot create record [${error}]`);
  }
};

// Button functions

const container = document.getElementById("container"); // Button container

const createAddButton = (tableId) => {
  const button = document.createElement("button");
  button.textContent = "+";
  button.title = "Crea nuovo";
  // Ensure the function is called with the parameter when clicked
  button.onclick = () => addRecord(tableId);
  button.className = "button";
  container.appendChild(button);
  return button;
};

// Create new button, when no record available

createAddButton(tableId);

// Listen for changes to the selected record

grist.onRecord((record) => {
  container.replaceChildren();
  createAddButton(tableId);
});
  1. Run the custom widget on any table.

This code fails, because response is empty.

Describe the expected behavior

response[0][0] should return the newly created record id.

Where have you encountered this bug?

Instance information (when self-hosting only)

  • Grist instance:

    • Version:
    • URL (if it's OK for you to share it):
    • Installation mode: docker/kubernetes/...
    • Architecture: single-worker/multi-workers
  • Browser name, version and platforms on which you could reproduce the bug:

  • Link to browser console log if relevant:

  • Link to server log if relevant:

@paulfitz
Copy link
Member

paulfitz commented Feb 4, 2025

Hi @emanuelegissi ! Running your code and writing response to console.log, I see:

{
  "actionNum": 1521,
  "actionHash": "810e85ae4419f7e00db896ce27c84f118f1688fadd9bcbe86651edf6b640a52a",
  "retValues": [
    5
  ],
  "isModification": true
}

So it looks to me that response.retValues[0] is what you are after. Has the format changed? @georgegevoian helped me look around for any obvious changes and we didn't come up with anything, but I'd be interested to know if code that was working for you before has stopped working.

@emanuelegissi
Copy link
Author

Sorry Paul, you are right. My fault, a silly typo. Thank you very much for your help. Closing now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants