• Ollie.ax81
  • NEWBIE
  • 0 Points
  • Member since 2004

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 28
    Replies
I cannot find an example of code that uploads a Document, the Samples does not.

Something seems to have changed since version 3 regarding the Document upload. The following used to work fine in version 3 but does not work in version 5.

public String CreateDocument(
String docName,
File doc,
String description,
ID authorId,
ID folderId) {

echo("CreateDocument");
echo("docName=" + docName);
echo("File doc=" + doc.toString());
echo("Description=" + description);
echo("authorId=" + authorId.getValue());
echo("folderId=" + folderId.getValue());

String retStatus = "Success";
int fileLength = (int) doc.length();
byte[] body = new byte[fileLength];
//Verify that we are already authenticated, if not
//call the login function to do so
if (!loggedIn) {
if (!login()) {
return "Bad SalesForce Login";
}
}
try {
FileInputStream fis = new FileInputStream(doc);
fileLength = fis.read(body);
} catch (FileNotFoundException e) {
retStatus = "Error Failed to CreateDocument:"
+ e.getLocalizedMessage();
e.printStackTrace();
return retStatus;
} catch (IOException e1) {
retStatus = "Error Failed to CreateDocument:"
+ e1.getLocalizedMessage();
e1.printStackTrace();
return retStatus;
}

if (authorId == null) {
//default to current user
authorId = this.loginResult.getUserId();
}

try {
Document document;
SObject[] accs = new SObject[1];
document = new Document();

document.setName(docName);
document.setBody(body);
document.setDescription(description);
document.setAuthorId(authorId);
document.setFolderId(folderId);

accs[0] = document;

//create the object(s) by sending the array to the web service
SaveResult[] sr = binding.create(accs);
for (int i = 0; i < sr.length; i++) {
SaveResult result = sr[i];
if (result.isSuccess()) {
retStatus = "Success Created Document " + docName;
this.setLastSFID(result.getId().getValue());
//stores sfid
// for saving
// to property
// of new
// document
} else {
Error[] ers = (Error[]) result.getErrors();
retStatus = "Error:";
for (int j = 0; j < ers.length; j++) {
Error error = ers[j];
retStatus += error.getMessage() + ";";
System.out.println("error.getStatusCode():"
+ error.getStatusCode());
echo(retStatus);
}
}
}
} catch (UnexpectedErrorFault uef) {
echo(uef.getExceptionMessage());
} catch (Exception ex) {
retStatus = "\nFailed to create folder, " + docName
+ ", error message was: \n" + ex.getMessage();
echo(retStatus);
}

return retStatus;
}
I had adding a document to a document folder working in version 3 and now it is not working with version 5.

Using Tomcat 5 and a web application, as well as a JUnit test script to exercise the following code.

public String CreateDocument(String docName, File doc, String description,
ID authorId, ID folderId) {
echo("CreateDocument");
echo("docName=" + docName);
echo("File doc=" + doc.toString());
echo("Description=" + description);
echo("authorId=" + authorId.getValue());
echo("folderId=" + folderId.getValue());

String retStatus = "Success";
int fileLength = (int) doc.length();
byte[] body = new byte[fileLength];

//Verify that we are already authenticated, if not
//call the login function to do so
if (!loggedIn) {
if (!login()) {
return "Bad SalesForce Login";
}
}
try {
FileInputStream fis = new FileInputStream(doc);
fileLength = fis.read(body);
} catch (FileNotFoundException e) {
retStatus = "Error Failed to CreateDocument:"
+ e.getLocalizedMessage();
e.printStackTrace();
return retStatus;
} catch (IOException e1) {
retStatus = "Error Failed to CreateDocument:"
+ e1.getLocalizedMessage();
e1.printStackTrace();
return retStatus;
}

if (authorId == null) {
//default to current user
authorId = this.loginResult.getUserId();
}

try {
Document document;
SObject[] accs = new SObject[1];
document = new Document();

document.setName(docName);
document.setBody(body);
document.setDescription(description);
document.setAuthorId(authorId);
document.setFolderId(folderId);

accs[0] = document;

//create the object(s) by sending the array to the web service
SaveResult[] sr = binding.create(accs);
for (int i = 0; i < sr.length; i++) {
SaveResult result = sr[i];
if (result.isSuccess()) {
retStatus = "Success Created Document " + docName;
this.setLastSFID(result.getId().getValue()); //stores sfid
// for saving
// to property
// of new
// document
} else {
Error[] ers = (Error[]) result.getErrors();
retStatus = "Error:";
for (int j = 0; j < ers.length; j++) {
Error error = ers[j];
retStatus += error.getMessage() + ";";
System.out.println("error.getStatusCode():"
+ error.getStatusCode());
echo(retStatus);
}
}
}
} catch (UnexpectedErrorFault uef) {
echo(uef.getExceptionMessage());
} catch (Exception ex) {
retStatus = "\nFailed to create folder, " + docName
+ ", error message was: \n" + ex.getMessage();
echo(retStatus);
}

return retStatus;
}

