add new custom field into lead table

Hi,

I want to add a new custom field (“age”) into leads table from my app bundle(plugin) programmatically.

how can I add a new field into database leads table programmatically?

Hi,
I want to add a new custom field (“age”) into leads table from my app bundle(plugin) programmatically.
how can I add a new field into database leads table programmatically?

Hello @vishal, did you find a solution for this question?

Thank you.

To create a new custom field programmatically it’s here : https://developer.mautic.org/#create-field

I assume what you want to do is to assign an age’s value to your leads programmatically, not “define” the custom field itself.

Well, once the field is created you can also modify the age for a specific lead through the API by hitting the contact’s endpoint.
The flow could be something like that:
1 Get contact’s id from the email address (https://developer.mautic.org/#list-contacts)
2 With the id, edit contact custom fields by passing the key:value pair in the data array (https://developer.mautic.org/#edit-contact)

Thanks @alban.
Finally I used API method like that you link here but I was searching for a method that create a new custom field for contact/company without calling the API from inside Mautic.
I tried with ORM\ClassMetadata $builder and something like $builder->createField('my_custom_field', 'text')->nullable()->build(); inside an helper but it doesn’t work.

Thanks anyway.

Hi

To add a new Field you can do it like this:

    $field = new LeadField();
    /** @var FieldModel $model */
    $model = $this->getContainer()->get('mautic.model.factory')->getModel('lead.field');
    $field
        ->setLabel('Yout Label')
        ->setAlias('your alias')
        ->setDefaultValue(false)
        ->setType('boolean');
    $model->saveEntity($field);