function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
WitteWitte 

How to save document and custom object.

Hello,

 

Can anyone help me? Im trying to save the input for a custom object. I also want to use the document object to save a document.

 

The Visual Force page

 

  <apex:page standardcontroller=Topics__c" extensions="Topics_Extension">
  <apex:form >
    <apex:pageBlock title="Topics">
                <table width="400px" >
                        <tr>
                                <td>Name:</td>
                                <td><apex:inputField value="{!Topics.Name__c}" required="true" /></td>
                        </tr>

                        <tr>
                                <td>Document:</td>
                                <td><apex:inputFile value="{!Topicsbody}" filename="{!topicsname}" required="true" /></td>
                        </tr> 

               </table>

               <apex:pageBlockButtons >
                     <apex:commandButton value="Save" action="{!saveTopics}"/>
               </apex:pageBlockButtons>

       </apex:pageBlock>

      </apex:form>
</apex:page>

 

The extension - Controller

public with sharing class Topics_Extension {
    public Topics__c Topic;
    ApexPages.StandardController controller;
   
    //document variables
    public blob Topicsbody;
    public string topicsname;
   
    public Topics_Extension(ApexPages.StandardController controller) {
    this.topics = (Topics__c)controller.getRecord();
    this.controller = controller;
     }

   
    public PageReference saveTopics() { 
        controller.save();  
        return null;
    }
}

 

How can i save both. so a new topics is inserted and the document is inserted in documents.

 

Thanking you in front,

Paul

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

Hi,

 

Try this,

 

http://salesforceworld.blogspot.com/2011/06/save-attachment-in-apex.html

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog

All Answers

ritika@developerforceritika@developerforce

Hi,

 

You can simply bind the inputField to a document and save it. This is how -

 

<apex:page standardcontroller=Topics__c" extensions="Topics_Extension">
  <apex:form >
    <apex:pageBlock title="Topics">
                <table width="400px" >
                        <tr>
                                <td>Name:</td>
                                <td><apex:inputField value="{!Topics.Name__c}" required="true" /></td>
                        </tr>

                        <tr>
                                <td>Document:</td>
                                <td><apex:inputFile value="{!topicDoc.body}" filename="{!topicDoc.Name}" required="true" /></td>
                        </tr> 

               </table>

               <apex:pageBlockButtons >
                     <apex:commandButton value="Save" action="{!saveTopics}"/>
               </apex:pageBlockButtons>

       </apex:pageBlock>

      </apex:form>
</apex:page>

 

The extension - Controller

public with sharing class Topics_Extension {
    public Topics__c Topic;
    ApexPages.StandardController controller;
   
    // CV/resume variables
   public Document topicDoc;   

  
    public Topics_Extension(ApexPages.StandardController controller) {
       this.topics = (Topics__c)controller.getRecord();
       this.controller = controller;

       topicDoc = new Document();
     }

   
    public PageReference saveTopics() { 
        controller.save();

        if (topicDoc.Body != null) {

            topicDoc.folderId = '<SOME ID>' // Give a Folder Id here, where you want the document to be inserted. Mandatory

            topicDoc.Description = 'Some Description' // Add soem description - although this is not a mandatory field

            insert topicDoc;

        }

        return null;
    }
}



Hope this helps.

 

WitteWitte

Hi Ritika,

 

Thank you for your reply.

i still have troubles doing so. I get a error.

Compile Error: DML requires SObject or SObject list type: Document at line (insert topicDoc).

 

Do you know how i can fix this.

 

thanks,

Paul

ritika@developerforceritika@developerforce

Hi,

 

Ideally, this should not throw an error, I have inserted documents like this. Please make sure you have included the topicDoc as a Document (this is a SObject type only). Please share your code here.

 

 

ritika@developerforceritika@developerforce

Hi,

Please also make sure to include the getter and setter for topicDoc, to have is accessible from VF page.

 

 

WitteWitte

Hi ritika,

 

Again thnaks for your reponse. This is my code some parts are in comment this because i cannot save the code for some reasons.

VF page

<apex:page StandardController="Topics__c" extensions="Topics_Extension">
    <apex:form >
    <apex:pageBlock title="Topics">
                <table width="400px" >
                        <tr>
                                <td>Name:</td>
                                <td><apex:inputField value="{!Topics__c.Name__c}" required="true" /></td>
                        </tr>
                        <!--
                        <tr>
                                <td>Document:</td>
                                <td><apex:inputFile value="{!topicDoc.body}" filename="{!topicDoc.name}" required="true" /></td>
                        </tr>
                        -->
               </table>
               <apex:pageBlockButtons >
                     <apex:commandButton value="Save" action="{!saveTopics}"/>
               </apex:pageBlockButtons>
       </apex:pageBlock>
       </apex:form>
</apex:page>

 

The Extension

public with sharing class Topics_Extension {
    public Topics__c Topics;
    ApexPages.StandardController controller;
  
    // CV/resume variables
    public Document topicDoc;  

    public Topics_Extension(ApexPages.StandardController controller) {
          this.topics = (Topics__c)controller.getRecord();
          this.controller = controller;
          topicDoc = new Document();
    }

   

    public Document gettopicDoc(){
         return topicDoc;
    }
  
    public PageReference saveTopics() {
         controller.save();
         //if (topicDoc.Body != null) {
            //topicDoc.folderId = 'TopicDoc'; // Give a Folder Id here, where you want the document to be inserted. Mandatory
            //topicDoc.Description = 'Some Description'; // Add soem description - although this is not a mandatory field
            //insert topicDoc;
        //}

        return null;
    }
}

 

Thank you for your help.

 

Greetings,

Paul

Chamil MadusankaChamil Madusanka

Hi,

 

Try this,

 

http://salesforceworld.blogspot.com/2011/06/save-attachment-in-apex.html

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog

This was selected as the best answer
ritika@developerforceritika@developerforce

Hi,

 

You need to expose the topicDoc for the VF page -

 

public Document topicDoc {get; set;}

 

Then uncomment the portion in VF page and try to save.

 

Hopefully this should work

 

 

WitteWitte

Hi Rikita,

Both thanks for your reply, but i still struggle with this.

 

i still gives an error. It says Unkown Property"'Document.name'

 

Do your know a answer to this.

 

Greetings,

Paul

WitteWitte

Hi Chamil,

 

Thank you for your reply,

 

Ive tryed out your code but this seems not to work. I get al kind of save errors. if i create a custom object Applicant.

If i look at your code there is some missing a well because it rerenders some part wich is not there.

 

can you help me with this.

 

Greetings,

Paul

ritika@developerforceritika@developerforce

Hi,

 

I am unable to understand the exact issue you are facing.

All you need to do is -

1. Create a document variable, and give get and set access both (I saw in you code you only wrote the getter)

2. Bind an inputfile to the documents body and name

3. In the save method, provide the folderId (or you can do that before, in constructor also) and insert the document

 

Please see this for your reference - http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_inputFile.htm

 

Hope this helps.

 

WitteWitte

Dear Rikita,

 

Thank you for your, replys through the proces. ive tried to make the solution of Chamil work. The second time i entered the code in my case it worked. Never theless i want to thank you for the replies you gave me.

 

Greetings,

Paul