{"id":9165,"date":"2024-09-19T05:46:08","date_gmt":"2024-09-19T05:46:08","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=9165"},"modified":"2024-09-19T09:55:14","modified_gmt":"2024-09-19T09:55:14","slug":"add-custom-dropdown-dynamically-from-db-in-vtiger-lead-form","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/add-custom-dropdown-dynamically-from-db-in-vtiger-lead-form\/","title":{"rendered":"Add Custom Dropdown Dynamically from DB in vTiger Lead Form"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,657<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p>To add a custom dropdown with values dynamically fetched from the database in the lead form of <strong>vTiger<\/strong>, you need to perform the following steps:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Create a Custom Field for Dropdown<\/strong><\/h3>\n\n\n\n<p>First, create a custom dropdown field in the <strong>Leads<\/strong> module.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to the <strong>vTiger CRM<\/strong> dashboard.<\/li>\n\n\n\n<li>Navigate to <strong>CRM Settings<\/strong> &gt; <strong>Module Management<\/strong> &gt; <strong>Module Layouts and Fields<\/strong>.<\/li>\n\n\n\n<li>Select the <strong>Leads<\/strong> module and click <strong>Add Custom Field<\/strong>.<\/li>\n\n\n\n<li>Choose <strong>Picklist (Dropdown)<\/strong> as the field type and give it a suitable label.<\/li>\n\n\n\n<li>Save the custom field.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Modify the Database to Fetch Dynamic Values<\/strong><\/h3>\n\n\n\n<p>If you want to fetch dropdown values dynamically from your database (e.g., from a custom table), you&#8217;ll need to modify the code.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Steps:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a Table to Store Dropdown Values:<\/strong><br>Create a table in the database to store the dynamic values for your dropdown.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">CREATE TABLE custom_dropdown_values (\n       id INT AUTO_INCREMENT PRIMARY KEY,\n       value VARCHAR(255) NOT NULL\n   );<\/mark><\/code><\/code><\/pre>\n\n\n\n<p>Insert some initial values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">INSERT INTO custom_dropdown_values (value) VALUES ('Value 1'), ('Value 2'), ('Value 3');<\/mark><\/code><\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Modify the Code to Fetch Values Dynamically:<\/strong> You&#8217;ll need to modify the core files to dynamically populate the dropdown values.<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go to the <strong>Leads<\/strong> module\u2019s layout template file, typically located at: <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">modules\/Leads\/DetailView.php<\/mark><\/code><\/li>\n\n\n\n<li>Modify the part of the form where the custom dropdown is being rendered. You can use a PHP query to fetch the values from the database. Example:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">global $adb;\n   $query = \"SELECT value FROM custom_dropdown_values\";\n   $result = $adb-&gt;pquery($query, array());\n   $dropdown_values = array();\n\n   while ($row = $adb-&gt;fetch_array($result)) {\n       $dropdown_values&#91;] = $row&#91;'value'];\n   }\n\n   \/\/ Output the dropdown in HTML\n   echo '&lt;select name=\"custom_field_dropdown\"&gt;';\n   foreach ($dropdown_values as $value) {\n       echo '&lt;option value=\"'.$value.'\"&gt;'.$value.'&lt;\/option&gt;';\n   }\n   echo '&lt;\/select&gt;';<\/mark><\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Ensure Values are Saved and Retrieved Properly<\/strong><\/h3>\n\n\n\n<p>Ensure the system saves the selected value in the database when the form is submitted. Check the appropriate handler file, usually under:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">modules\/Leads\/Save.php<\/mark><\/code><\/code><\/pre>\n\n\n\n<p>Update the code to handle the saving of the selected dropdown value into the <strong>vtiger_leadscf<\/strong> (custom fields) table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>Test the Form<\/strong><\/h3>\n\n\n\n<p>After implementing these changes, test the lead form by navigating to the <strong>Leads<\/strong> module, creating a new lead, and checking if the dropdown is populated with dynamic values from your database. Also, ensure that the selected value is saved properly.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>To simply add a custom dropdown with all needed values dynamically gotten easily from the database in the lead form of\u00a0vTiger, you just want to perform the various above-mentioned steps while having the <a href=\"https:\/\/www.infinitivehost.com\/managed-vtiger-solutions\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\"><strong>best Vtiger hosting solutions<\/strong><\/mark><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1,657 Views To add a custom dropdown with values dynamically fetched from the database in the lead form of vTiger, you need to perform the following steps: 1. Create a Custom Field for Dropdown First, create a custom dropdown field in the Leads module. 2. Modify the Database to Fetch Dynamic Values If you want [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[204],"tags":[],"class_list":["post-9165","post","type-post","status-publish","format-standard","hentry","category-vtiger-solutions"],"_links":{"self":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/comments?post=9165"}],"version-history":[{"count":2,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9165\/revisions"}],"predecessor-version":[{"id":9170,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9165\/revisions\/9170"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=9165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=9165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=9165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}