SugarClub
SugarClub
  • User
  • Site
  • Search
  • User
  • Groups & Discussions
    Groups & Discussions
    • Product Forums
      Product-focused Q&A, discussions, best practices, fixes, and help
      Product Forums
      • Sugar Market
      • Sugar Sell & Enterprise
      • Sugar Serve
      • sales-i
    • User Groups
      Professional, Industry, Language
    • Get Involved
      Learn how to become a Raving Fan
    • Social Club
      Live, interactive, virtual meetups with other Sugar customers and Sugar’s Subject Matter experts!
    • Leadership Lounge
      Network with fellow organizational leaders, ask questions, and share insights
    • Developers
      Visit DevClub, the SugarClub group for Sugar Developers
      Developers
      • DevClub
      • Mobile Developers
      • Developer Builds
        Supplemental access level required. Inquiries: developers@sugarcrm.com
    • Additional Groups (Access Required)
      Groups that require special access will be displayed here. Contact sugarclub@sugarcrm.com for assistance. Click here to see all groups
      Additional Groups (Access Required)
      • SugarCloud Platform
  • Product Information
    Product Information
    • Release Central
      Find release-specific content to prepare for your next Sugar update
    • Documentation & Resources
      Looking to expand your Sugar knowledge? Explore our in-depth documentation and other helpful resources!
    • Product Update Blogs
      Updates about each Sugar product
    • Customer Stories »
      Case Studies by SugarCRM
  • Training & Certification
    Training & Certification
    • Training & Certification Home
      Live & On-Demand classes, Quick Videos, Sugar Certifications, and more!
    • Quick Videos
      Short videos about using Sugar
    • My SugarU Dashboard »
    • SugarU News & Updates
  • Adoption
    Adoption
    • Grow Adoption Framework
      Get started on your adoption journey and review the adoption resources from SugarCRM
  • Calendar
  • News
    News
    • Sugar News
    • SugarCRM.com News »
    • Dev Blog
    • SugarCRM Marketplace Blog
  • Help
    Help
    • Welcome to Sugar!
      New to Sugar? Get started here!
    • SugarClub Help & Instructions
      Learn more about SugarClub and find answers to questions about this site
    • New to SugarClub?
      Start your community journey here
    • Technical Support
      Sugar's support resources
      Technical Support
      • Case Portal »
        Access the SugarCRM Case Portal
      • Working with Sugar Support »
        Find out more about engaging with the SugarCRM Support team
      • SugarCloud Information
        Find information about SugarCloud service updates and site status. Contact sugarclub@sugarcrm.com to request access
  • More from Sugar
    More from Sugar
    • DevClub
    • PartnerClub
    • Support
    • SugarOutfitters Marketplace
    • sugarcrm.com
  • DevClub
  • PartnerClub
  • Support
  • Marketplace
  • sugarcrm.com
DevClub
DevClub
Dev Tutorials Programatically manage Dropdown List
Click here to join this group and curate your SugarClub experience.
  • +On-Boarding Framework
  • +Customization Guides
  • +Modern UI Technical Guide
  • +Automated PHP Compatibility Tool
  • Did you know? Copying related records is a breeze!
  • How to write code for SugarCloud webinar Q&A
  • HOW TO: enforce ACL on Tags
  • Programatically manage Dropdown List
  • Remove custom fields created via package installation
  • Sugar Developer Tools
  • Tutorial:  How to register custom platforms in Sugar instances via Platform extension
  • Adding a google reCAPTCHA in a Web-to-Lead form
  • Sugar Developer Blog Style Guide

You are currently reviewing an older revision of this page.

  • History View current version

Programatically manage Dropdown List

Custom API developed in order to change/add/delete key's from a dropdownList

 

This API have 2 ways to be called, that works for 3 different means:

  • Add a new Key from a DropDown
    • rest/v10/DropdownListKey/add
  • Edit an existing Key from a DropDown
    • rest/v10/DropdownListKey/add
  • Remove an existing Key from a DropDown
    • rest/v10/DropdownListKey/delete

 

In order to work we need to call a POST for the desired endPoint with the configuration body:

{
"list_name":"<LIST_NAME>",
"item_key":"<KEY_DROPDOWN>",
"item_value":"<KEY_VALUE>",
"lang":"<LANG>"
}

 

API code:

custom/clients/base/api/DropDownFiller.php

<?php

