I have a very simple update here:
Document emailDoc = emailLogoDoc[0];
emailDoc.Body = newLogoData.emailLogo;
emailDoc.Url = Url.getSalesforceBaseUrl().getProtocol() + '://'+System.Url.getSalesforceBaseUrl().getHost().remove('-api')+'/servlet/servlet.ImageServer?id='+emailDoc.Id+'&oid='+UserInfo.getOrganizationId();
try{
update emailDoc;
}catch(DmlException e){
system.debug(e.getDmlMessage(0) + ', Line: ' + e.getLineNumber());
}
If the document already exists (checking against the name). When it does exist, and is updating, I get the following error and the record fails to update
This Document Unique Name already exists or has been previously used. Please choose a different name.
This makes no sense to me... of course the Document Unique Name already exists, that is why it is being updated, not inserted! I am not changing the name, making a new document with the same name, etc... So I do not get why this error is occurring.
How do you properly update existing documents and insert new documents through apex? All I am really concerned with is setting the Url and Body and IsPublic to true.
(I am making an interface to allow users to upload images to be used for various things in other visualforce pages, so if an image already exists (then a document exists for it), just update, but if it doesn't exist, then I want to create a new one in apex, which is why I am doing this).
UPDATE
I am now able to create and update these documents. I wasn't adding the MIME type, which it apparently needed to recognize what kind of image it was and how to render it I guess. So, in the document folder, the documents are what they should be after being created or edited.
But now, those changes are not being reflected on the VF page when I refresh the page after updating.
In my controller, I do this to grab the image URL:
public String siteLogoUrl{
get{
List<Document> siteLogoDoc = [SELECT Id, Name, Url, Body FROM Document WHERE Name = 'SchedulerSiteLogo'];
if(siteLogoDoc.size() > 0){
return Url.getSalesforceBaseUrl().getProtocol() + '://'+System.Url.getSalesforceBaseUrl().getHost().remove('-api')+'/servlet/servlet.ImageServer?id='+siteLogoDoc[0].Id+'&oid='+UserInfo.getOrganizationId();
//return siteLogoDoc.get(0).Url;
}
else{
return null;
}
}
}
Then do this in the VF page to render it:
<apex:image id="schedulerSiteLogo" value="{!siteLogoUrl}" rendered="{!siteLogoUrl != null}" title="Selected Logo" style="margin-left: 15px; margin-top: 15px; margin-bottom: 15px;" />
But it keeps displaying the old image and doesn't register the changes even though the changes took place in the actual document folder.
Is there a better way to pass a document to an apex:image? I couldn't figure out how to pass it the body of the document.
Another Update
Changing my URL to '/servlet/servlet.FileDownload?file=' + emailLogoDoc[0].Id; that my getter returns fixed this. The thing is that it takes a few minutes it seems for changes to actually take place. The files change in the document file, but you can reload right away and the images are not changed on the VF page. Then if you wait like 5 minutes and refresh, all the sudden everything is up to date and the images are changed how they should be......