3

任何人都知道如何通过 Sugar 的 REST API v4 从表单中的多选输入中插入多个值。我还没有找到任何关于此的文档。以下正确插入 1 个值,但我坚持保存多个值:

function getSugar($method, $parameters){
    $url = 'https://****.sugarondemand.com/service/v4/rest.php';
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $json = json_encode($parameters);
    $postArgs = array(
         'method' => $method,
         'input_type' => 'JSON',
         'response_type' => 'JSON',
         'rest_data' => $json
          );
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
    $response = curl_exec($curl);
    if (!$response) {
         die("Connection Failure.\n");
     }
     $result = json_decode($response);
     if ( !is_object($result) ) {
         die("Error handling result.\n");
     }
     if($method == "set_entry" || $method == "login"){
         if ( !isset($result->id) ) {
              die("Error: {$result->name} - {$result->description}\n.");
         }
         $id = $result->id;
         return $id;
      }else{
         if ( !is_object($result) ) {
             var_dump($response);
             die("Error handling result.\n");
          }
          return true;
     }
}

$x = "ABC";
$parameters = array(
    'session' => $sessionId,
    'module' => 'Leads',
    'name_value_list' => array(
         array('name' => 'programs_c', 'value' => $x),
         ),
 );
 $method = "set_entry";
 $leadID = getSugar($method, $parameters);

字段“programs_c”是 Sugar 中的自定义多选字段,具有多个下拉值。我试过运行一个 for-each 循环,但只插入最后一个值。我还尝试了几乎所有可能的在数组中插入数组的变体,但没有任何成功。任何帮助深表感谢!我花了几个小时试图弄清楚这一点。在此先感谢您提供有关如何处理此问题的任何见解/方向。

4

2 回答 2

5

以这种格式放置字段内容是否有效?

^value1^,^value2^,^value3^
于 2013-01-21T20:57:35.953 回答
0

这是我编写的一些方法来帮助构建多选查询字符串。

private static function convertMultiSelectValueToArray($multiString) {
    return explode(",", $multiString);
}

/**
 * @param $notifyUser
 * @param $manageContacts
 * @param string $currMultiSelectString
 * @return string
 *
 * Examples:
 * print SugarCRM::buildRelationshipMultiselect(true,true) . "\n";  -->  ^Notify of New Updates^,^AllowManageContacts^
 * print SugarCRM::buildRelationshipMultiselect(false,true) . "\n"; --> ^AllowManageContacts^
 * print SugarCRM::buildRelationshipMultiselect(false,false, PERMISSION_ALLOW_USER_TO_MANAGE_CONTACTS . "," . PERMISSION_NOTIFY_OF_NEW_UPDATES . ",^SomeOtherThingThatShouldStillBeHere^") . "\n"; --. ^AllowManageContacts^
 */
public static function buildRelationshipMultiselect($notifyUser, $manageContacts, $currMultiSelectString='') {
    $arr = array();
    if( !empty($currMultiSelectString) ) {
        $arr = SugarCRM::convertMultiSelectValueToArray($currMultiSelectString);
    }

    // Remove them so we dont get duplicates & so they are removed if booleans are false...
    if( count($arr) > 0 ) {
        $arr = SugarCRM::removeElementFromArray($arr,PERMISSION_NOTIFY_OF_NEW_UPDATES);
        $arr = SugarCRM::removeElementFromArray($arr,PERMISSION_ALLOW_USER_TO_MANAGE_CONTACTS);
    }

    if( $notifyUser ) {
        $arr[] = PERMISSION_NOTIFY_OF_NEW_UPDATES;
    }

    if( $manageContacts ) {
        $arr[] = PERMISSION_ALLOW_USER_TO_MANAGE_CONTACTS;
    }

    return implode(",", $arr);
}

public function createNewContactRelatedToAccount($firstName,$lastName,$emailAddress,$phone,
                                                 $description,$accountId,$isManageContacts=false, $isNotifyOfUpdates=false)
{
    // TODO, check if user exists first!

    $relationships = SugarCRM::buildRelationshipMultiselect($isNotifyOfUpdates, $isManageContacts /** PUT EXISTING VALUE HERE IF UPDATING! **/);
    $sugar = new Sugar_REST( SUGAR_REST_URL, SUGAR_USER_NAME, SUGAR_PASSWORD);
    $values = array(
        'first_name' => htmlentities($firstName),
        'last_name' => htmlentities($lastName),
        'email1' => htmlentities($emailAddress),
        'description' => htmlentities($description),
        'phone_work' => htmlentities($phone),
        SUGAR_FIELDNAME_FOR_PERMISSIONS => $relationships
    );

    $contact = $sugar->set("Contacts", $values );
    $contactId = $contact['id'];
    $relStatus = $sugar->set_relationship("Accounts",$accountId, "contacts", $contactId );

    return $relStatus['created'] == 1;
}

private static function removeElementFromArray(&$search_arr, $itemToRemove) {
    // Should have just used array_search...
    foreach($search_arr as $key => $value) {
        if($value == $itemToRemove) {
            unset($search_arr[$key]);
        }
    }
    return $search_arr;
}
于 2013-06-27T08:47:15.217 回答