Everything is fine until I look at the SaveResult and see the error.

Here is the log for the applicable section.

SalesForceContentManager:CreateDocument
SalesForceContentManager:docName=enterprise.wsdl
SalesForceContentManager:File doc=C:\DOCUME~1\MICHAE~1\LOCALS~1\Temp\ajcs31224.tmp
SalesForceContentManagerescription=From SFCM
SalesForceContentManager:authorId=00530000000cyikAAA
SalesForceContentManager:folderId=00l30000000h0i0AAA
SalesForceContentManager:un=ollie@someplace.com
SalesForceContentManager:pw=LETMEIN
SalesForceContentManager:Creating the binding to the web service...
SalesForceContentManager:LOGGING IN NOW....
SalesForceContentManager:
The session id is: 3Zx8p0zhPhQtWnxCSu8vx2ghC1A3EoTCWdz56hjDXXKx26smrLqeI_BX1E97gD0qV8TI9e.M4mBvhyqgMvY5PzteaH1xyGi1
SalesForceContentManager:
The new server url is: https://na1-api.salesforce.com/services/Soap/c/5.0
SalesForceContentManager:
Getting server's timestamp...
SalesForceContentManager:Time stamp on server: May 20, 2005 10:17:42 PM GMT
error.getStatusCode():INVALID_TYPE_ON_FIELD_IN_RECORD
SalesForceContentManager:Error:Body: value not of required type: 10;

I can create Folders and other SObjects and as I said this was working a while ago.

I know its something simple but I dunno. The sforce code was generated by the eclipse wsdl plugin from the enterprise.wsdl generated and downloaded TODAY.

Thanks in advance for your help.

Ollie
does anybody have ant configured to generate the sforce client code?

I found http://ws.apache.org/axis/java/ant/ant.html so I am looking for someone that already has this working...lazy for sure, but perhaps others would be interested.

thanks.

Message Edited by Ollie on 05-18-2005 11:10 AM

The log shows:

The new server url is: https://na1-api.salesforce.com/services/Soap/c/3.0
SalesForceContentManager:
Getting server's timestamp...
SalesForceContentManager:Time stamp on server: May 18, 2005 4:30:54 PM GMT
error.getStatusCode():INVALID_TYPE_ON_FIELD_IN_RECORD
SalesForceContentManager:Error:Body: value not of required type: 115;


The head of one of the applicable .java source files is:

