You need to sign in to do that
Don't have an account?

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.
The code above looks fine. Are you facing any error or issues.? If so specify those.
Cheers..!