I have been struggling a lot for ChatGPT to create the correct code for the stuff I want Grist to do.
It will usually build Python code expecting libraries to be used, or SQL, etc.
For example…
“in Python, how to get the first record of a table?”
CHATGPT: To get the first record of a table in Python, you can use the get
method with no arguments. For example:
my_table = my_base.getTable(‘My Table’)
first_record = my_table.get()
This will return the first record of My Table
. If the table is empty, get
will return None
.
**It’s actually a Trigger formula in a system that works like a database and where I can set the values for the fields in the records. **
But I can also set trigger formulas for the columns in Python.
So this column has a reference to a table called Estagio_Leads. Each time a new value is inserted in the current table, that cell’s trigger formula will trigger and call the first value in the table Estagio_Leads, whose value is “1 - Novo Lead”.
I am not using any PyPI nor other Library.
ChatGPT
To achieve this, you can define a trigger formula for the column that references the Estagio_Leads
table. The trigger formula should call the lookupRecords()
function to get the first record in the Estagio_Leads
table, and then return the id
of that record.
Here’s an example trigger formula that you can use:
first_lead_stage = Estagio_Leads.lookupRecords()[0]
return first_lead_stage.id
This code will get the first record in the Estagio_Leads
table, and then return its id
value. Note that you may need to adjust the table and field names to match your specific setup.
got an error TypeError : ‘RecordSet’ object is not subscriptable
CHATGPT: The error message “TypeError: ‘RecordSet’ object is not subscriptable” usually occurs when you try to access an item in a RecordSet using square brackets, but the RecordSet object doesn’t support that operation. To fix this error, you can try using the get()
method of the RecordSet object to retrieve the first record. Here’s an example:
lead_estagio = Estagio_Leads.get()[0]
In this code, the get()
method is used to retrieve the first record from the Estagio_Leads
table, and then the [0]
index is used to extract the first value from that record.
used the code lead_estagio == Estagio_Leads.get()[0], but got a NameError: name ‘lead_estagio’ is not defined
CHATGPT
It seems like you haven’t defined the variable lead_estagio
before using it. You can define it before using it in the trigger formula. Here’s an example of how to do it:
define variable lead_estagio and set it to the first record in Estagio_Leads
lead_estagio = Estagio_Leads.get()[0]
define the trigger formula for the column
def trigger_formula(record, column):
# set the value of the column to lead_estagio
column.value = lead_estagio
Make sure to replace column
with the actual name of the column in your code.
If I use Estagio_Leads.get()[0] it says usertable has no attribute ‘get’.
what if I try to use the first ID?
…
after some 4 more prompt exchanges I got this
and gave up.
The correct code was MUCH simpler and I got in this forum.
=Estagio_Leads.lookupOne(id=1)
What am I missing on how to ask ChatGPT to give me simple answers that work with GRIST?
I asked it what Prompt should I make for it to give me a simple answer like that one above. This is what it wrote:
“What’s the simplest way to retrieve the first record from a related table called ‘Estagio_Leads’ in a trigger formula in the current table, without using external libraries or complex code?”
However, in a new chat, I used that prompt and the answer was for Excel or GoogleSheets or a relational database in SQL. And when asking
"“What’s the simplest way to retrieve the first record from a related table called ‘Estagio_Leads’ in a trigger formula in the current table, in Python, without using external libraries or complex code?”
The answer was AGAIN quite complicated (and possibly wouldn´t work with GRIST)
import sqlite3
# connect to the database
conn = sqlite3.connect('your_database_name.db')
# create a cursor object
cursor = conn.cursor()
# retrieve the first record from the related table 'Estagio_Leads'
cursor.execute("SELECT * FROM Estagio_Leads LIMIT 1;")
record = cursor.fetchone()
# close the cursor and the database connection
cursor.close()
conn.close()
# print the retrieved record
print(record)