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
  •   I have not done this but, as you noticed, the HowTo that you are referring to is very old (2011) which suggests it is a 6.x solution. Version 7.x is very different.

    You will want to look at the enum field type: <mysugar>/clients/base/fields/enum/enum.js to see how the values are set for the dropdown options and you will want to either extend that field type for your module or create your own field type for that particular multi-select.

    Note that the getAppListStrings is used at multiple points in the enum for building the list, displaying, and saving values. So you'll need to make sure that your values pass all the checks and balances for dropdowns. As you may have noticed, if you have a value in the database that is no longer in the dropdown's app_list_strings (like a legacy value) it won't display at all in the front end. (Have a way to mark dropdown values as legacy ) so you will need to make sure your dynamic dropdown values exist when you load your list view, record view etc.

    I hope this helps you move forward and I look forward to seeing how you solve this challenge.

    FrancescaS

Reply
  •   I have not done this but, as you noticed, the HowTo that you are referring to is very old (2011) which suggests it is a 6.x solution. Version 7.x is very different.

    You will want to look at the enum field type: <mysugar>/clients/base/fields/enum/enum.js to see how the values are set for the dropdown options and you will want to either extend that field type for your module or create your own field type for that particular multi-select.

    Note that the getAppListStrings is used at multiple points in the enum for building the list, displaying, and saving values. So you'll need to make sure that your values pass all the checks and balances for dropdowns. As you may have noticed, if you have a value in the database that is no longer in the dropdown's app_list_strings (like a legacy value) it won't display at all in the front end. (Have a way to mark dropdown values as legacy ) so you will need to make sure your dynamic dropdown values exist when you load your list view, record view etc.

    I hope this helps you move forward and I look forward to seeing how you solve this challenge.

    FrancescaS

Children
No Data