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 

Apex Code modify 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.
 

Prabu MahalingamPrabu Mahalingam
Hi Sabah Farhana,
The code above looks fine. Are you facing any error or issues.? If so specify those.

Cheers..!