How to iterate through arrays and bundles

Hello! I have this scenario that interacts with kommo. The purpose of it is to create a new lead and after that look for leads already in kommo with the same value for the phone custom field. This is to avoid duplicates.

I’ve started doing this by using the search leads kommo module. It does return 10 bundles (10 leads). But for each lead I want to check if the phone custom field value is the same as the value in the lead I just created. I just don’t know how to

  1. iterate through the Custom Field Values within each bundle to find the value for the phone custom field (telefone_original in portuguese). For this I know I need to use the custom field Id.

  2. repeat this process for every other bundle (or at least until a lead with the same phone number is found)

Here are some screenshots:

Hi!

Can you please try and export the Output of one of the bundle in Search Leads (try to hide personal data first), so that I test and show you how you can do?

Benjamin

3 Likes

Hi @Thiago_Borja
You can iterate through the custom fields value array and apply a filter to check if the lead’s phone number already exists. If you provide the output bundle for both the search lead and create lead operations, we can assist you more effectively.

image

If you require additional assistance, please don’t hesitate to reach out to us.
MSquare Support| Book Live Implementation
Visit us here
Youtube Channel

1 Like

Hi guys! Thank you for the help. Here’s the output for the create lead module:

[
{
“id”: 10542412,
“request_id”: “0”,
“_links”: {
“self”: {
“href”: “https://forecastdigitalmaster.kommo.com/api/v4/leads/10542412
}
}
}
]

And here’s the output of one single bundle from the search module (i cannot post the whole output here, too many characters):

{
“id”: 10542412,
“name”: “teste”,
“price”: 0,
“responsible_user_id”: 11295579,
“group_id”: 0,
“status_id”: 68805835,
“pipeline_id”: 8777415,
“loss_reason_id”: null,
“created_by”: 0,
“updated_by”: 0,
“created_at”: “2024-06-12T12:27:46.000Z”,
“updated_at”: “2024-06-12T12:27:46.000Z”,
“closed_at”: null,
“closest_task_at”: null,
“is_deleted”: false,
“custom_fields_values”: [
{
“field_id”: 1962698,
“field_name”: “CIDADE”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “Salvador”
}
],
“is_computed”: false
},
{
“field_id”: 1962680,
“field_name”: “DATA”,
“field_code”: null,
“field_type”: “date”,
“values”: [
{
“value”: 1717642800
}
],
“is_computed”: false
},
{
“field_id”: 1962684,
“field_name”: “DIA DA SEMANA”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “Quinta-feira”
}
],
“is_computed”: false
},
{
“field_id”: 1962686,
“field_name”: “HORA”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “12:37:05”
}
],
“is_computed”: false
},
{
“field_id”: 1962688,
“field_name”: “NOME”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “teste”
}
],
“is_computed”: false
},
{
“field_id”: 1962690,
“field_name”: “telefone_original”,
“field_code”: null,
“field_type”: “numeric”,
“values”: [
{
“value”: “71999999999”
}
],
“is_computed”: false
},
{
“field_id”: 1962696,
“field_name”: “ESTADO”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “Bahia”
}
],
“is_computed”: false
},
{
“field_id”: 1962700,
“field_name”: “CANAL”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “Leadster”
}
],
“is_computed”: false
},
{
“field_id”: 1962702,
“field_name”: “TAGS”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “
}
],
“is_computed”: false
},
{
“field_id”: 1962706,
“field_name”: “URL CAPTURA”,
“field_code”: null,
“field_type”: “url”,
“values”: [
{
“value”: “https://forecastdigital.com.br/teste/#bpc-loas-idoso?campaignid=20758468511&adgroupid=159035103241&matchtype=p&device=m&creative=680106717547&keyword=pedir%20bpc%20loas
}
],
“is_computed”: false
},
{
“field_id”: 1962710,
“field_name”: “ID CAMPANHA”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “20758468511”
}
],
“is_computed”: false
},
{
“field_id”: 1962716,
“field_name”: “PALAVRA CHAVE”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “pedir bpc loas”
}
],
“is_computed”: false
},
{
“field_id”: 1962718,
“field_name”: “CORRESPONDÊNCIA”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “Frase”
}
],
“is_computed”: false
},
{
“field_id”: 1962720,
“field_name”: “DISPOSITIVO”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “Celular”
}
],
“is_computed”: false
},
{
“field_id”: 1966734,
“field_name”: “ID ANÚNCIO”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “680106717547”
}
],
“is_computed”: false
},
{
“field_id”: 1966738,
“field_name”: “ID GRUPO DE ANÚNCIO”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “159035103241”
}
],
“is_computed”: false
},
{
“field_id”: 1962678,
“field_name”: “ID”,
“field_code”: null,
“field_type”: “text”,
“values”: [
{
“value”: “172”
}
],
“is_computed”: false
}
],
“score”: null,
“account_id”: 32881355,
“labor_cost”: null,
“is_price_computed”: false,
“_links”: {
“self”: {
“href”: “https://forecastdigitalmaster.kommo.com/api/v4/leads/10542412?with=&limit=250&filter[pipeline_id]=8777415&order[created_at]=desc&page=1
}
},
“_embedded”: {
“tags”: ,
“companies”:
},
IMTLENGTH”: 10,
IMTINDEX”: 1
}