if (!defined('sugarEntry') || !sugarEntry)
    die('Not A Valid Entry Point');

class DropDownFiller extends SugarApi {
    public function registerApiRest() {
         
        $dropDownApi = array(
            'dropdownListAddKey' => array(
                'reqType' => 'POST',
                'path' => array('DropdownListKey','add'),
                'pathVars' => array('',''),
                'method' => 'addDropDownKeyValue',
                'shortHelp' => 'create a new item to an specific list',
                'longHelp' => 'custom/clients/base/api/help/MyEndPoint_MyGetEndPoint_help.html',
            ),
            'dropdownListRemoveKey' => array(
                'reqType' => 'POST',
                'path' => array('DropdownListKey','delete'),
                'pathVars' => array('', ''),
                'method' => 'removeDropDownKeyValue',
                'shortHelp' => 'Remove a key from an specific list',
                'longHelp' => 'custom/clients/base/api/help/MyEndPoint_MyGetEndPoint_help.html',
            ),
        );

        return $dropDownApi;
    }

    /**
     * Method to be used for rest/v10/DropdownListKey/add endpoint
     */
    public function addDropDownKeyValue($api, $args) {
         
        require_once('include/utils.php');
        require_once('modules/ModuleBuilder/MB/ModuleBuilder.php');
        require_once('modules/ModuleBuilder/parsers/parser.dropdown.php');
         
        $list_name = $args['list_name'];
        $item_key = $args['item_key'];
        $item_value = $args['item_value'];
        $lang = $args['lang'];
         
        $displayValue = translate($list_name, '', $item_key);
         
        if(!isset($displayValue)){
            return "DropdownList not found";
        }
        else if( is_array($displayValue) ){
            $parser = new ParserDropDown();
            $params = array();
            $_REQUEST['view_package'] = 'studio';
            $params['view_package'] = 'studio';
            $params['dropdown_name'] = $list_name;
            $params['dropdown_lang'] = $lang;
            $_REQUEST['dropdown_lang'] = $lang;
             
            $displayValue[$item_key] = $item_value;
             
            foreach ($displayValue as $k=>$v) {
                $drop_list[] = array($k,$v);
            }
             
            $json = getJSONobj();
            $params['list_value'] = $json->encode($drop_list);
            $parser->saveDropDown($params);
             
            return "Key/value added";
        }
        else{
            $displayValue = translate($list_name);
             
            $parser = new ParserDropDown();
            $params = array();
            $_REQUEST['view_package'] = 'studio';
            $params['view_package'] = 'studio';
            $params['dropdown_name'] = $list_name;
            $params['dropdown_lang'] = $lang;
            $_REQUEST['dropdown_lang'] = $lang;
             
            $displayValue[$item_key] = $item_value;
             
            foreach ($displayValue as $k=>$v) {
                $drop_list[] = array($k,$v);
            }
             
            $json = getJSONobj();
            $params['list_value'] = $json->encode($drop_list);
            $parser->saveDropDown($params);
             
            return "Key/value changed";
        }
    }
     
    /**
     * Method to be used for rest/v10/DropdownListKey/delete endpoint
     */
    public function removeDropDownKeyValue($api, $args) {
         
        require_once('include/utils.php');
        require_once('modules/ModuleBuilder/MB/ModuleBuilder.php');
        require_once('modules/ModuleBuilder/parsers/parser.dropdown.php');
         
        $list_name = $args['list_name'];
        $item_key = $args['item_key'];
        $lang = $args['lang'];
         
        $displayValue = translate($list_name, '', $item_key);
         
        if(!isset($displayValue)){
            return "DropdownList not found";
        }
        else if( is_array($displayValue) ){
            return "Key not found";
        }
        else{
            $parser = new ParserDropDown();
            $params = array();
            $_REQUEST['view_package'] = 'studio';
            $params['view_package'] = 'studio';
            $params['dropdown_name'] = $list_name;
            $params['dropdown_lang'] = $lang;
            $_REQUEST['dropdown_lang'] = $lang;
             
            $displayValue = translate($list_name);
             
            unset($displayValue[$item_key]);
             
            foreach ($displayValue as $k=>$v) {
                $drop_list[] = array($k,$v);
            }
             
            $json = getJSONobj();
            $params['list_value'] = $json->encode($drop_list);
            $parser->saveDropDown($params);
             
            return "Key removed";
        }
    }
}
?>