Bitrix24 Community

Support » Forum » CRM » Cannot get REST API to update custom fields
Pages: 1
RSS
Cannot get REST API to update custom fields, The Rest API seems really useful, but no matter what I do, I cannot update custom fields.
I have added several custom fields to leads and want to populate those in a web form. I can see that the php array is populated with all the correct values, and whilst the lead gets created and I get a 201 status code, none of the custom fields are set.

Any ideas please?

Thanks
Mike
Hi Mike,

Make sure that you pass a custom field's code correctly. It should look the following way:  UF_CRM_<unique_code>. You can find this code at the URL of the required field editing form, e.g. https://<you_bitrix24_name>.bitrix24.com/crm/configs/fields/CRM_LEAD/edit/UF_CRM_1351087512/

If you still have any questions, please contact our Helpdesk.

Kind Regards,

Yana.
Thanks Yana that is great!  I got it working but would have never guessed how to find code for each custom field!

I will post the amended html form and php code that I used in case it helps anybody else.
Edited: Mike Paterson - 05/20/2014 20:25:51
I took the sample code provided in the blog and because I am running the site in Joomla, I split the php into a file called form.php, and saved the html form separately.

In case anybody is using Joomla, I used the JUMI extension to add the html form as custom code to the chosen page/article.

I also added a redirect to the php to display the ThankYou page I already had on the site.


FORM.PHP

<?
// CRM server conection data
define('CRM_HOST', 'yourdomain.bitrix24.com'); // your CRM domain name
define('CRM_PORT', '443'); // CRM server port
define('CRM_PATH', '/crm/configs/import/lead.php'); // CRM server REST service path

// CRM server authorization data
//define('CRM_LOGIN', 'userid');  login of a CRM user able to manage leads//
//define('CRM_PASSWORD', 'password');  password of a CRM user//
// OR you can send special authorization hash which is sent by server after first successful connection with login and password
define('CRM_AUTH', 'Your Auth Code Here'); // authorization hash

/********************************************************************************************/

// POST processing
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$leadData = $_POST['DATA'];

// get lead data from the form
$postData = array(
'TITLE' => $leadData['TITLE'],
'COMPANY_TITLE' => $leadData['COMPANY_TITLE'],
'NAME' => $leadData['NAME'],
'LAST_NAME' => $leadData['LAST_NAME'],
'PHONE_WORK' => $leadData['PHONE_WORK'],
'EMAIL_WORK' => $leadData['EMAIL_WORK'],
'COMMENTS' => $leadData['COMMENTS'],
     'UF_CRM_1399567957' => $leadData['Product Interest'], // These are the extra custom fields I added to the Leads
'UF_CRM_1399568024' => $leadData['Request Demo'], // If you sel ect each custom field in turn in Bitrix24 you will find the UF_CRM codes at the end of each url 'UF_CRM_1399568207' => $leadData['Request Trial'],
'UF_CRM_1399568174' => $leadData['Request More Information'],

           );


// append authorization data
if (defined('CRM_AUTH'))
{
$postData['AUTH'] = CRM_AUTH;
}
else
{
$postData['LOGIN'] = CRM_LOGIN;
$postData['PASSWORD'] = CRM_PASSWORD;
}

// open socket to CRM
$fp = fsockopen("ssl://".CRM_HOST, CRM_PORT, $errno, $errstr, 30);
if ($fp)
{
// prepare POST data
$strPostData = '';
foreach ($postData as $key => $value)
$strPostData .= ($strPostData == '' ? '' : '&').$key.'='.urlencode($value);

// prepare POST headers
$str = "POST ".CRM_PATH." HTTP/1.0\r\n";
$str .= "Host: ".CRM_HOST."\r\n";
$str .= "Content-Type: application/x-www-form-urlencoded\r\n";
$str .= "Content-Length: ".strlen($strPostData)."\r\n";
$str .= "Connection: close\r\n\r\n";

$str .= $strPostData;

// send POST to CRM
fwrite($fp, $str);

// get CRM headers
$result = '';
while (!feof($fp))
{
$result .= fgets($fp, 128);
}
fclose($fp);

// cut response headers
$response = explode("\r\n\r\n", $result);

// $output = '<pre>'.print_r($response[1], 1).'</pre>';
    // redirect to Thank You page and exit
header("Location: http://proxima-software.com/Thank-You";
die();
//code down here now wont get run

}
else
{
echo 'Connection Failed! '.$errstr.' ('.$errno.')';
}
}
else
{
$output = '';
}


