T
Twenty2mo ago
saomair

Workflow to delete all the people

Hi, is there any way we can select a company, delete it, and also delete all the people associated with that company? I’m using the latest version that includes the iterator option in the workflow
8 Replies
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
By deleting you mean soft deleting or destroying them all together?
Prastoin
Prastoin2mo ago
Hello there Not natively from the UI but using a workflow yes you could
saomair
saomairOP2mo ago
Yes soft delete How is that possible ?
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
I'll explain in a while after I figure out the workaround
saomair
saomairOP2mo ago
Thanks
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
Found solution, probably over-engineered but working
ɃØĦɆᵾS
ɃØĦɆᵾS2mo ago
To put it simply, you need 2 workflows: one with launch manually trigger with bulk option and one with webhook First workflow should look like on first screenshot, second like on second screenshot, code node in first workflow has code below There's small caveat with SERVERLESS_FUNCTION_EXECUTION_LIMIT_REACHED but that's probably only me testing different things with extensibility
import axios from 'axios';

const TWENTY_API_KEY = '<API_KEY>';
const WEBHOOK_URL = '<WEBHOOK_URL_FROM_SECOND_WORKFLOW>';

export const main = async (params: {
a: string;
}): Promise<String[]> => {
const { a } = params;

const GraphQLquery = `query MyQuery {
companies(filter: {name: {eq: "${a}"}}) {
edges {
node {
people {
edges {
node {
id
}
}
}
}
}
}
}`;

const options = {
method: 'POST',
url: 'http://localhost:3000/graphql',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${TWENTY_API_KEY}`,
},
data: {
query: GraphQLquery,
variables: {},
}
};
let ids: Array<String> = [];
try {
const response = await axios.request(options);
const edges = response.data.data.companies.edges[0].node.people.edges;
for (let i = 0; i < edges.length; i++){
ids.push(edges[i].node.id);
}

const options2 = {
method: 'POST',
url: WEBHOOK_URL,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${TWENTY_API_KEY}`,
},
data: {
data: ids
}
}
const response2 = await axios.request(options2);
return ids;
} catch (error) {
return error;
}
};
import axios from 'axios';

const TWENTY_API_KEY = '<API_KEY>';
const WEBHOOK_URL = '<WEBHOOK_URL_FROM_SECOND_WORKFLOW>';

export const main = async (params: {
a: string;
}): Promise<String[]> => {
const { a } = params;

const GraphQLquery = `query MyQuery {
companies(filter: {name: {eq: "${a}"}}) {
edges {
node {
people {
edges {
node {
id
}
}
}
}
}
}
}`;

const options = {
method: 'POST',
url: 'http://localhost:3000/graphql',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${TWENTY_API_KEY}`,
},
data: {
query: GraphQLquery,
variables: {},
}
};
let ids: Array<String> = [];
try {
const response = await axios.request(options);
const edges = response.data.data.companies.edges[0].node.people.edges;
for (let i = 0; i < edges.length; i++){
ids.push(edges[i].node.id);
}

const options2 = {
method: 'POST',
url: WEBHOOK_URL,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${TWENTY_API_KEY}`,
},
data: {
data: ids
}
}
const response2 = await axios.request(options2);
return ids;
} catch (error) {
return error;
}
};
No description
No description
saomair
saomairOP2mo ago
Appreciate your time let me try and get back to you thanks

Did you find this page helpful?