Jan 23, 2014

How to update Sugar CRM user Account & contacts detail usign SUGAR API in PHP?

$set_entry_parameters = array(
//session id
"session" => $session_id,
//The name of the module from which to retrieve records.
"module_name" => "Accounts",
//Record attributes
"name_value_list" => array(
 //to update a record, you will need to pass in a record id which you have stored in DB
 array("name" => "id", "value" => $accountStoredId),
 array("name" => "name", "value" => $name),  
 array('name'=>'billing_address_street ', "value"=>$billing_address_street),
 array('name'=>'billing_address_city', "value"=>$billing_address_city),
 array('name'=>'billing_address_state ', "value"=>$billing_address_state),
 array('name'=>'billing_address_postalcode ', "value"=>$billing_address_postalcode)
),
);
$set_entry_result = call("set_entry", $set_entry_parameters, $url);

Related Post:
How to use Sugar CRM API integration with PHP?
How to add user data in Sugar CRM custom module and make relationship in PHP?

How to add user data in Sugar CRM custom module and make relationship in PHP?

Step 1: Please follow the same steps as shown in this article... How to use Sugar CRM API integration with PHP?

Step 2: But please note the following points for custom module.

Note 1: Custom module name must be like 'CustomModulePackagekey_ModuleName'.
Note 2: 'link_field_name' must be the Related Module Name
These above details you can find in 'ModuleBuilder' section of Sugar Admin account.

Step 3: Done. :)

How to integrate Sugar CRM API with PHP?

Step 1: Login connection with SUGAR

$url = "https://urdomain.sugarondemand.com/service/v4_1/rest.php"; //Rest api url
$username = "urusername"; // sugar account username
$password = "urpassword"; // sugar account password

//login Authentication Process
$login_parameters = array(
"user_auth" => array(
 "user_name" => $username,
 "password" => md5($password),
 "version" => "1"
),
"application_name" => "URWebsiteName",
"name_value_list" => array(),
);
//call to sugar api
$login_result = call("login", $login_parameters, $url);
//get session id
$session_id = $login_result->id;

Step 2: Create call() function as follows

function call($method, $parameters, $url)
{
ob_start();
$curl_request = curl_init();

curl_setopt($curl_request, CURLOPT_URL, $url);
curl_setopt($curl_request, CURLOPT_POST, 1);
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, 1);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

$jsonEncodedData = json_encode($parameters);

$post = array(
"method" => $method,
"input_type" => "JSON",
"response_type" => "JSON",
"rest_data" => $jsonEncodedData
);

curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($curl_request);
curl_close($curl_request);

$result = explode("\r\n\r\n", $result, 2);
$response = json_decode($result[1]);
ob_end_flush();

return $response;
}

Step 3: How to add user data in 'Accounts' Module.

$set_entry_parameters = array(
//session id
"session" => $session_id,
//The name of the module from which to retrieve records.
"module_name" => "Accounts",
//Record attributes
"name_value_list" => array(  
 array("name" => "name", "value" => $name),  
 array('name'=>'billing_address_street ', "value"=>$billing_address_street),
 array('name'=>'billing_address_city', "value"=>$billing_address_city),
 array('name'=>'billing_address_state ', "value"=>$billing_address_state),
 array('name'=>'billing_address_postalcode ', "value"=>$billing_address_postalcode)
),
);
$set_entry_result = call("set_entry", $set_entry_parameters, $url);
$accountId = $set_entry_result->id; // Store $accountId in db for update the same record in future

Step 4: How to add user data in 'Contacts' Module.

$set_entry_parameters_contacts = array(
 
"session" => $session_id,
"module_name" => "Contacts",

"name_value_list" => array(  

array("name" => 'first_name', "value" => $first_name),
array("name" => 'last_name', "value" => $last_name),
array('name'=>'account_name', "value"=>$account_name),
array('name'=>'middle_name_c', "value"=>$middle_name_c), // middle_name_c is the custom field in Contacts module
array('name'=>'email1', "value"=>$email1)

),
);
}

$set_entry_result_contact= call("set_entry", $set_entry_parameters_contacts, $url);
$contactId = $set_entry_result_contact->id; // Store $accountId in db for update the same record in future

Step 5: Now let's make relationship between Accounts & Contacts modules

if($accountId != "" && $contactId != "")
{
$parameters = array( 
'session' => $session_id,
'module_name' => 'Accounts',
'module_id' => $accountId,
'link_field_name' => 'contacts', 
'related_ids' => array($contactId) 
); 

$relationship_res = call('set_relationship', $parameters, $url);
}

Step 6: Done. :)

How to add user data in Sugar CRM custom module and make relationship in PHP?