HTML FORM


<ht ml>
<head>
<met a http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<?=$output;?>
<fo rm action="form.php" method="post">
<input type="hidden" name="DATA[TITLE]" value="Lead fr om Proxima Website" /><br />
[COLOR=#ff0000]<label for="Product Interest">Product interest
Removed
Edited: Mike Paterson - 05/20/2014 20:46:40 (BBcode was screwing the messages up)
Trying again as the forum software does not like code being posted



HTML FORM

<ht ml>
<head>
<met a http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<?=$output;?>
<fo rm action="form.php" method="post">
<input type="hidden" name="DATA[TITLE]" value="Lead from Proxima Website">
<label for="Product Interest">Product interest</label>
<select name="DATA[Product Interest]" title="Product Interest">
<option value="">--None--</option>
<option value="Business Service Management">Business Service Management</option>
<option value="Datacentre Management">Datacentre Management</option>
<option value="CMDB/CMS">CMDB/CMS</option>
<option value="IT Analytics">IT Analytics</option>
<option value="Performance and Capacity Management">Performance and Capacity Management</option>
<option value="Workload Automation">Workload Automation</option>
<option value="Managed File Transfer">Managed File Transfer</option>
<option value="Password Reset Self-Service">Password Reset Self-Service</option>
<option value="IT Asset Management">IT Asset Management</option>
<option value="Asset Discovery">Asset Discovery</option>
<option value="Service Desk Analytics">Service Desk Analytics</option>
<option value="SLA/KPI Management">SLA/KPI Management</option>
<option value="Bring Your Own Device">Bring Your Own Device</option>
<option value="Mobile Device Management">Mobile Device Management</option>
<option value="Datacentre Power Management">Datacentre Power Management</option>
<option value="IT Transformation Consultancy">IT Transformation Consultancy</option>
<option value="IT Project Resourcing">IT Project Resourcing</option>
</select><br>

<label for="Request Demo">Request Demo</label>
<select  name="DATA[Request Demo]" title="If you would like a product demo, please select Yes and if appropriate type your requirements in the Comments and Questions field">
<option value="No">No</option>
<option value="Yes">Yes</option>
</select><br>

<label for="Request Trial">Request Trial</label>
<select  name="DATA[Request Trial]" title="If you would like to trial the software, please select Yes and if appropriate type your requirements in the Comments and Questions field">
<option value="No">No</option>
<option value="Yes">Yes</option>
</select><br>

<label for="Request More Information">Request More Information</label>
<select name="DATA[Request More Information]" title="If you would like more information, please select Yes and if appropriate type your requirements in the Comments and Questions field">
<option value="No">No</option>
<option value="Yes">Yes</option>
</select><br>



Company Name: <input type="text" name="DATA[COMPANY_TITLE]" value="" /><br />
First Name: <input type="text" name="DATA[NAME]" value="" /><br />
Last Name: <input type="text" name="DATA[LAST_NAME]" value="" /><br />
Telephone: <input type="text" name="DATA[PHONE_WORK]" value="" /><br />
Email: <input type="text" name="DATA[EMAIL_WORK]" value="" /><br />
Comments: <textarea name="DATA[COMMENTS]"></textarea><br />


<input type="submit" value="Send" />
</form>


Hope this helps somebody
Cheers
Mike
Edited: Mike Paterson - 05/20/2014 21:01:57
Is there a method in the API to get that custom fields?
Any method?
I need same.
How can I get custom field data from API?

How can I filter by custom field?
Found the answer to filter:

crm.contact.list.json?FILTER[UF_CRM_1468978857]=XXX
I have added several custom fields to leads and want to populate those  in a web form. The last is the product to be attached to the lead. I can see that the php array is populated with all the  correct values, and whilst the lead gets created and I get a 201 status  code, none of the product fields are set.

Any ideas please

Thanks,

Peter
Pages: 1
2,000,000+
organizations
are already using Bitrix24