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
jucuzoglujucuzoglu 

How to upload an attachment as a Salesforce Document

Can someone please point me in the right direction, I am looking to use the PHP SOAP API to upload a document to store as a Document object within Salesforce.

 

I want the document to be available publicly which I can do with the document type.

 

Any tips and suggestions and pointers appriciated.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Pat PattersonPat Patterson

This sample shows how to upload a Document into a given Folder - you can run it from the command line with php script.php <folder> <file>

 

 

<?php
if ( $argc != 3 ) {
	die('Usage: php '.$argv[0]." <folder> <file>\n");
}

require_once ('soapclient/SforcePartnerClient.php');

define("SF_USERNAME", 'user@example.com');
define("SF_PASSWORD", 'password');
define("SF_SECURITY_TOKEN", 'SECUEITY_TOKEN');

$mySforceConnection = new SforcePartnerClient();
$mySforceConnection->createConnection("soapclient/partner.wsdl.xml");
$mySforceConnection->login(SF_USERNAME, SF_PASSWORD.SF_SECURITY_TOKEN);

// Get the Folder Id
$response = $mySforceConnection->query('SELECT Id FROM Folder WHERE Name = \''.$argv[1].'\'');
if ( count($response->records) != 1 ) {
	die('Found '.count($response->records).' matches for '.$argv[1]."\n");
}

$folder_id = $response->records[0]->Id;
echo 'Found folder id '.$folder_id."\n";

// Upload the file as a Document
$fields = array (
	'Body' => base64_encode(file_get_contents($argv[2])),
	'Description' => 'File '.$argv[2],
	'FolderId' => $folder_id,
	'IsPublic' => true,
	'Name' => $argv[2]
);

$sObject = new SObject();
$sObject->fields = $fields;
$sObject->type = 'Document';

echo "Creating Document\n";
$response = $mySforceConnection->create(array ($sObject));

print_r($response);
?>

Cheers,


Pat

 

All Answers

Pat PattersonPat Patterson

This sample shows how to upload a Document into a given Folder - you can run it from the command line with php script.php <folder> <file>

 

 

<?php
if ( $argc != 3 ) {
	die('Usage: php '.$argv[0]." <folder> <file>\n");
}

require_once ('soapclient/SforcePartnerClient.php');

define("SF_USERNAME", 'user@example.com');
define("SF_PASSWORD", 'password');
define("SF_SECURITY_TOKEN", 'SECUEITY_TOKEN');

$mySforceConnection = new SforcePartnerClient();
$mySforceConnection->createConnection("soapclient/partner.wsdl.xml");
$mySforceConnection->login(SF_USERNAME, SF_PASSWORD.SF_SECURITY_TOKEN);

// Get the Folder Id
$response = $mySforceConnection->query('SELECT Id FROM Folder WHERE Name = \''.$argv[1].'\'');
if ( count($response->records) != 1 ) {
	die('Found '.count($response->records).' matches for '.$argv[1]."\n");
}

$folder_id = $response->records[0]->Id;
echo 'Found folder id '.$folder_id."\n";

// Upload the file as a Document
$fields = array (
	'Body' => base64_encode(file_get_contents($argv[2])),
	'Description' => 'File '.$argv[2],
	'FolderId' => $folder_id,
	'IsPublic' => true,
	'Name' => $argv[2]
);

$sObject = new SObject();
$sObject->fields = $fields;
$sObject->type = 'Document';

echo "Creating Document\n";
$response = $mySforceConnection->create(array ($sObject));

print_r($response);
?>

Cheers,


Pat

 

This was selected as the best answer
Walter@AdicioWalter@Adicio

found very helpful. thank you.

Suresh GocharSuresh Gochar
 iwant Show my salesforce image my  C #  page  , Any idea  , i m here   new in  salesforce , i got path    


<img src="/servlet/servlet.FileDownload?file=00P280000081wVi" alt=" " height="25" width="25" border="0"/>

but image not show

plz help it urgent