Friday, March 15, 2013

Adding Attachments to a Object


Ex:1

Visualforce Page:

<apex:page standardcontroller="Student1__c" extensions="studentcls">
    <apex:form >
        <apex:pageBlock >
            <apex:pageblockSection title="Student Details">
                <apex:inputField value="{!Student1__c.Name}"/>
                <apex:inputField value="{!Student1__c.Gender__c}"/>
                <apex:inputField value="{!Student1__c.Qualification__c}"/>
                <apex:inputField value="{!Student1__c.Skill_Set__c}"/>
                <apex:inputField value="{!Student1__c.Location__c}"/>
            </apex:pageblockSection>
            <apex:pageblockSection title="Upload Resume">
                <apex:pageblockSectionItem >
                    <apex:outputLabel value="Upload File" for="file"/>
                    <apex:inputFile value="{!attbody}" fileName="{!attname}"/>        
                </apex:pageblockSectionItem>       
            </apex:pageblockSection>
            <apex:pageblockButtons >
                <apex:commandButton value="Save" action="{!saverec}"/>
            </apex:pageblockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:

public with sharing class studentcls {
Student1__c s;
     public studentcls(ApexPages.StandardController controller) {
           s = (Student1__c)controller.getrecord(); 
     }
     public Blob attbody{get; set;}
     public String attname{get; set;}
     public Attachment attachment{get; set;}
     public PageReference saverec(){
         upsert s;
         if(attbody!=null)
         {
         List<attachment> la = [select id from attachment where parentid=:s.id];
         if(la.size()>0)
         delete la;
         Attachment a = new Attachment();
         a.body = attbody;
         a.name = attname;
         a.parentid = s.id;
         insert a;
         }
         return new pagereference('/'+s.id);
                                   }
}

Ex:2

Visualforce page:

<apex:page controller="clsstudent">
  <apex:form >
   
   <table >
   <tr>
    <td align="right">
     <apex:outputLabel style="font-size:18px">Student Name:</apex:outputLabel> 
    </td>
    
    <td>
     <apex:inputText value="{!sname}"/>
    </td>
   </tr>
   
   <tr>
    <td align="right">
     <apex:outputLabel style="font-size:18px">Qualification:</apex:outputLabel>
    </td>
    
    <td>
     <apex:inputText value="{!qua}"/>
    </td>
   </tr>
   
   <tr>
    <td align="right">
     <apex:outputLabel style="font-size:18px">Skills:</apex:outputLabel>
    </td>
    
    <td>
     <apex:inputTextarea value="{!sk}"/>
    </td>
   </tr>
   
   <tr>
    <td align="right">
     <apex:outputLabel style="font-size:18px">Gender:</apex:outputLabel>
    </td>
    
    <td>
     <apex:inputText value="{!gn}"/>
    </td>
   </tr>
   
   <tr>
    <td align="right">
     <apex:outputLabel style="font-size:18px">Location:</apex:outputLabel>
    </td>
    
    <td>
     <apex:inputText value="{!loc}"/>
    </td>
   </tr>
   
   <tr>
   <td align="right">
    <apex:inputFile value="{!filedata}" filename="{!filename}" />
    </td>
   </tr>
   </table>
   
    <apex:commandButton value="Save" action="{!save}"/>
   
  </apex:form>
</apex:page>

Apex Class:

public with sharing class clsstudent {

    public PageReference save() {
         Student1__c s=new Student1__c();
   
    s.Name=sname ;
    s.Qualification__c=qua ;
    s.Skill_Set__c=sk ;
    s.Gender__c=gn ;
    s.Location__c=loc ;
    insert s;
    attachment a=new attachment();
    a.Body=filedata;
    a.name=filename;
    a.parentid=s.id;
    insert a;
   
    pagereference ref=new pagereference('/'+s.id);
        return ref;
    }

    public blob filedata { get; set; }
    public String filename { get; set; }
    public String loc { get; set; }
    public String gn { get; set; }
    public String sk { get; set; }
    public String qua { get; set; }
    public String sname { get; set; }
}

No comments:

Post a Comment