dynamic multi-select from API call

Hello

    what i'm trying to achieve here is to make an API call and use the retrieved values to generate a dynamic multi-select field and be able to save selected options. I have used HOWTO: Have a dropdown field that get's it's options from a function as a start point with no success. This is what i did:

1- created a new dropdown from dropdown editor named test_users_list to attach it to the multi-select field below. The reason i created this list was b/c when you create a multi-select field from studio it can't be empty and i didn't want to use any of the existing ones.

2- created a new multi-select field on campaigns (using dropdown list from step 1) that created the following file

custom/Extension/modules/Campaigns/Ext/Vardefs/sugarfield_test_users_c.php

3- inside that file it contained

<?php
// created: 2017-10-19 03:23:07

$dictionary['Campaign']['fields']['test_users_c']['labelValue']='test users';
$dictionary['Campaign']['fields']['test_users_c']['dependency']='';
$dictionary['Campaign']['fields']['test_users_c']['visibility_grid']='';

?>

4- proceeded to create the function as advised on the HOWTO and my file ended up like this

<?php
// created: 2017-10-19 03:23:07
function getUsers() {
$ch = curl_init('https://jsonplaceholder.typicode.com/users');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$userss = json_decode($result, true);

$userList = array();
$userList['']='';
foreach ($userss as $i => $userx) {
$userList[$userss[$i]['username']]=$userss[$i]['name'];
}

return $userList;
}
unset($dictionary['Campaign']['fields']['test_users_c']['options']);
$dictionary['Campaign']['fields']['test_users_c']['function']='getUsers';
$dictionary['Campaign']['fields']['test_users_c']['labelValue']='test users';
$dictionary['Campaign']['fields']['test_users_c']['dependency']='';
$dictionary['Campaign']['fields']['test_users_c']['visibility_grid']='';

?>

Went to campaigns module, edited a campaign and the field is empty the array list wasn't created

empty multi-select

One thing that i noticed is that i didn't have 

$dictionary['Campaign']['fields']['test_users_c']['options']

to unset as advised on the HOWTO. I tried removing it with same results.

This HOWTO seems to be for an older version should still work for Ent 7.9.2, what could i be missing?

P.S. the API Call and the Array list do work i tested them separately this is how it looks the array list that should populate the multi-select

Array
(
[] =>
[Bret] => Leanne Graham
[Antonette] => Ervin Howell
[Samantha] => Clementine Bauch
[Karianne] => Patricia Lebsack
[Kamren] => Chelsey Dietrich
[Leopoldo_Corkery] => Mrs. Dennis Schulist
[Elwyn.Skiles] => Kurtis Weissnat
[Maxime_Nienow] => Nicholas Runolfsdottir V
[Delphine] => Glenna Reichert
[Moriah.Stanton] => Clementina DuBuque
)

please advise

Sergio

Parents
  • Hello Sergio, 

    I've done something similar. In my case, The users from Sugar have a Sales Channel property (A multiselect field, so they can have different Sales Channels).

    Then, when I'm creating an opportunity, there is a Sales Channel dropdown field that will populate ONLY the values from the assigned user, and everytime the user gets changed, the function will retreive the proper Sales Channels.

    If the assigned user has only one sales channel , then it will be selected by default.

    ({
    extendsFrom: 'OpportunitiesCreateView',
    initialize: function (options) {
    this._super('initialize', [options]);
    this._filterSalesChannelDropdown();
    this.model.on('change:assigned_user_id', this._filterSalesChannelDropdown, this);
    },

    _renderHtml: function(options) {

    this._super("_renderHtml", [options]);
    },

    _filterSalesChannelDropdown: function() {
    //debugger;
    var self = this;
    var userBean = app.data.createBean('Users', {id:self.model.get("assigned_user_id")});
    var saleschannel_list = app.lang.getAppListStrings('Sales_Channel_list');

    var request = userBean.fetch({

    success: _.bind(function(data) {
    if (userBean.get("sales_channel_c")){
    inString = (userBean.get("sales_channel_c")).toString();
    var dd_values = {};
    var dd_array = {};
    var array_count = 0;
    _.each(saleschannel_list, function(value, key) {
    //console.log(key, value);
    if(key === ""){
    dd_values[key] = value;
    }else if(inString.search(key)==-1){
    delete dd_values[key];
    }else{
    dd_values[key] = value;
    dd_array[array_count] = key;
    array_count = array_count+1;
    }
    });
    self.model.fields['sales_channel_c'].options = dd_values;
    if (Object.keys(dd_values).length == 2){
    self.model.set('sales_channel_c', dd_array[0]);
    }
    }
    },this),
    });

    },

    _dispose: function() {
    this._super('_dispose', []);
    }

    })

    Hope this helps.

    Angel Mtz.

Reply
  • Hello Sergio, 

    I've done something similar. In my case, The users from Sugar have a Sales Channel property (A multiselect field, so they can have different Sales Channels).

    Then, when I'm creating an opportunity, there is a Sales Channel dropdown field that will populate ONLY the values from the assigned user, and everytime the user gets changed, the function will retreive the proper Sales Channels.

    If the assigned user has only one sales channel , then it will be selected by default.

    ({
    extendsFrom: 'OpportunitiesCreateView',
    initialize: function (options) {
    this._super('initialize', [options]);
    this._filterSalesChannelDropdown();
    this.model.on('change:assigned_user_id', this._filterSalesChannelDropdown, this);
    },

    _renderHtml: function(options) {

    this._super("_renderHtml", [options]);
    },

    _filterSalesChannelDropdown: function() {
    //debugger;
    var self = this;
    var userBean = app.data.createBean('Users', {id:self.model.get("assigned_user_id")});
    var saleschannel_list = app.lang.getAppListStrings('Sales_Channel_list');

    var request = userBean.fetch({

    success: _.bind(function(data) {
    if (userBean.get("sales_channel_c")){
    inString = (userBean.get("sales_channel_c")).toString();
    var dd_values = {};
    var dd_array = {};
    var array_count = 0;
    _.each(saleschannel_list, function(value, key) {
    //console.log(key, value);
    if(key === ""){
    dd_values[key] = value;
    }else if(inString.search(key)==-1){
    delete dd_values[key];
    }else{
    dd_values[key] = value;
    dd_array[array_count] = key;
    array_count = array_count+1;
    }
    });
    self.model.fields['sales_channel_c'].options = dd_values;
    if (Object.keys(dd_values).length == 2){
    self.model.set('sales_channel_c', dd_array[0]);
    }
    }
    },this),
    });

    },

    _dispose: function() {
    this._super('_dispose', []);
    }

    })

    Hope this helps.

    Angel Mtz.

Children