[Solved] Updating a custom field in a lead entity doesn't seem to get persisted

Hi all,

my issue could be related to this one Updating leads after adding fields but since there was no answer on this topic I create this one.

I got a few custom fields in my leads table.
My code which shall to update the fields in the leads table it looks like that:

$entity = $repo->findOneBy( $keyArray );

/*
The following sets a value of my custom field which I added in the Contacts table via
“Settings/CustomFields”
*/
$entity->setCustomfieldvalue(5);
$entity->setFirstName(“anotherName”);

/*
The following is a method which is provided by the CustomFieldEntityTrait which is used in the
Mautic\LeadBundle\Entity\Lead class.
It prints out the information that the customfieldvalue has been set!
*/
print_r($entity->getUpdatedFields());
Array
(
[customfieldvalue] => 5
)

$this->em->persist($entity);
$this->em->flush();

The problem is that the persist and the flush only store the update of the FirstName field (which is part of the Mautic Lead entity). However the custom field gets ignored.

Does anybody know how what I am doing wrong?
Another useful information might be if I am creating the dataset (so not updating an existing one, but creating a new one) using:

$this->leadModel->saveEntity($entity);

Both fields are set correctly. So really seems to be a problem with the update of custom fields.

Update:
I played around with it and it seems when I am using the

$this->leadModel->saveEntity($entity);

expression for the updated entity it works.
I haven’t done this in the first place, because I was expecting to the entity gets then added in a new dataset in the lead table (which would then duplicate the existing dataset). However I think because the $entity already holds an “id” value, the “saveEntity” expression, does overwrite the existing entity.

So this is fine for me and I will close this task as solved.