How to update the list of items from the response in some fields ?

1. I am calling an API on click of button. I am able to get response.  

Response Sample :  

{

Item1,

Item2,

Item3,

Item4

}

2.Now I want to put these values in some field (dropDownlist) where user can select it and use it. 

3.I had no idea how to put the items from response into dropdownlist or any other view filed. 

Please help me with  some suggestions and advice.

Version used : Sugar 10.3 (enterprise)

record.js

({
/*Disabling button ,condition based or dynamically on RecordView.
*
* custom/modules/Accounts/clients/base/views/record/reord.js
*/
extendsFrom:'RecordView',
initialize:function(options){
this._super('initialize',[options]);
//add listener for custom button
this.context.on('button:activate_button:click', this.activate_button, this);
},
activate_button:function(){
var self = this;
app.api.call('read', app.api.buildURL('Accounts/CustomerActivationAPI/' + this.model.get('id')), null,{
success:function(fdata)
{
console.log("Response", fdata);
},
error: function(error){
},
});
}
})

  • Hi Gautam,

    You have to set the values to model in the code and save the model.

    Please try to add something like this inside the success function.

    success:function(fdata)
    {
    console.log("Response", fdata);

    self.model.set('fieldname1', Item1);
    self.model.set('fieldname2', Item2);
    self.model.save();

    },

    where Item1 and Item2 you will be getting from your response and fieldname1,fieldname2 are the field names in your module.

    Hope this helps.

    Thank you

    Poojitha Katram

    Senior Solution Engineer,

    www.bhea.com

  • I unknow your business rule, but my suggest you work with fields dependencies, where your API returns one value and you set this value any field inside model, with this, your dependencie code start works create a new value list for this field.

    support.sugarcrm.com/.../

    I hope help you, regards

  • Just to be clear, you want to ADD these values that come back from the API to a drop-down.  So the API runs, the valuses get added to a dropdown and then the user can select ONE (or more I guess) of these values from the dropdown.

    Is that correct?

  • Yes correct.

    Also changes should saved . I mean once values get added it should save in db. Once I do reload or navigate somewhere and come back to the same record, values should not get vanished. I dn't to click on 'Save'button as I have some logic hook on save. 

  • OK, I have a module where on save I add the name (and id) from that module to a drop down list for another module to use.  Its pretty simple and should work for you.  I dont have time to reorg the code for your needs right now but if you cant make it work just yell and I can find some time this week.

    <?php
    
    
    	/**
    	 * Update the Dropdown when new items are added/activated or items are removed/inactivated
    	 *
    	 * @param SugarBean $bean
    	 */
    	private function updateLanguageFile($bean)
    	{
    		$sql = "SELECT id, name 
    				FROM acc_s2s_access 
    				WHERE deleted=0 AND
    					  status!='Inactive' 
    				ORDER BY name ASC";
    		$result = $GLOBALS['db']->query($sql, true);
    		$newLangList = array();
    		while ($hash = $GLOBALS['db']->fetchByAssoc($result)) {
    			$newLangList[$hash['id']] = $hash['name'];
    		}
    		//Since this is a before save logic hook the status
    		// in the DB has not been set yet so we have to account
    		// for that here
    		if ($bean->status == 'Active') {
    			//Add (If needed, Might already be there)
    			$newLangList[$bean->id] = $bean->name;
    		} else {
    			//Remove
    			unset($newLangList[$bean->id]);
    		}
    		asort($newLangList);
    		$this->addItemsToDropdown('app_access_list', $newLangList);
    	}
    
    	/**
    	 * @param string $dropdown_list
    	 * @param array  $item_list
    	 */
    	private function addItemsToDropdown($dropdown_list, $item_list)
    	{
    		require_once('modules/ModuleBuilder/MB/ModuleBuilder.php');
    		require_once('modules/ModuleBuilder/parsers/parser.dropdown.php');
    		$parser = new ParserDropDown();
    		$params = array();
    		$_REQUEST['view_package'] = 'studio'; //need this in parser.dropdown.php
    		$params['view_package'] = 'studio';
    		$params['dropdown_name'] = $dropdown_list;
    		$params['dropdown_lang'] = 'en_us';
    		foreach ($item_list as $k => $v) {
    			$drop_list[] = array($k, $v);
    		}
    		//TODO:Update this to use namespaces
    		$json = getJSONobj();
    		$params['list_value'] = $json->encode($drop_list);
    		$parser->saveDropDown($params);
    	}
    }