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
LaurentDelLaurentDel 

Retrieve Document with the Webservice API

Hi,

 

I am developing a Java application talking to Salesforce with the WebService API. I have generated my Java library from the Entreprise WSDL with wsc-20.jar.

I got my connection running and the user is now logged in without a problem. I request a Document and am now trying to get the Body in a String.

I cannot understand what to do with byte[] we get from object.getBody().

It says in the documentation:
"Required. Encoded file data. If specified, then do not specify a URL."
And then for type base64:
"Base 64-encoded binary data. Fields of this type are used for storing binary files in Attachment records, Document records, and Scontrol records. In these objects, the Body or Binary field contains the (base64 encoded) data, while the BodyLength field defines the length of the data in the Body or Binary field. In the Document object, you can specify a URL to the document instead of storing the document directly in the record."

So I understand that the data in the body of the document is encoded in base64. So I need to decode it.

I found this method provided by Salesforce:
com.sforce.ws.util.Base64.decode

I also found examples of:
org.apache.commons.codec.binary.Base64

I tried both ways and I still can't get my file's content right.

Here is my code:

 

public void queryRecords() {
		QueryResult qResult = null;
		  try {
		    String soqlQuery = "SELECT name, body, BodyLength, Type FROM Document LIMIT 10";
			qResult = connection.query(soqlQuery);
			boolean done = false;
			if (qResult.getSize() > 0) {
			  System.out.println("Logged-in user can see " + 
			      qResult.getRecords().length + 
			      " Document records."
			  );
			  while (! done) {
			    SObject[] records = qResult.getRecords();
			    for ( int i = 0; i < records.length; ++i ) {
			    	Document con = (Document) records[i];
				    String fName = con.getName();
				    int bodyLength = con.getBodyLength();
				    String type = con.getType();
				    System.out.println("Doc name " + (i + 1) + ": " + con.getName() + " - bodyLength: "+bodyLength+" - type: "+type);
				    
				    System.out.println("Doc body " + (i + 1) + ": " + con.getBody());
				    System.out.println("Doc body " + (i + 1) + ": " + con.getBody().toString()); 
				    System.out.println("Doc body sf" + (i + 1) + ": " + com.sforce.ws.util.Base64.decode(con.getBody()) );
				    System.out.println("Doc body sf 2" + (i + 1) + ": " + com.sforce.ws.util.Base64.decode(con.getBody()).toString() );
				    
				    String decodedResult = Base64.decodeBase64(con.getBody()).toString();
				    System.out.println("Doc body commons decode" + (i + 1) + ": " + org.apache.commons.codec.binary.Base64.decodeBase64(con.getBody()));
				    System.out.println("Doc body commons decode" + (i + 1) + ": " + decodedResult);
				    
				    String decodedResult2 = con.getBody().toString();
				    System.out.println("Doc body " + (i + 1) + ": " + decodedResult2);
				    
			    }
			    if (qResult.isDone()) {
			        done = true;
			    } else {
			        qResult = connection.queryMore(qResult.getQueryLocator());
			    }
			  }
			} else {
			  System.out.println("No records found.");
			}
			  System.out.println("\nQuery succesfully executed.");
		  } catch (ConnectionException ce) {
		    ce.printStackTrace();
		  }
	}

 

 

I would be really grateful if you could give me any advice.

Cheers,
Laurent

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

Because your using the enterprise WSDL, which is strongly typed, the soap stack has already base64 decoded the string to a byte [], you just need to write the bytes to a file.

All Answers

SuperfellSuperfell

Because your using the enterprise WSDL, which is strongly typed, the soap stack has already base64 decoded the string to a byte [], you just need to write the bytes to a file.

This was selected as the best answer
LaurentDelLaurentDel

Thanks, it was so simple that I didn't think about it :/

 

My example:

 

Document doc = (Document) records[i];
FileOutputStream fos = new FileOutputStream("sample2.txt");
	      // We'll write the string below into the file
	      fos.write(doc.getBody());
	      fos.close(); 

 

 

Cheers,

Laurent