0
好吧,我的任務是創建一個可以保存一些數據的自定義salesforce對象。這是很容易的部分。Salesforce自定義對象和附件
我在掙扎的地方是添加一塊來上傳與自定義對象中創建的記錄相關聯的文件。
自定義對象本質上是一個帶有字段的表單。但我也需要上傳相關文件。
所以我構建了自定義對象的表單部分。我使用Apex類和VisualForce頁面來實現「上傳」部分。
不幸的是,在測試過程中,我一直在VisualForce/Apex上發現錯誤。
我得到的錯誤有以下幾種:
家長:家長ID:值類型不正確的ID:00590000000yBOmAAM
而且我的Apex類看起來是這樣的:
public with sharing class FileToDocument {
public Attachment attachment{
get{
if(attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}
public PageReference upload(){
attachment.OwnerId = UserInfo.getUserId();
attachment.ParentId = '00590000000yBOm';
attachment.IsPrivate = false;
try{
insert attachment;
}
catch(DMLException e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error Uploading Attachment'));
return null;
}
finally{
attachment = new Attachment();
}
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'Attachment Uploaded Successfully'));
return null;
}
}
我很SalesForce很多新手。我認爲問題是ParentID行,但我不知道如何解決它。
我想要做的是能夠上傳/附加文件到記錄,而我填寫表格的記錄。
我VisualForce看起來是這樣的:
<apex:page controller="FileToDocument">
<apex:form enctype="multipart/form-data">
<apex:pageMessages />
<apex:pageblock >
<apex:pageBlockButtons >
<apex:commandButton action="{!upload}" value="Attach"/>
</apex:pageBlockButtons>
<apex:pageBlockSection showHeader="false" columns="2" id="block1">
<apex:pageBlockSectionItem >
<apex:outputLabel value="File Name" for="fileName"/>
<apex:inputText value="{!attachment.name}" id="fileName"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="File" for="file"/>
<apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
但是,是什麼我是做錯了什麼?我的另一個想法是,只有在創建記錄之後才能上傳適當的文件。即使如此,我仍然需要知道如何將其與父母ID相關聯。
一如既往,非常感謝任何幫助。