简体   繁体   English

Salesforce 自定义对象和附件

[英]Salesforce custom object and attachments

Ok, I've been tasked with creating a custom salesforce object that saves some data.好的,我的任务是创建一个自定义的 salesforce 对象来保存一些数据。 That's the easy part.这是容易的部分。

Where I'm struggling is adding a piece to upload files associated with the record created in the custom object.我正在努力的地方是添加一个片段来上传与在自定义对象中创建的记录相关联的文件。

The custom object is essentially a form with fields on it.自定义对象本质上是一个带有字段的表单。 But I also need to upload relevant files.但我也需要上传相关文件。

So I built the form part of the custom object.所以我构建了自定义对象的表单部分。 I used an Apex class and a VisualForce page for the "upload" part.我为“上传”部分使用了 Apex 类和 VisualForce 页面。

Unfortunately, in the course of testing, I keep getting errors on the VisualForce/Apex piece.不幸的是,在测试过程中,我在 VisualForce/Apex 部分不断出现错误。

The errors I get are the following:我得到的错误如下:

Parent: Parent ID: id value of incorrect type: 00590000000yBOmAAM父级:父级 ID:类型错误的 id 值:00590000000yBOmAAM

And my Apex class looks like this:我的 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;
    }
}

I am very much a novice at SalesForce.我是 SalesForce 的新手。 I think the problem is the ParentID line, but I don't know exactly how to fix it.我认为问题是 ParentID 行,但我不知道如何解决它。

What I'm trying to do is to be able to upload/attach documents to a record, while I'm filling out the form for the record.我正在尝试做的是能够在我填写记录表格的同时将文档上传/附加到记录中。

My VisualForce looks like this:我的 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>

So what could I be doing wrong?那么我可能做错了什么? My other thought is that there's simply no way to upload appropriate files until after the record has been created.我的另一个想法是,在创建记录之前根本无法上传适当的文件。 Even then, I think I'd still need to know how to associate it with the Parent ID.即便如此,我想我仍然需要知道如何将它与父 ID 相关联。

Any help is greatly appreciated, as always.一如既往,非常感谢任何帮助。

This is no longer relevant.这不再相关。 The project was reassigned to a different group.该项目被重新分配给不同的组。

Anyone landing here check Link to tutorial任何登陆这里的人检查链接到教程

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM