You need to sign in to do that
Don't have an account?
Ollie.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;
}
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;
}
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
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);
}
}
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;
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
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.