function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
esource8esource8 

Adding attachments to salesforce cases

I'm having some trouble adding attachments to a salesforce case. I'm pretty new to developing on salesforce, and our system uses a custom implementation using the Drupal CMS to send and retreive data from salesforce.

 

We have a form system that, upon submit, executes a function that inserts data into a salesforce case. What I'm trying to do is add some code that takes a file (an array with a filename, path, mimetype, filesize), and adds it to a salesforce case. I have not been able to find any documentation that explains how this works with any sort of clarity, and again, please note that I am pretty unfamilair with the salesforce API.

 

The insertion currently works fine, I just need to figure out how to add attachments to it. Any help you could give me would be greatly appreciated.

 

Here is the function that handles the salesforce insertion:

 

function esource_inquiry_form_submit($form_id, $form_values) {
	global $user;
	global $sf_retrieve;
	global $form_values;
	profile_load_profile($user);
	
	$first_name = $user->profile_sf_FirstName;
	$last_name = $user->profile_sf_LastName;
	$user_name = $first_name ." ". $last_name;
	if (strlen($first_name) == 0 && strlen($last_name) == 0) {
		$user_name = $user->name;
	}
	$user_email = $user->mail;
	$user_phone = $user->profile_sf_Phone;

	$rid = 0;
	$user_company = $user->profile_sf_AccountName;

	//get the user's Salesforce ID, to map to ContactId in Salesforce case; added BJ 12/07/07
	
	$user_contactId = $user->profile_sf_Id;
	$idLen = strlen($user_contactId);
	$user_contactId = substr($user_contactId,0,$idLen-3);
	//////////

	$fields = array(
		'ContactId' => $user_contactId,
		'SuppliedName' => htmlspecialchars($user_name),
		'SuppliedPhone' => htmlspecialchars($form_values['phone']),
		'SuppliedEmail' => htmlspecialchars($user_email),
		'SuppliedCompany' =>htmlspecialchars($user_company),
		'Description' => htmlspecialchars($form_values['description']),
		'Priority' => htmlspecialchars($form_values['priority']),
		'Subject' => htmlspecialchars($form_values['subject']),
		'Origin' => "Web",
		'Detail__c' => $form_values['detail'],
//but attachments aren't fields, can't use this
 ); //This is the attachment array $attachment = $form_values['attachment']; $case_created = false; $test_mode = false; try {
//This works just fine, but how to add attachments to the case?
 $sf = new salesforce("modules/sf/partner.wsdl.xml", $test_mode); $sf_caseid = $sf->insert_case($fields);

//Where do I add attachments to the case insert?

 if ($sf_caseid === false) { watchdog('esource', "Failed to add case to salesforce; adding to sf_update_q", WATCHDOG_ERROR); } else { watchdog('esource', "Added case ".$sf_caseid." to salesforce"); $case_created = true; } } catch (Exception $e) { // connection failed watchdog('esource', "Connection to salesforce.com failed: ".$e->getMessage(), WATCHDOG_ERROR); } if ($case_created == false) { db_query("INSERT INTO {sf_update_q} (sf_table, value) VALUES ('Case', '%s')", serialize ($fields)); $message .= 'Name: ' . $user_name . '<br>'; $message .= 'Phone: ' . $form_values['phone'] . '<br>'; $message .= 'Email: ' . $user_email . '<br>'; $message .= 'Company: ' . $user_company . '<br><br>'; $message .= 'Your Question: ' . $form_values['description'] . '<br>'; $message .= 'Priority: ' . $form_values['priority'] . '<br>'; $message .= 'Subject: ' . $form_values['subject'] . '<br>'; $message .= 'Detail: ' . $form_values['detail'] . '<br>'; notify_user('webmaster@esource.com', 'Failed to add a case to Salesforce; adding to sf_update_q', $message); } else if ($case_created == true) { $message .= '<b>Thank you for your inquiry. The following information has been submitted to our Member Inquiry Service:</b><br>'; $message .= 'Name: ' . $user_name . '<br>'; $message .= 'Phone: ' . $form_values['phone'] . '<br>'; $message .= 'Email: ' . $user_email . '<br>'; $message .= 'Company: ' . $user_company . '<br><br>'; $message .= 'Your Question: ' . $form_values['description'] . '<br>'; $message .= 'Priority: ' . $form_values['priority'] . '<br>'; $message .= 'Subject: ' . $form_values['subject'] . '<br>'; $message .= 'Detail: ' . $form_values['detail'] . '<br>'; //notify_user(variable_get('member_inquiry_email_address', get_default_member_inquiry_email_address()), 'New Case Added by Member', $message); //message no longer being sent to Leland, because it duplicates a Salesforce notification. Changed 12/07/07 BJ } $_SESSION['inquiry_values'] = $form_values; return 'esource/inquiry_land'; }

 
Thanks in advance to anyone who can offer help.

SuperfellSuperfell

Once you've created the case, you would make an additional call to create passing in one or more sobjects of type attachment. You set their parentId field to the Id of the case you just created, and set the body field to be the base64 encoded binary data (I'm not familar with the php tools, they might encode it for you)

esource8esource8

Thanks for the prompt response Simon, it's great to have help on stuff like this - it's really appreciated. So I had some sample code from some other threads, and I think I've done what you've suggested, but now I still don't know how to send the new attachment object to salesforce. I've written some code based on that, but I also get an error when I follow it. Here's what I've written, commented to help explain.

 

	$sf_caseid = $sf->insert_case($fields);
		
	
//Attempting to create a new SF object using some previously-submitted examples
$sAttachment= new SObject(); $sAttachment->type = 'Attachment'; $handle = fopen($attachment->filepath,'r+'); $file_content = fread($handle,filesize($attachment->filesize)); fclose($handle); $encoded = chunk_split(base64_encode($file_content)); $sAttachment->body = $encoded; $createFields = array ( 'Name' => $attachment->filename, 'ParentId' => $sf_caseid, 'Body' => $encoded ); $sAttachment->fields = $createFields; $createResponse = $sf->create(array($sAttachment));

 However, when I try to execute this code, I get this error:

 

Fatal error: Call to undefined method salesforce::create() 

 

Any suggestions?