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 

INVALID_CROSS_REFERENCE_KEY

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;
}
Ollie.ax81Ollie.ax81
I believe the problem may be with the authorId which is required but all I have is the 15 char ID, is this statuscode because the authorId is a 15 char vs. 18 char?
DevAngelDevAngel

Hi Ollie,

This error is typically associated with a deleted object.  To be sure the folder is not deleted, get it's id using the API first.

DevAngelDevAngel
Nope, the user id from the LoginResult is 18 chars.  In any event, the id fields accept 15 and 18.
Ollie.ax81Ollie.ax81
Well when I changed the authorId from 15 char to 18 char version and created the document, the error went away....so perhaps not all fields accept both...;-)
Ollie.ax81Ollie.ax81
for clarity I had obtained the authorId from doing a view source on the Web UI of SalesForce, not from the loginResult where I got the 18 char that worked.
akshay21akshay21

hello, i have got 15 digit ID but where can i geet 18 digit ID..?