Hello!

I have first a question: why do you create the Lead, and only after, you check if there’s a duplicate?
This question is important because:
1- If ever it’s duplicated, you will have to do extra work, such as (maybe) delete the Lead you just created.
2- because the Lead is added before, you will ALWAYS find a duplicate, because the Search Leads will also return you the Lead you have just created.

Maybe you need to create him anyway and only notify or do extra stuff if it already existed; but in that case, you will have to count the number of times the phone number exists (it’s doable but it’s extra steps we’ll have to talk about later).

If you want to check if Lead already exists (from a Phone number you receive in your webhook), before you create the Lead; I would suggest to put FIRST “Search Leads”, and then create the new Lead only if nothing was found.

However, I will show you how to extract the phone number with functions (you won’t need to use an Iterator here). It will be a first step. Then, the rest of the scenario will depend on what you want to achieve.

Thank you for your extract of date, it was super useful!

I used a Parse JSON module to simulate the output of “Search Leads”

We have a Array of collections. with a “field_name” and another array called “values”. Let’s assume that there’s always ONE phone number to extract (we will get the first value of the array). If you need to compare with all possible phone number, it will be more complex…

So, the idea is to get the first item of Values when field_name=telefone_original


Here is the function calls (you can copy/paste and change the mapping with the one from Search Leads): {{get(map(2.custom_fields_values; "values.1.value"; "field_name"; "telefone_original"); 1)}}

What does it do?

  1. get the first “value” of the Values array of the item where field_name=telefone_original => this will return a new array that contains the phone number (the map function is used)
  2. from this new array, extract item 1. (the get function is used)

After this, you get the phone number
image

Then you can compare this value with the one you got in your webhook.

But again, it’s possible you have to review the way you build your scenario.

I hope it helps

Benjamin

3 Likes

Hello, Benjamin!

Thank you so much for your answer! That did work.

I’m still getting used to this make syntax, I’m only familiar with javascript and a little bit of python.

There wasn’t any particular reason as to why I was creating a led before searching for it, it was just because I was asked to do this validation afterwards. I think from now on things will get a bit easier and I’ll be able to do what I want.

Thanks again!

EDIT: now I’m having another difficulty: how can I compare the phone I get from the webhook with ALL the phones that I get from the search? I’m using set multiple variable, but it doesnt work as intended…

1 Like

Hey @Thiago_Borja, I’m really happy it helped!
I don’t really understand your last question. If you are stuck can you please open a now topic and tag me so that I can have a look. It will be easier to see where you stand now and check what’s left to do :blush:

Kind regards

Benjamin