Workflow : Count number of objects

Hello, when a "Company" is updated, I want to update the number of Inks (it's a custom object) associated. So I created a workflow with a serverless code but I struggle to access to this "inks" object If I launch this : query MyQuery { companies(filter: {name: {eq: "Diamine"}}) { edges { node { id name inks { countNotEmptyId } } } } } I get this : { "data": { "companies": { "edges": [ { "node": { "id": "6ce1408b-7b4e-444a-9a79-2a4dd8541569", "name": "Diamine", "inks": { "countNotEmptyId": 163 } } } ] } } } Is there a way to do get this "countNotEmptyId" using a serverless function ?
14 Replies
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
Please correct me if I'm wrong: company and inks have relation between each other and you want to find all inks which have relation to company named Diamine?
greg [iero]
greg [iero]OP2mo ago
Exactly ! A company can have many Inks
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
🤔 Let me check something
greg [iero]
greg [iero]OP2mo ago
I want to update a field "ink_count" for every company "updated". I can access company simple objects (name, id..) but I don't know how to access this "inks" object
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
I did this very simplified code
let data = `{
"data": {
"companies": {
"edges": [
{
"node": {
"id": "6ce1408b-7b4e-444a-9a79-2a4dd8541569",
"name": "Diamine",
"inks": {
"countNotEmptyId": 163
}
}
}
]
}
}
}`;
let parsed = JSON.parse(data);
console.log(parsed.data.companies.edges[0].node.inks.countNotEmptyId);
let data = `{
"data": {
"companies": {
"edges": [
{
"node": {
"id": "6ce1408b-7b4e-444a-9a79-2a4dd8541569",
"name": "Diamine",
"inks": {
"countNotEmptyId": 163
}
}
}
]
}
}
}`;
let parsed = JSON.parse(data);
console.log(parsed.data.companies.edges[0].node.inks.countNotEmptyId);
greg [iero]
greg [iero]OP2mo ago
Thanks bob I have no problem using external API, but I try to access this object using the workflow serverless function. Defaut function is : export const main = async (params: { a: string; b: number; }): Promise<object> => { const { a, b } = params; // Rename the parameters and code below with your own logic // This is just an example const message = Hello, input: ${a} and ${b}; return { message }; }; But I don't find how to access company "inks" in this function.
greg [iero]
greg [iero]OP2mo ago
No description
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
Ah, I see, that's not possible
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
To simply put, relation with objects is called nested field and nested fields and filters will be done in Q1 26 https://github.com/twentyhq/core-team-issues/issues/214
GitHub
👁️ Nested Fields · Issue #214 · twentyhq/core-team-issues
About In a database, a &quot;Customer&quot; object might have a relation to an &quot;Order&quot; object. Using nested fields, you can display details of an order (like order date or total amount) d...
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
So for time being, there's no other way than sending a request through API and parsing the data
greg [iero]
greg [iero]OP2mo ago
Oh, ok, I thought as it is a One (Company) to Many (Inks), it was possible to link them togther. As I can access them using API, I don't see why I cannot from serverless code, it's based on the API as well, no?
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
I don't know, @thomast? 😅
thomast
thomast2mo ago
Thank you for the ping @bob! Hi @greg [iero], we do not support one to many relationships in variable picker, only many to one. This is not based on API. What you can do is doing first a code step to perform an api call to twenty to retrieve the inks associated to the company via API. Alternatively you can: - enable workflow iterators in lab - add a find record node, enabling the iterators in lab will remove the limit - retrieve the inks associated to the company but using the find record node
greg [iero]
greg [iero]OP2mo ago
Thanks for the clarification, I think I will use the API then to update the field

Did you find this page helpful?