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
Sabah FarhanaSabah Farhana 

Modify Apex/VF to add two attachments

Hi ,

I have the following controller and Vf page that adds one attachment

controller:

public with sharing class OpportunityAttachmentController{
  
    public Opportunity_Attachment__c objOppAtt {get; set;}
 
    public OpportunityAttachmentController(ApexPages.StandardController controller) {
        attach = new Attachment();
        objOppAtt = (Opportunity_Attachment__c)controller.getRecord();
     
    }

    public Attachment attach {get;set;}
    public Attachment attach1 {get;set;}
 
    //When user clicks upload button on Visualforce Page, perform upload/insert

    public ApexPages.Pagereference save(){
        if(attach.body==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file to upload'));
            return null;
        }
        insert objOppAtt;
     
      
        attach.ParentId = objOppAtt.id;
        insert attach;
      
       
        objOppAtt.URL__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach.id;
        objOppAtt.name = attach.Name;
        update objOppAtt;
      
        return new PageReference('/'+objOppAtt.Opportunity__c); 
    }
  
  
  
  
  
}


VF page:
<apex:page standardController="Opportunity_Attachment__c" extensions="OpportunityAttachmentController">
    <apex:sectionHeader title="New Opportunity Attachment" subtitle="Opportunity Attachment Edit"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockSection columns="1">
              
                <apex:outputField value="{!Opportunity_Attachment__c.Opportunity__c}"/>
                <apex:pageBlockSectionItem >Upload Contract<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" /></apex:pageBlockSectionItem>
              
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Upload" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


I want to add one more attachment  so i modify the vf as follows:
<apex:page standardController="Opportunity_Attachment__c" extensions="OpportunityAttachmentController">
    <apex:sectionHeader title="New Opportunity Attachment" subtitle="Opportunity Attachment Edit"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockSection columns="1">
             
                <apex:outputField value="{!Opportunity_Attachment__c.Opportunity__c}"/>
                <apex:pageBlockSectionItem >Upload Contract<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" />
            <apex:pageBlockSectionItem >Upload Attributes<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" />


</apex:pageBlockSectionItem>
             
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Upload" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


do i have to write a seperate controller,or i can modify the same?If modify the same ,how?

Please help.
VictorFelisbinoVictorFelisbino
You already created a second attachment variable in your controller, you just need to use it in the visualfoce page and add it to save it when the user clicks to upload:

Controller:
public with sharing class OpportunityAttachmentController{
 
    public Opportunity_Attachment__c objOppAtt {get; set;}

    public OpportunityAttachmentController(ApexPages.StandardController controller) {
        attach = new Attachment();
        attach1 = new Attachment();
        objOppAtt = (Opportunity_Attachment__c)controller.getRecord();
    
    }

    public Attachment attach {get;set;}
    public Attachment attach1 {get;set;}

    //When user clicks upload button on Visualforce Page, perform upload/insert

    public ApexPages.Pagereference save(){
       //assumming that you are requiring the user to attach something
       if(attach.name == null || attach1.name == null){
           
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file to upload'));
            return null;

       }
       if(attach.name != null){
            attach.ParentID = objOppAtt.id;
            try{
                insert attach;
            }catch(DMLException e){
                
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
                    return null;

            }
       }
       if(attach1.name != null){
            attach1.ParentID = objOppAtt.id;
            try{
                insert attach1;
            }catch(DMLException e){
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
                    return null;
            }
       }

     
      //you will need to create another field to hold your attachment link and your attachment name ---  or just use the attachments related list in the page layout.
        objOppAtt.URL1__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach.id;
        objOppAtt.name1__c = attach.Name;
        objOppAtt.URL2__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach1.id;
        objOppAtt.name2__c = attach1.Name;

        update objOppAtt;
     
        return new PageReference('/'+objOppAtt.Opportunity__c);
    }

}

visualforce page
<apex:page standardController="Opportunity_Attachment__c" extensions="OpportunityAttachmentController">
    <apex:sectionHeader title="New Opportunity Attachment" subtitle="Opportunity Attachment Edit"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockSection columns="1">
            
                <apex:outputField value="{!Opportunity_Attachment__c.Opportunity__c}"/>
                <apex:pageBlockSectionItem >Upload Contract<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" />
            <apex:pageBlockSectionItem >Upload Attributes<apex:inputFile required="true" value="{!attach1.body}" filename="{!attach1.name}" />

</apex:pageBlockSectionItem>
            
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Upload" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

hope this at least gives you an idea of how to attach two files at once...