Custom fields are available for several services: leads, contacts, companies, customers, catalogElements, and segments.
To access custom fields on a model, use the getCustomFieldsValues() method. This returns a CustomFieldsValuesCollection or null if no values exist.
Inside the collection, you will find models that inherit from BaseCustomFieldValuesModel. These models provide methods to manage field data:
getFieldId() / setFieldId()getFieldType()getFieldCode() / setFieldCode()getFieldName() / setFieldName()getValues() / setValues()
Note that getValues() returns a BaseCustomFieldValueCollection containing BaseCustomFieldValueModel objects. Because different field types (Text, Numeric, Checkbox, etc.) require different data structures, the library provides specific model and collection classes for each type located in the following namespaces:
- Field value models:
\AmoCRM\Models\CustomFieldsValues\ValueModels - Field value collections:
\AmoCRM\Models\CustomFieldsValues\ValueCollections - Custom field models:
\AmoCRM\Models\CustomFieldsValues
// Create an entity model
$lead = new LeadModel();
$lead->setId(1);
// Create a collection of custom fields
$leadCustomFieldsValues = new CustomFieldsValuesCollection();
// Create a model for a specific field type (e.g., Text)
$textCustomFieldValuesModel = new TextCustomFieldValuesModel();
$textCustomFieldValuesModel->setFieldId(123);
// Add values to the field model
$textCustomFieldValuesModel->setValues(
(new TextCustomFieldValueCollection())
->add((new TextCustomFieldValueModel())->setValue('Text'))
);
// Add the field model to the entity's custom fields collection
$leadCustomFieldsValues->add($textCustomFieldValuesModel);
// Set the custom fields on the entity
$lead->setCustomFieldsValues($leadCustomFieldsValues);