Add a Company via Web Form
Scope:
crmWho can execute the method: users with permission to create companies in CRM
You can place a form on the site to collect client data. When a client fills out the form, their data will be sent to CRM, and you will be able to process the request.
Setting up the form consists of two steps.
-
Place the form on an HTML page. It will send data to the handler.
-
Create a file to process the data. The handler will accept and prepare the data, and then create a company using the method crm.company.add.
1. Creating the Web Form
Let's create a web form on the website page with three fields:
-
TITLE— company name, required, -
EMAIL— email address, -
PHONE— phone number.
When submitted, the form sends data to the handler form.php.
<form id="form_to_crm" method="POST" action="form.php">
<!-- Company name (required field) -->
<input type="text" name="TITLE" placeholder="Company name" required>
<!-- Email address -->
<input type="text" name="EMAIL" placeholder="Email">
<!-- Phone number -->
<input type="text" name="PHONE" placeholder="Phone">
<!-- Submit button -->
<input type="submit" value="Submit">
</form>
<!-- Include jQuery for AJAX request -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
// Submit the form without reloading the page
$('#form_to_crm').on('submit', function(el) {
el.preventDefault(); // Cancel the default submission
// Get form data
var formData = $(this).serialize();
// Send data to the server
$.ajax({
'method': 'POST',
'dataType': 'json',
'url': 'form.php', // Handler file
'data': formData,
success: function(data) {
alert(data.message); // Show the result
}
});
});
});
</script>
2. Creating the Form Handler
To process the values from the form fields and add a company to CRM, we will create the handler form.php.
To add a company, we will use the method crm.company.add. In the fields object, we pass the fields:
-
TITLE— company name, -
COMPANY_TYPE— type of company. We specifyCUSTOMER, as only clients of the company fill out the form, -
PHONE— phone number, -
EMAIL— email address.
The values for TITLE, PHONE, and EMAIL are obtained from the form. The system stores phone and email as an array of objects crm_multifield, so they need to be formatted as an array.
-
If a value exists, we add it as the first element
VALUEin the array, and the second value specifies the typeVALUE_TYPE, for example:-
WORK— for phone, -
HOME— for email.
-
-
If no value exists, we pass an empty array.
Check which required fields are set for companies in your Bitrix24. All required fields must be passed to the method crm.company.add.
<?
// Get data from the form
$sTitle = htmlspecialchars($_POST["TITLE"]);
$sPhone = htmlspecialchars($_POST["PHONE"]);
$sEmail = htmlspecialchars($_POST["EMAIL"]);
// Format phone and email for Bitrix24 in crm_multifield format
$arPhone = (!empty($sPhone)) ? array(array('VALUE' => $sPhone, 'VALUE_TYPE' => 'WORK')) : array();
$arEmail = (!empty($sEmail)) ? array(array('VALUE' => $sEmail, 'VALUE_TYPE' => 'HOME')) : array();
// Send data to Bitrix24
$result = CRest::call(
'crm.company.add',
[
'fields' =>[
"TITLE" => $sTitle, // Company name
"COMPANY_TYPE" => 'CUSTOMER', // Company type — client
"PHONE" => $arPhone, // Phone
"EMAIL" => $arEmail, // Email
]
]
);
// Return the result
if(!empty($result['result'])){
echo json_encode(['message' => 'Company added']);
}
elseif(!empty($result['error_description'])){
echo json_encode(['message' => 'Company not added: '.$result['error_description']]);
}
else{
echo json_encode(['message' => 'Company not added']);
}
?>