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
Ollie.ax81Ollie.ax81 

How about a Document upload Example???

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;
}
SuperfellSuperfell
what error do you get ?
Ollie.ax81Ollie.ax81
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;

See the Java Development Forum Thread >>> INVALID_TYPE_ON_FIELD_IN_RECORD

The curious thing about my view on this, is that the sforce api developers MUST have tests for the api to test all the functionality, so what would be the harm in including those tests as examples to us developers? The Samples seems way to skimpy.

I don't mean to preach but the easier you make it for developers and customers to integrate sForce the faster it will grow.

Forums are good, but for developers a Wiki is better for sharing code and solutions.

Thanks ahead of time for your help whatever it maybe.

Ollie
Ollie.ax81Ollie.ax81
The same Document loading I had working in version 3 doesn't work in version 5 and I can't find any examples! I am dead sure sforce has test scripts they use to test Document SObject binding. If not that might explain why it doesn't work any more, i.e. it isn't tested. If so, how about putting that code up here as an example so we mushrooms out in the field can see how it is supposed to be done.
SuperfellSuperfell
This works fine for me (C#)

static void Main(string[] args)
{
if (args.Length != 3)
{
Console.WriteLine("Docloader usage : docloader.exe ");
return;
}

sforce.SforceService svc = new sforce.SforceService();
sforce.LoginResult lr = svc.login(args[1], args[2]);
svc.Url = lr.serverUrl ;
svc.SessionHeaderValue = new sforce.SessionHeader();
svc.SessionHeaderValue.sessionId = lr.sessionId;

sforce.QueryResult qr = svc.query("Select Id From Folder");

sforce.Document d = new sforce.Document();
d.FolderId = qr.records[0].Id;

using (FileStream fs = File.OpenRead(args[0]))
{
d.Body = new byte[fs.Length];
fs.Read(d.Body, 0, d.Body.Length);
}
d.Name = (new FileInfo(args[0])).Name;

sforce.SaveResult sr = svc.create(new sforce.sObject[] {d})[0];
if (sr.success)
{
Console.WriteLine("Document created with Id " + sr.id);
}
else
{
Console.WriteLine("Failed to create document: " + sr.errors[0].message);
}
}
Ollie.ax81Ollie.ax81
Thanks,

Despite the C# vs. Java differences and some variable names I don't see much difference between your example and mine.

condensed

int fileLength = (int) doc.length();
byte[] body = new byte[fileLength];

if (!loggedIn) {
if (!login()) {
return "Bad SalesForce Login";
}
}
try {
FileInputStream fis = new FileInputStream(doc);
fileLength = fis.read(body);
} catch (FileNotFoundException e) {
e.printStackTrace();
return retStatus;
} catch (IOException e1) {
e1.printStackTrace();
return retStatus;
}

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

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

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

accs[0] = document;

SaveResult[] sr = binding.create(accs);
} catch (UnexpectedErrorFault uef) {
log.error(uef.getExceptionMessage());
} catch (Exception ex) {
retStatus = "\nFailed to create folder, " + docName
+ ", error message was: \n" + ex.getMessage();

}

This login is successful, and the logs show valid authorId and folderIds


SalesForceContentManager:authorId=00530000000cyikAAA
SalesForceContentManager:folderId=00l30000000h0i0AAA

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 18, 2005 4:30:54 PM GMT

but it fails with:

error.getStatusCode():INVALID_TYPE_ON_FIELD_IN_RECORD
SalesForceContentManager:Error:Body: value not of required type: 10;
SuperfellSuperfell
I think you'll need to get a capture of the actual soap request to see what's going on.
Ollie.ax81Ollie.ax81
Yup, working on that now. I am going the route of the message handler and client-config.wsdd

Can't seem to get the axis client to see/use the client-config.wsdd but from what I can google I am not alone.

Will try the Call.setClientHandlers().

ARG!

Message Edited by Ollie on 05-26-2005 04:17 PM

Ollie.ax81Ollie.ax81
The problem wasn't with the code that created the create operation, it was ok, but it was down deeper where I was trying to setup logging to see what was going on.

I was trying to use my own Handler to log the messages, but because of a class loader problem/conflict with another webapp that needed to have axis loaded by the tomcat common loader from /common/lib/ my client-config.wsdd wasn't happy in /common/classes/ nor was it even read when it was in webapp/WEB-INF/classes/

So I tried to do logging directly from the axis generated client classes and I won't tell you the mistake I made ::embarrased:: but a hint is that you can't put a 'this' in a default constructor....;-)

So when I cleared that mistake, it started working fine.

I have another thread going that relates to setting the handler and will put the working example over there.