T
Twenty5mo ago
TimM

Code Examples for Workflows

Are there any code examples that anyone has shared for the Code Workflow action? Specifically I'm curious how I should go about accessing the GraphQL API from a Code action. I'm very familiar with GraphQL from having worked a lot with Shopify, but I'm not so familiar with how to use it in this environment. For instance, should I just hardcode my API key into the code? Side note: I can access quite a few useful variables via process.env, even though shows an error in the code editor.
27 Replies
ɃØĦɆᵾS
ɃØĦɆᵾS5mo ago
@Baptiste Devessier could you take a look please?
thomast
thomast4mo ago
Hi, there are no code examples for now AFAIK. You will have to hardcode your API key into the code indeed
hilarex
hilarex2mo ago
Just to understand the code block, it's independent of the previous block ? I wanted to fetch records using the "search records", and then apply a small function on the array. There is no way of doing that ?
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
It can be but it's not a requirement, if you want to use the output of previous block, you have to pass it to a function as an argument like here, just make sure you match the type of argument
No description
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
But the best way would be to just use iterator to iterate over all records and pass current object to code node
hilarex
hilarex2mo ago
Ahh perfect, it's working ! But now I cannot use the output of the code block for the next block :
No description
hilarex
hilarex2mo ago
My goal is to get all tasks that are late (dueDate before today), and send an e-mail with the list of the tasks (title) I use that as a function, and it's working when I click "test" :
export const main = async (params: {
tasks: Array<any>;
totalCount: number;
}): Promise<{ message: string }> => {
const { tasks, totalCount } = params;

const message = `You have ${totalCount} tasks:\n` +
tasks.map(task => `- ${task.title} (${task.dueDate})`).join("\n");

return { message };
};
export const main = async (params: {
tasks: Array<any>;
totalCount: number;
}): Promise<{ message: string }> => {
const { tasks, totalCount } = params;

const message = `You have ${totalCount} tasks:\n` +
tasks.map(task => `- ${task.title} (${task.dueDate})`).join("\n");

return { message };
};
martmull
martmull2mo ago
@TimM process.env may contain information about the AWS Lambda environment. What useful variables can we get from it?
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
Return it as an object like return { output: message }; From what I remember, all functions work only on objects so passing a "simple" type like number or string won't work
hilarex
hilarex2mo ago
I managed to make the test function work. The issue is that the input for the test function is a string, and the code block is using an object. So I did a little conversion :
type Task = { title: string; dueDate?: string };

export const main = async (params: {
tasks: string | Task[]; // can be a JSON string or an array
totalCount?: number; // optional; defaults to tasks.length
}): Promise<{ output: string }> => {
let raw: unknown = params.tasks;

if (typeof raw === "string") {
try {
raw = JSON.parse(raw);
} catch {
raw = []; // bad JSON -> empty list
}
}

const tasks: Task[] = Array.isArray(raw) ? (raw as Task[]) : [];
const totalCount = params.totalCount ?? tasks.length;

const list = tasks
.map(t => `- ${t.title}${t.dueDate ? ` (${t.dueDate})` : ""}`)
.join("\n");

const message =
`You have ${totalCount} task${totalCount === 1 ? "" : "s"}:\n` +
(list || "- (no tasks)");

return { output: message };
};
type Task = { title: string; dueDate?: string };

export const main = async (params: {
tasks: string | Task[]; // can be a JSON string or an array
totalCount?: number; // optional; defaults to tasks.length
}): Promise<{ output: string }> => {
let raw: unknown = params.tasks;

if (typeof raw === "string") {
try {
raw = JSON.parse(raw);
} catch {
raw = []; // bad JSON -> empty list
}
}

const tasks: Task[] = Array.isArray(raw) ? (raw as Task[]) : [];
const totalCount = params.totalCount ?? tasks.length;

const list = tasks
.map(t => `- ${t.title}${t.dueDate ? ` (${t.dueDate})` : ""}`)
.join("\n");

const message =
`You have ${totalCount} task${totalCount === 1 ? "" : "s"}:\n` +
(list || "- (no tasks)");

return { output: message };
};
But now, the output of message, or output in the next block is :
{"type":"doc","content":[{"type":"paragraph","content":[{"type":"variableTag","attrs":{"variable":"You have 15 tasks: ...."}}]}]}
{"type":"doc","content":[{"type":"paragraph","content":[{"type":"variableTag","attrs":{"variable":"You have 15 tasks: ...."}}]}]}
instead of just the text
martmull
martmull2mo ago
You need to click on "Generate Function Output"
hilarex
hilarex2mo ago
Yes I have this now
No description
hilarex
hilarex2mo ago
But the mail I receive is :
{"type":"doc","content":[{"type":"paragraph","content":[{"type":"variableTag","attrs":{"variable":"You have 15 tasks: ...."}}]}]}
{"type":"doc","content":[{"type":"paragraph","content":[{"type":"variableTag","attrs":{"variable":"You have 15 tasks: ...."}}]}]}
martmull
martmull2mo ago
It will make you run the function in the test tab, and next steps will be able to infer return type by infering the test result
hilarex
hilarex2mo ago
it would be nice to be able to have the same input in the test tab as in the code tab. Because in "code" we can an Array, but in "test" it's always a string
martmull
martmull2mo ago
I read export const main = async (params: { tasks: string | Task[]; // can be a JSON string or an array totalCount?: number; // optional; defaults to tasks.length }) Why tasks is string or task[] ?
hilarex
hilarex2mo ago
to handle the test tab
martmull
martmull2mo ago
ok thats indeed an issue, let me check ok ok indeed @thomast when do we plan to have properly typed inputs in workflow steps?
hilarex
hilarex2mo ago
and any idea why the output is a full object instead of just my serverless output string ?
martmull
martmull2mo ago
Ineed there is an issue with body formatting, email step receives the proper input but email body is wrong
No description
No description
martmull
martmull2mo ago
@hilarex you have spotted an issue thanks, we will fix that
martmull
martmull2mo ago
@thomast looks like the body content in send email step is wrong wdyt?
No description
hilarex
hilarex2mo ago
glad I could help !
martmull
martmull2mo ago
@Félix does it ring a bell ?
thomast
thomast2mo ago
the recent refacto of the body input may be the root cause https://github.com/twentyhq/twenty/pull/15157. Did not investigate deeper though
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
hilarex
hilarex3d ago
Hello, any update on this issue ? I'm on version v1.11.5

Did you find this page helpful?