2013-01-21 46 views
3

任何人都知道如何通過糖的REST API v4從表單中的多選輸入中插入多個值。我還沒有找到任何文件。下面襯塊1倍的值正確,但是我卡上保存多個值:如何通過Rest API向SugarCRM發佈多選值?

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」是糖自定義多選場與幾個下拉值。我試過運行一個for-each循環,但只能插入最後一個值。我也嘗試過在數組中插入數組的幾乎所有可能的變體,但沒有任何成功。任何幫助深表感謝!我花了數小時試圖弄清楚這一點。提前感謝您對如何處理此問題的任何見解/指導。

回答

3

是否將字段內容以此格式工作?

^value1^,^value2^,^value3^ 
+0

jmetric - 謝謝你!我做了以下工作,它完美地工作:array('name'=>'programs_c','value'=>'^ ABC ^,^ EFG ^'), – pcommons

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; 
}