• Jean Mendez 3
  • 0 Points
  • Member since 2017

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1

I am attempting to design a trigger to insert new Content (ContentVersion) when a new attachment is added.  We are switching from using attachments to Content.  However, the majority of files are added through via the desktop email programs (using Maildrop and Salesforce for Outlook) so I have written a trigger to copy the file to Content. Unfortunately, I can't seem to get it working for the body/file.  After insert, the attachment body referenced seems to be in some temp location and before insert the attachment has yet to be loaded so it can't find any body.


When I use before insert, I get the following error: Error: Required fields are missing: [Body]


When I use after insert, I get the following error: Error: Apex trigger NewAttachment caused an unexpected exception, contact your administrator: NewAttachment: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, java.io.FileNotFoundException: /tmp/bvf/cmp_5879646311450005283.def (No such file or directory): []: Trigger.NewAttachment: line 56, column 5


Any suggestions?


Here's my trigger:


trigger NewAttachment on Attachment (before insert) {

List<Attachment_Tracker__c> tracker = new List<Attachment_Tracker__c>();
List<ContentVersion> CV = new List<ContentVersion>();

for (Integer i = 0; i < Trigger.new.size(); i++) {
	String AttParent = Trigger.new[i].ParentId;
	String ContentDesc = '';
	String FileName = Trigger.new[i].Name;
	Blob BodyData = Trigger.new[i].Body;
		// Check to make sure there is a ParentID for the attachment and if records already exist for this Parent
		if(AttParent!=null) {
			String AttParSub = AttParent.substring(0,15);
			Map<String,ID> TrackMap = new Map<String,ID>();
			for (Attachment_Tracker__c c: [select Contact__c, id from Attachment_Tracker__c Where Contact__c =:AttParent] ) {
				TrackMap.put(c.Contact__c, c.id);

			// Make sure the ParentID is a ContactID and it isn't already in the Attachment Tracker
			if(AttParent.startsWith('003')) {
				//Get the Contact Name
				Contact FullName = [SELECT Name, AccountID, Title FROM Contact WHERE Id = :AttParent];
				ContentDesc = FullName.Name+' ('+FullName.Title+' at '+FullName.AccountID+')';
				String ContName = FullName.Name;
				String ContAcct = FullName.AccountID;
				String ContTit = FullName.Title;
				// Get ID to set the Workspace to Resumes
				ContentWorkspace Workspace = [select id from ContentWorkspace where name = 'Resumes' limit 1];

				// First Copy Attachemnet to Content //
			    CV.add(new ContentVersion(
			    	FirstPublishLocationId = Workspace.id, 
			    	// Title = Trigger.new[i].Name,
			    	Contact__c = Trigger.new[i].ParentId,
			    	PathOnClient = Trigger.new[i].Name, 
			    	VersionData = BodyData
			    	) );

				// Now Update the Attachment Tracker //
					tracker.add(new Attachment_Tracker__c(
		            Contact__c = Trigger.new[i].ParentId,
					File_Name__c = Trigger.new[i].Name,
					ContactName__c = ContName,
					Job_Title__c = ContTit,
	                ) ) ;
    insert tracker;
	insert CV;