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相关联。
一如既往,非常感谢任何帮助。