/**
* Document.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package com.sforce.soap.enterprise.sobject;

public class Document extends com.sforce.soap.enterprise.sobject.SObject implements java.io.Serializable {
private com.sforce.soap.enterprise.ID authorId;
private byte[] body;

...

My code accesses and binds ok and creates a Document Folder ok but when I use the following code to try to upload a document into a folder I get an error that contains the subject of this message.

my code looks like:

public String CreateDocument(String docName, File doc, String description,
ID authorId, ID folderId) {
echo("CreateDocument");
echo("docName=" + docName);
echo("File doc=" + doc.toString());
echo("Description=" + description);
echo("authorId=" + authorId.getValue());
echo("folderId=" + folderId.getValue());

String retStatus = "Success";
int fileLength = (int) doc.length();
byte[] body = new byte[fileLength];

//Verify that we are already authenticated, if not
//call the login function to do so
if (!loggedIn) {
if (!login()) {
return "Bad SalesForce Login";
}
}
try {
FileInputStream fis = new FileInputStream(doc);
fileLength = fis.read(body);
} catch (FileNotFoundException e) {
retStatus = "Error Failed to CreateDocument:"
+ e.getLocalizedMessage();
e.printStackTrace();
return retStatus;
} catch (IOException e1) {
retStatus = "Error Failed to CreateDocument:"
+ e1.getLocalizedMessage();
e1.printStackTrace();
return retStatus;
}

if (authorId == null) {
//default to current user
authorId = this.loginResult.getUserId();
}

try {
Document document;
SObject[] accs = new SObject[1];
document = new Document();

document.setName(docName);
document.setBody(body);
document.setDescription(description);
document.setAuthorId(authorId);
document.setFolderId(folderId);

accs[0] = document;

//create the object(s) by sending the array to the web service
SaveResult[] sr = binding.create(accs);
...

This code was working at one time and the authorId and folderId are valid, and the body is a byte[] that contains the document, albeit not base64 encoded...

Do I need to base64 encode the body?
Do I need to update my sforce sources?

Message Edited by Ollie on 05-18-2005 10:18 AM

I want to create a document through the API. I can create a folder and get the ID from the result, but I cannot leave the authorId out and I don't have the 18 char ID for myself to insert into the authorId field to create the document.

Seems to me that the authorId value should default to the user doing the create...but that's just the way I would do it.
I have created a folder in the SalesForce.com UI, now I want to search for that folder through the API to get its ID so I can use the API to add documents to that folder.

I tried modifying the searchexample with

sr = binding.search("find " + folderName +
"in name fields " +
"returning " +
"folder(id)");

where folderName is the name of the folder created in the Web UI.

can I just do a search (or query) to get all the folders? I can loop through those and match my name.
I am trying to add a document to a document folder with the following code, and I am calling this method from a JUnit test script that is passing in hard coded values and I am getting

error.getStatusCode() = INVALID_CROSS_REFERENCE_KEY

I obtained the authorId and folderId's from a view source of the Web Pages associated with the folder where I want to create the document. I have already created a folder on salesforce using a similar method called CreateFolder().

I cannot find a reference for this status code in the documentation or in this forum.

public String CreateDocument(String docName, File doc, String description, ID authorId, ID folderId) {
String retStatus = "Success";
int fileLength = (int)doc.length();
byte[] body = new byte[fileLength];

//Verify that we are already authenticated, if not
//call the login function to do so
if (!loggedIn) {
if (!login()) {
return "Bad SalesForce Login";
}
}
try {
FileInputStream fis = new FileInputStream(doc);
fileLength = fis.read(body);
}
catch (FileNotFoundException e) {
retStatus = "Error Failed to CreateDocument:" + e.getLocalizedMessage();
e.printStackTrace();
return retStatus;
}
catch (IOException e1) {
retStatus = "Error Failed to CreateDocument:" + e1.getLocalizedMessage();
e1.printStackTrace();
return retStatus;
}


try {
Document document;
SObject[] accs = new SObject[1];
document = new Document();

document.setName(docName);
document.setBody(body);
document.setDescription(description);
document.setAuthorId(authorId);
document.setFolderId(folderId);

accs[0] = document;

//create the object(s) by sending the array to the web service
SaveResult[] sr = binding.create(accs);
for (int i = 0; i < sr.length; i++) {
SaveResult result = sr[i];
if (result.isSuccess()){
retStatus = "Success Created Document " + docName;
} else {
Error[] ers = (Error[]) result.getErrors();
retStatus = "Error:";
for (int j = 0; j < ers.length; j++) {
Error error = ers[j];
retStatus += error.getMessage() + ";";
System.out.println("error.getStatusCode():" + error.getStatusCode());
echo(retStatus);
}
}
}
}
catch (UnexpectedErrorFault uef) {
echo(uef.getExceptionMessage());
}
catch (Exception ex) {
retStatus = "\nFailed to create folder, " + docName + ", error message was: \n"
+ ex.getMessage();
echo(retStatus);
}

return retStatus;
}
I cannot find an example of code that uploads a Document, the Samples does not.

Something seems to have changed since version 3 regarding the Document upload. The following used to work fine in version 3 but does not work in version 5.

public String CreateDocument(
String docName,
File doc,
String description,
ID authorId,
ID folderId) {

echo("CreateDocument");
echo("docName=" + docName);
echo("File doc=" + doc.toString());
echo("Description=" + description);
echo("authorId=" + authorId.getValue());
echo("folderId=" + folderId.getValue());

String retStatus = "Success";
int fileLength = (int) doc.length();
byte[] body = new byte[fileLength];
//Verify that we are already authenticated, if not
//call the login function to do so
if (!loggedIn) {
if (!login()) {
return "Bad SalesForce Login";
}
}
try {
FileInputStream fis = new FileInputStream(doc);
fileLength = fis.read(body);
} catch (FileNotFoundException e) {
retStatus = "Error Failed to CreateDocument:"
+ e.getLocalizedMessage();
e.printStackTrace();
return retStatus;
} catch (IOException e1) {
retStatus = "Error Failed to CreateDocument:"
+ e1.getLocalizedMessage();
e1.printStackTrace();
return retStatus;
}

if (authorId == null) {
//default to current user
authorId = this.loginResult.getUserId();
}

try {
Document document;
SObject[] accs = new SObject[1];
document = new Document();

document.setName(docName);
document.setBody(body);
document.setDescription(description);
document.setAuthorId(authorId);
document.setFolderId(folderId);

accs[0] = document;

//create the object(s) by sending the array to the web service
SaveResult[] sr = binding.create(accs);
for (int i = 0; i < sr.length; i++) {
SaveResult result = sr[i];
if (result.isSuccess()) {
retStatus = "Success Created Document " + docName;
this.setLastSFID(result.getId().getValue());
//stores sfid
// for saving
// to property
// of new
// document
} else {
Error[] ers = (Error[]) result.getErrors();
retStatus = "Error:";
for (int j = 0; j < ers.length; j++) {
Error error = ers[j];
retStatus += error.getMessage() + ";";
System.out.println("error.getStatusCode():"
+ error.getStatusCode());
echo(retStatus);
}
}
}
} catch (UnexpectedErrorFault uef) {
echo(uef.getExceptionMessage());
} catch (Exception ex) {
retStatus = "\nFailed to create folder, " + docName
+ ", error message was: \n" + ex.getMessage();
echo(retStatus);
}

return retStatus;
}
I had adding a document to a document folder working in version 3 and now it is not working with version 5.

Using Tomcat 5 and a web application, as well as a JUnit test script to exercise the following code.

public String CreateDocument(String docName, File doc, String description,
ID authorId, ID folderId) {
echo("CreateDocument");
echo("docName=" + docName);
echo("File doc=" + doc.toString());
echo("Description=" + description);
echo("authorId=" + authorId.getValue());
echo("folderId=" + folderId.getValue());

String retStatus = "Success";
int fileLength = (int) doc.length();
byte[] body = new byte[fileLength];

//Verify that we are already authenticated, if not
//call the login function to do so
if (!loggedIn) {
if (!login()) {
return "Bad SalesForce Login";
}
}
try {
FileInputStream fis = new FileInputStream(doc);
fileLength = fis.read(body);
} catch (FileNotFoundException e) {
retStatus = "Error Failed to CreateDocument:"
+ e.getLocalizedMessage();
e.printStackTrace();
return retStatus;
} catch (IOException e1) {
retStatus = "Error Failed to CreateDocument:"
+ e1.getLocalizedMessage();
e1.printStackTrace();
return retStatus;
}

if (authorId == null) {
//default to current user
authorId = this.loginResult.getUserId();
}

try {
Document document;
SObject[] accs = new SObject[1];
document = new Document();

document.setName(docName);
document.setBody(body);
document.setDescription(description);
document.setAuthorId(authorId);
document.setFolderId(folderId);

accs[0] = document;

//create the object(s) by sending the array to the web service
SaveResult[] sr = binding.create(accs);
for (int i = 0; i < sr.length; i++) {
SaveResult result = sr[i];
if (result.isSuccess()) {
retStatus = "Success Created Document " + docName;
this.setLastSFID(result.getId().getValue()); //stores sfid
// for saving
// to property
// of new
// document
} else {
Error[] ers = (Error[]) result.getErrors();
retStatus = "Error:";
for (int j = 0; j < ers.length; j++) {
Error error = ers[j];
retStatus += error.getMessage() + ";";
System.out.println("error.getStatusCode():"
+ error.getStatusCode());
echo(retStatus);
}
}
}
} catch (UnexpectedErrorFault uef) {
echo(uef.getExceptionMessage());
} catch (Exception ex) {
retStatus = "\nFailed to create folder, " + docName
+ ", error message was: \n" + ex.getMessage();
echo(retStatus);
}

return retStatus;
}

Everything is fine until I look at the SaveResult and see the error.

Here is the log for the applicable section.

SalesForceContentManager:CreateDocument
SalesForceContentManager:docName=enterprise.wsdl
SalesForceContentManager:File doc=C:\DOCUME~1\MICHAE~1\LOCALS~1\Temp\ajcs31224.tmp
SalesForceContentManagerescription=From SFCM
SalesForceContentManager:authorId=00530000000cyikAAA
SalesForceContentManager:folderId=00l30000000h0i0AAA
SalesForceContentManager:un=ollie@someplace.com
SalesForceContentManager:pw=LETMEIN
SalesForceContentManager:Creating the binding to the web service...
SalesForceContentManager:LOGGING IN NOW....
SalesForceContentManager:
The session id is: 3Zx8p0zhPhQtWnxCSu8vx2ghC1A3EoTCWdz56hjDXXKx26smrLqeI_BX1E97gD0qV8TI9e.M4mBvhyqgMvY5PzteaH1xyGi1
SalesForceContentManager:
The new server url is: https://na1-api.salesforce.com/services/Soap/c/5.0
SalesForceContentManager:
Getting server's timestamp...
SalesForceContentManager:Time stamp on server: May 20, 2005 10:17:42 PM GMT
error.getStatusCode():INVALID_TYPE_ON_FIELD_IN_RECORD
SalesForceContentManager:Error:Body: value not of required type: 10;

I can create Folders and other SObjects and as I said this was working a while ago.

I know its something simple but I dunno. The sforce code was generated by the eclipse wsdl plugin from the enterprise.wsdl generated and downloaded TODAY.

Thanks in advance for your help.

Ollie
does anybody have ant configured to generate the sforce client code?

I found http://ws.apache.org/axis/java/ant/ant.html so I am looking for someone that already has this working...lazy for sure, but perhaps others would be interested.

thanks.

Message Edited by Ollie on 05-18-2005 11:10 AM

The log shows:

The new server url is: https://na1-api.salesforce.com/services/Soap/c/3.0
SalesForceContentManager:
Getting server's timestamp...
SalesForceContentManager:Time stamp on server: May 18, 2005 4:30:54 PM GMT
error.getStatusCode():INVALID_TYPE_ON_FIELD_IN_RECORD
SalesForceContentManager:Error:Body: value not of required type: 115;


The head of one of the applicable .java source files is:

/**
* Document.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package com.sforce.soap.enterprise.sobject;

public class Document extends com.sforce.soap.enterprise.sobject.SObject implements java.io.Serializable {
private com.sforce.soap.enterprise.ID authorId;
private byte[] body;

...

My code accesses and binds ok and creates a Document Folder ok but when I use the following code to try to upload a document into a folder I get an error that contains the subject of this message.

my code looks like:

public String CreateDocument(String docName, File doc, String description,
ID authorId, ID folderId) {
echo("CreateDocument");
echo("docName=" + docName);
echo("File doc=" + doc.toString());
echo("Description=" + description);
echo("authorId=" + authorId.getValue());
echo("folderId=" + folderId.getValue());

String retStatus = "Success";
int fileLength = (int) doc.length();
byte[] body = new byte[fileLength];

//Verify that we are already authenticated, if not
//call the login function to do so
if (!loggedIn) {
if (!login()) {
return "Bad SalesForce Login";
}
}
try {
FileInputStream fis = new FileInputStream(doc);
fileLength = fis.read(body);
} catch (FileNotFoundException e) {
retStatus = "Error Failed to CreateDocument:"
+ e.getLocalizedMessage();
e.printStackTrace();
return retStatus;
} catch (IOException e1) {
retStatus = "Error Failed to CreateDocument:"
+ e1.getLocalizedMessage();
e1.printStackTrace();
return retStatus;
}

if (authorId == null) {
//default to current user
authorId = this.loginResult.getUserId();
}

try {
Document document;
SObject[] accs = new SObject[1];
document = new Document();

document.setName(docName);
document.setBody(body);
document.setDescription(description);
document.setAuthorId(authorId);
document.setFolderId(folderId);

accs[0] = document;

//create the object(s) by sending the array to the web service
SaveResult[] sr = binding.create(accs);
...

This code was working at one time and the authorId and folderId are valid, and the body is a byte[] that contains the document, albeit not base64 encoded...

Do I need to base64 encode the body?
Do I need to update my sforce sources?

Message Edited by Ollie on 05-18-2005 10:18 AM

I wrote a small program to login and then do a sfdc.describe.  Everything works fine, and i can actually output the fields coming back.  However, when i move this code into a servlet( same exact code ) and the code is executed from a servlet, i get the following error:

org.apache.xmlrpc.XmlRpcException: user not valid

I also checked the error code and it is 1112 ( invalid login )

Why is it working when it's running as a standalone program but not working when i'm in a servlet?  Anyone else encounter this?

I'm using apache's XmlRpcClient class.

 

 

 

  • October 10, 2003
  • Like
  • 0