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
iceberg4uiceberg4u 

Having problems with uploading files using APIs

This is the code soapbinding stub has been used with all of sforce packages.The IDE is Eclipse.

The BASE64 CODE is working fine.Whenever i try to upload files into my Documents I get an error message:

folder name id:CRDocuments
Folder was not null
The number of the document is:1

The body length is:22
The text in the body is:[B@1989b5
Errors were found on item 0
Error code is: INVALID_FIELD_FOR_INSERT_UPDATE
Error message: Unable to create/update fields: BodyLength. Please check the security settings of this field and verify that it is read/write for your profile.


The class file that I have used is:

com.sforce.soap.enterprise.sobject.Document document = new com.sforce.soap.enterprise.sobject.Document();
document.setName("Test");
document.setBody(Base64.base64Encode(strDoc).getBytes());
document.setBodyLength(Base64.base64Encode(strDoc).getBytes().length);
document.setFolder(folder);
document.setContentType("txt");
documents = new com.sforce.soap.enterprise.sobject.Document[1];
documents[0] = document;

Can anyone suggest what I am doing wrong??
SuperfellSuperfell
1. you're using the enterprise API, the client soap stack is going to base64 encode the array of bytes you set as the body for you, you're code is going to end up double encoding.
2. Don't set the bodyLength, the server will set it based on the body during the save.
3. "txt" is not a content-type, type "text/plain"
4. CRDocuments is not a valid FolderId, either query for a folderId from the Folder object, or use the UsersId, to create a document in the users private documents folder.
iceberg4uiceberg4u
Thanx Ron I implemented all your suggestions and even then I am still getting an error please could you see what I am doing wrong??
Here is the whole code:

private void createDocument()
    {
        try {
            getUserInput("Hit enter to upload file......");
            String strDoc = "this is a test file that I am trying to make. ";
            System.out.println(strDoc);
            //Retrieve Folder for file upload
            Folder folder = null;
            QueryResult qr = binding.query("Select Id, Name from Folder Where Name = 'TestSumiran'");
            if (qr.getSize() > 0)
            {
                folder = (Folder) qr.getRecords(0);
            }
            System.out.println("folder name id:" + folder.getName());
            //Create document object
            if(folder != null)
            {
                System.out.println("Folder was not null");
                com.sforce.soap.enterprise.sobject.Document document = new com.sforce.soap.enterprise.sobject.Document();
                document.setName("Test");
                document.setBody(strDoc.getBytes());
             
                document.setFolder(folder);
                document.setContentType("text/plain");
          
                documents = new com.sforce.soap.enterprise.sobject.Document[1];
                documents[0] = document;
               
                // create the object(s) by sending the array to the web service
               
                SaveResult[] sr = binding.create(documents);
                System.out.println("The number of the document is:"+sr.length);
                System.out.println();
                System.out.println("The body length is:"+document.getBodyLength());
                System.out.println("The text in the body is:"+document.getBody());
            
                for (int j = 0; j <sr.length; j++)
                {
                    if (sr[j].isSuccess())
                    {
                        System.out.println("A document was created with an id of: " + sr[j].getId());
                    }
                    else
                    {
                        for (int i = 0; i < sr[j].getErrors().length; i++)
                        {
                            com.sforce.soap.enterprise.Error err = sr[j].getErrors()[i];
                            System.out.println("Errors were found on item " + new Integer(j).toString());
                            System.out.println("Error code is: " + err.getStatusCode().toString());
                            System.out.println("Error message: " + err.getMessage());
                        }
                    }
                }
                getUserInput("\nHit return to continue...");
            }
        } catch (ApiFault af) {
            System.out
                    .println("\nFailed to create account, error message was: \n"
                            + af.getExceptionMessage());
            getUserInput("\nHit return to continue...");
        } catch (Exception ex) {
            System.out
                    .println("\nFailed to create account, error message was: \n"
                            + ex.getMessage());
            getUserInput("\nHit return to continue...");
        }
    }

and the error that i get is:

Errors were found on item 0
Error code is: INVALID_FIELD_FOR_INSERT_UPDATE
Error message: Document: bad field names on insert/update call: Folder

SuperfellSuperfell
set the FolderId field, not the Folder object.
iceberg4uiceberg4u
Thank you Simon.

Just one question->Can we upload pdf files???if yes what should i have as the content type??

and for the other developers here's the code for uploading files:-
private void createDocument()
{
try {
getUserInput("Hit enter to upload file......");
String strDoc = "This is a test words written in the file";
System.out.println(strDoc);
//Retrieve Folder for file upload
Folder folder = null;
QueryResult qr = binding.query("Select Id, Name from Folder Where Name = 'TestSumiran'");
if (qr.getSize() > 0)
{
System.out.println("The folder size is greater than zero");
folder = (Folder) qr.getRecords(0);
}
System.out.println("folder name id:" + folder.getName());
System.out.println("folder id is:"+folder.getId());
//Create document object
if(folder != null)
{
System.out.println("Folder was not null");
com.sforce.soap.enterprise.sobject.Document document = new com.sforce.soap.enterprise.sobject.Document();
document.setName("Important file");
document.setBody(strDoc.getBytes());

document.setFolderId(folder.getId());

document.setContentType("text/plain");

documents = new com.sforce.soap.enterprise.sobject.Document[1];
documents[0] = document;

// create the object(s) by sending the array to the web service

SaveResult[] sr = binding.create(documents);
System.out.println("The number of the document is:"+sr.length);
System.out.println();
System.out.println("The body length is:"+document.getBodyLength());
System.out.println("The text in the body is:"+document.getBody());

for (int j = 0; j {
if (sr[j].isSuccess())
{
System.out.println("A document was created with an id of: " + sr[j].getId());
}
else
{
for (int i = 0; i < sr[j].getErrors().length; i++)
{
com.sforce.soap.enterprise.Error err = sr[j].getErrors()[i];
System.out.println("Errors were found on item " + new Integer(j).toString());
System.out.println("Error code is: " + err.getStatusCode().toString());
System.out.println("Error message: " + err.getMessage());
}
}
}
getUserInput("\nHit return to continue...");
}
} catch (ApiFault af) {
System.out
.println("\nFailed to create account, error message was: \n"
+ af.getExceptionMessage());
getUserInput("\nHit return to continue...");
} catch (Exception ex) {
System.out
.println("\nFailed to create account, error message was: \n"
+ ex.getMessage());
getUserInput("\nHit return to continue...");
}
}
SuperfellSuperfell
http://www.google.com/search?client=safari&rls=en-us&q=pdf+content-type&ie=UTF-8&oe=UTF-8