Any one help me on mass update delete in sugar CRM API
I want to delete multiple records at a time.
How mass update delete works ?
Any working example?
Any one help me on mass update delete in sugar CRM API
I want to delete multiple records at a time.
How mass update delete works ?
Any working example?
Hi Rana,
I added a small php script which retrieves all Leads with status = "Dead" and deletes them.
The trick is to collect all IDs in an array and then to call the MassUpdate function with a double nested array like this:
$leads_to_delete = array(); $leads_to_delete[] = 'id1'; ... $leads_to_delete[] = 'idn'; $url = $base_url . '/Leads/MassUpdate'; $mu_params = array( "massupdate_params" => array( "uid" => $leads_to_delete ) ); $mu_response = call($url, $oauth2_token_response->access_token, 'DELETE', $mu_params);
Full code of my example:
<?php $base_url = "https://localhost/sugarent1010/rest/v10"; $username = "admin"; // user who is admin $password = "**********"; // password $platform = "mobile"; // not base to prevent kicking out of the desktop app ini_set('max_execution_time', 0); $script_start = time(); $time_start = time(); // Login - POST /oauth2/token $login_url = $base_url . "/oauth2/token"; $logout_url = $base_url . "/oauth2/logout"; $oauth2_token_arguments = array( "grant_type" => "password", "client_id" => "sugar", "client_secret" => "", "username" => $username, "password" => $password, "platform" => $platform ); //FOR ALL Leads while (true) { // Login // To be sure there is no side effect, renew login after max_num changes $oauth2_token_response = call($login_url, '', 'POST', $oauth2_token_arguments); $DEBUG .= "## TOKEN RESPONSE: ".print_r($oauth2_token_response,true)." ##</hr>\n"; echo $DEBUG; $DEBUG=""; if ($oauth2_token_response->access_token == "") die("No Login"); // READ max_num Leads - 2 for test, 200 for prod $url = $base_url . '/Leads'; $leads_arguments = array( "filter" => array( array( "status" => "Dead" ) ), "max_num" => 2, "offset" => 0, "fields" => "id,first_name,last_name", ); $DEBUG .= "## SEARCH LEADS: ".print_r($leads_arguments,true)." ##</br>\n"; $leads_response = call($url, $oauth2_token_response->access_token, 'GET', $leads_arguments); $DEBUG .= "## SEARCH RESULT: ".print_r($leads_response,true)." ##</br>\n"; if (count($leads_response->records) > 0) { $leads_to_delete = array(); foreach($leads_response->records as $idc => $led) { // Add ID to list of mass delte IDs $leads_to_delete[] = $led->id; } // Call MassUpdate $url = $base_url . '/Leads/MassUpdate'; $mu_params = array( "massupdate_params" => array( "uid" => $leads_to_delete ) ); $DEBUG .= "## MU call: ".$url." ##</br>\n"; $mu_response = call($url, $oauth2_token_response->access_token, 'DELETE', $mu_params); $DEBUG .= "## MU RESULT: ".print_r($mu_response,true)." ##</br>\n"; } else { break; } // logout to clean up call($logout_url, '', 'POST', $oauth2_token_arguments); echo $DEBUG; $DEBUG=""; // break; //test } $script_runtime = time()-$script_start; $DEBUG .= "TIME needed: ".$script_runtime."<br>\n"; echo $DEBUG; $DEBUG=""; //////////////////////////////////////////////////////////////////// // END OF MAIN //////////////////////////////////////////////////////////////////// /** * Generic function to make cURL request. * @param $url - The URL route to use. * @param string $oauthtoken - The oauth token. * @param string $type - GET, POST, PUT, DELETE. Defaults to GET. * @param array $arguments - Endpoint arguments. * @param array $encodeData - Whether or not to JSON encode the data. * @param array $returnHeaders - Whether or not to return the headers. * @param array $filenHeader - Whether or not to upload a file * @return mixed */ function call( $url, $oauthtoken='', $type='GET', $arguments=array(), $encodeData=true, $returnHeaders=false, $fileHeader=false ) { $type = strtoupper($type); if ($type == 'GET') { $url .= "?" . http_build_query($arguments); } $curl_request = curl_init($url); if ($type == 'POST') { curl_setopt($curl_request, CURLOPT_POST, 1); } elseif ($type == 'PUT') { curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT"); } elseif ($type == 'DELETE') { curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE"); } curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders); curl_setopt($curl_request, CURLOPT_SSL_VERIFYHOST, 0); // wichtig curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0); // wichtig curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0); if (!empty($oauthtoken)) { if ($fileHeader) { curl_setopt($curl_request, CURLOPT_HTTPHEADER, array( "oauth-token: {$oauthtoken}")); } else { curl_setopt($curl_request, CURLOPT_HTTPHEADER, array( "oauth-token: {$oauthtoken}", "Content-Type: application/json")); } } else { curl_setopt($curl_request, CURLOPT_HTTPHEADER, array( "Content-Type: application/json")); } if (!empty($arguments) && $type !== 'GET') { if ($encodeData) { //encode the arguments as JSON $arguments = json_encode($arguments); } curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments); } $result = curl_exec($curl_request); if ($returnHeaders) { //set headers from response list($headers, $content) = explode("\r\n\r\n", $result ,2); foreach (explode("\r\n",$headers) as $header) { header($header); } //return the nonheader data return trim($content); } curl_close($curl_request); //decode the response from JSON $response = json_decode($result); return $response; } ?>
PS: replaced a $offset by 0
Harald Kuske
Principal Solution Architect – Professional Services, EMEA
hkuske@sugarcrm.com
SugarCRM Deutschland GmbH
Hi Rana,
I added a small php script which retrieves all Leads with status = "Dead" and deletes them.
The trick is to collect all IDs in an array and then to call the MassUpdate function with a double nested array like this:
$leads_to_delete = array(); $leads_to_delete[] = 'id1'; ... $leads_to_delete[] = 'idn'; $url = $base_url . '/Leads/MassUpdate'; $mu_params = array( "massupdate_params" => array( "uid" => $leads_to_delete ) ); $mu_response = call($url, $oauth2_token_response->access_token, 'DELETE', $mu_params);
Full code of my example:
<?php $base_url = "https://localhost/sugarent1010/rest/v10"; $username = "admin"; // user who is admin $password = "**********"; // password $platform = "mobile"; // not base to prevent kicking out of the desktop app ini_set('max_execution_time', 0); $script_start = time(); $time_start = time(); // Login - POST /oauth2/token $login_url = $base_url . "/oauth2/token"; $logout_url = $base_url . "/oauth2/logout"; $oauth2_token_arguments = array( "grant_type" => "password", "client_id" => "sugar", "client_secret" => "", "username" => $username, "password" => $password, "platform" => $platform ); //FOR ALL Leads while (true) { // Login // To be sure there is no side effect, renew login after max_num changes $oauth2_token_response = call($login_url, '', 'POST', $oauth2_token_arguments); $DEBUG .= "## TOKEN RESPONSE: ".print_r($oauth2_token_response,true)." ##</hr>\n"; echo $DEBUG; $DEBUG=""; if ($oauth2_token_response->access_token == "") die("No Login"); // READ max_num Leads - 2 for test, 200 for prod $url = $base_url . '/Leads'; $leads_arguments = array( "filter" => array( array( "status" => "Dead" ) ), "max_num" => 2, "offset" => 0, "fields" => "id,first_name,last_name", ); $DEBUG .= "## SEARCH LEADS: ".print_r($leads_arguments,true)." ##</br>\n"; $leads_response = call($url, $oauth2_token_response->access_token, 'GET', $leads_arguments); $DEBUG .= "## SEARCH RESULT: ".print_r($leads_response,true)." ##</br>\n"; if (count($leads_response->records) > 0) { $leads_to_delete = array(); foreach($leads_response->records as $idc => $led) { // Add ID to list of mass delte IDs $leads_to_delete[] = $led->id; } // Call MassUpdate $url = $base_url . '/Leads/MassUpdate'; $mu_params = array( "massupdate_params" => array( "uid" => $leads_to_delete ) ); $DEBUG .= "## MU call: ".$url." ##</br>\n"; $mu_response = call($url, $oauth2_token_response->access_token, 'DELETE', $mu_params); $DEBUG .= "## MU RESULT: ".print_r($mu_response,true)." ##</br>\n"; } else { break; } // logout to clean up call($logout_url, '', 'POST', $oauth2_token_arguments); echo $DEBUG; $DEBUG=""; // break; //test } $script_runtime = time()-$script_start; $DEBUG .= "TIME needed: ".$script_runtime."<br>\n"; echo $DEBUG; $DEBUG=""; //////////////////////////////////////////////////////////////////// // END OF MAIN //////////////////////////////////////////////////////////////////// /** * Generic function to make cURL request. * @param $url - The URL route to use. * @param string $oauthtoken - The oauth token. * @param string $type - GET, POST, PUT, DELETE. Defaults to GET. * @param array $arguments - Endpoint arguments. * @param array $encodeData - Whether or not to JSON encode the data. * @param array $returnHeaders - Whether or not to return the headers. * @param array $filenHeader - Whether or not to upload a file * @return mixed */ function call( $url, $oauthtoken='', $type='GET', $arguments=array(), $encodeData=true, $returnHeaders=false, $fileHeader=false ) { $type = strtoupper($type); if ($type == 'GET') { $url .= "?" . http_build_query($arguments); } $curl_request = curl_init($url); if ($type == 'POST') { curl_setopt($curl_request, CURLOPT_POST, 1); } elseif ($type == 'PUT') { curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT"); } elseif ($type == 'DELETE') { curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE"); } curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders); curl_setopt($curl_request, CURLOPT_SSL_VERIFYHOST, 0); // wichtig curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0); // wichtig curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0); if (!empty($oauthtoken)) { if ($fileHeader) { curl_setopt($curl_request, CURLOPT_HTTPHEADER, array( "oauth-token: {$oauthtoken}")); } else { curl_setopt($curl_request, CURLOPT_HTTPHEADER, array( "oauth-token: {$oauthtoken}", "Content-Type: application/json")); } } else { curl_setopt($curl_request, CURLOPT_HTTPHEADER, array( "Content-Type: application/json")); } if (!empty($arguments) && $type !== 'GET') { if ($encodeData) { //encode the arguments as JSON $arguments = json_encode($arguments); } curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments); } $result = curl_exec($curl_request); if ($returnHeaders) { //set headers from response list($headers, $content) = explode("\r\n\r\n", $result ,2); foreach (explode("\r\n",$headers) as $header) { header($header); } //return the nonheader data return trim($content); } curl_close($curl_request); //decode the response from JSON $response = json_decode($result); return $response; } ?>
PS: replaced a $offset by 0
Harald Kuske
Principal Solution Architect – Professional Services, EMEA
hkuske@sugarcrm.com
SugarCRM Deutschland GmbH
Thanks
I will try that solution
Harald Kuske brother i am using your call function and delete query for that
its giving me this response
object(stdClass)#1 (2) { ["failed"]=> int(0) ["status"]=> string(4) "done" }
and mu_params become like this
array(1) { ["massupdate_params"]=> array(1) { ["uid"]=> string(118) "["a4d45120-0947-11eb-98a6-02994e5d8eda","9e237cde-0947-11eb-8c7a-06937d6972be","95bfd7d6-0947-11eb-b30b-02994e5d8eda"]" } }
but its not deleting any id
can you please guide me what's am i doing wrong?