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
Tester6789Tester6789 

Add attachment to flow using VF page

Hi All,

I have to add an attachment(resume) in my flow in one of the screens.The flow is on contact object.I have embedded the flow in a visual force page and and created its controller too,but Im confused how to call the VF page in my flow to add the attachment.

Here is the page:

<apex:page controller="InlineFlowAttachment" sidebar="false">

    
    <flow:interview name="Upload_Resume" interview="{!myFlow}"></flow:interview>
    
    <apex:outputPanel rendered="{!if(VarAttachmentParentID !='',true,false)}">
    <apex:pageMessages />
    <apex:form >
    <apex:pageBlock title="Upload an Attachment">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!upload}" value="Save"/>
        </apex:pageBlockButtons>
    
    <apex:pageBlockSection showHeader="false" columns="2" id="block1">
    
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="File Name" for="FileName"/>
            <apex:inputText value="{!attachment.name}" id="FileName"/>
        </apex:pageBlockSectionItem>
    
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="File" for="File"/>
            <apex:inputfile value="{!attachment.body}" filename="{!attachment.name}" id="File"/>
        </apex:pageBlockSectionItem>
        
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Description" for="Description"/>
            <apex:inputTextArea value="{!attachment.Description}" id="Description"/>
        </apex:pageBlockSectionItem>
    
    </apex:pageBlockSection>
    </apex:pageBlock>
     </apex:form>   
    </apex:outputPanel>
  </apex:page>
                                      

Here is the controller:

public with sharing class InlineFlowAttachment{

    public InlineFlowAttachment(ApexPages.StandardController controller) {

    }


    public Flow.Interview.Upload_Resume myflow{get;set;}
    
    public InlineFlowAttachment(){
    
        myflow=new Flow.Interview.Upload_Resume(new map<string,object>());
    
    }
    
    public string VarAttachmentParentID;
    
    public string getVarAttachmentParentID(){
      
        if(VarAttachmentParentID==null){
        VarAttachmentParentID='';
        return VarAttachmentParentID;
        }
        return myflow.VarAttachmentParentID;    
     }

    public Attachment attachment{
        get{
            if(attachment==null)
              attachment=new attachment();
              return attachment;  
        }
        set; 
    }

    public PageReference upload(){
    
        attachment.Ownerid=UserInfo.getUserID();
        Attachment.isPrivate=true;
        Attachment.parentid=getVarAttachmentParentID();
        
        try{
            insert attachment;
        }
    
        catch(DMLexception e){
            ApexPages.AddMessage(new ApexPages.Message(APexPages.Severity.Error,'Error'));
            return null;
            }
            
         finally{
             attachment=new attachment();
         }
         
         ApexPages.AddMessage(new ApexPages.Message(APexPages.Severity.INFO,'Success'));
         VarAttachmentParentID='';   
         return null;
    }
}

Any help will be appreciated.Thanks.
Tester6789Tester6789
I updated the flow and made my apex class into an extension since parentid was of contact.I did a record lookup to get the contact id,and the page rendered correctly showing the attachment file name and description fields,but when I click save,i gives the error:

User-added image Here is my udated class and VF page:

<apex:page standardcontroller="contact" extensions="InlineFlowAttachment" sidebar="false">
 
    <flow:interview name="Upload_Resume" interview="{!myFlow}"><apex:param name="VarAttachmentParentID" value="{!Contact.Id}"/></flow:interview>
        <apex:form >
        <apex:outputpanel rendered="{!if(varAttachmentParentId != '', true,false)}" styleClass="shrink">
            <div class="shrink">
                <apex:pageMessages />
                
                <apex:pageBlock title="Upload an Attachment">
                
                    <apex:pageBlockButtons >
                        <apex:commandButton action="{!upload}" value="Save"/>
                    </apex:pageBlockButtons>
                    
                    <apex:pageBlockSection showHeader="false" columns="2" id="block1">
                    
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="File Name" for="fileName"/>
                            <apex:inputText value="{!name}" id="fileName"/>
                        </apex:pageBlockSectionItem>
                        
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="File" for="file"/>
                            <apex:inputFile value="{!a.body}" filename="{!name}" id="file"/>
                        </apex:pageBlockSectionItem>
                        
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Description" for="description"/>
                            <apex:inputTextarea value="{!description}" id="description"/>
                        </apex:pageBlockSectionItem>
                        
                       <!-- <apex:pageBlockSectionItem >
                            <apex:outputLabel value="File Name" for="fileName"/>
                            <apex:inputText value="{!attachment.name}" id="fileName"/>
                        </apex:pageBlockSectionItem>
                        
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="File" for="file"/>
                            <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
                        </apex:pageBlockSectionItem>
                        
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Description" for="description"/>
                            <apex:inputTextarea value="{!attachment.description}" id="description"/>
                        </apex:pageBlockSectionItem> 
                        -->
                        
                    </apex:pageBlockSection>
                    
                </apex:pageBlock>
            </div>
        </apex:outputPanel>
    </apex:form>
  </apex:page>

****************************************************************

public with sharing class InlineFlowAttachment{
    
     public Flow.Interview.Upload_Resume myflow{get;set;}
     public string VarAttachmentParentID;
    
     public Attachment a {get; set;} 
     public Blob body {get; set;}
     public String name {get; set;}
     public String description {get; set;}
     
    public InlineFlowAttachment(ApexPages.StandardController controller) {
        myflow=new Flow.Interview.Upload_Resume(new map<string,object>());
        a = new Attachment();
    }
  
    
    public string getVarAttachmentParentID(){
      
        if(VarAttachmentParentID==null){
        VarAttachmentParentID='';
        return VarAttachmentParentID;
        }
        return myflow.VarAttachmentParentID;    
     }

    public Attachment attachment{
        get{
            if(attachment==null)
              { system.debug('initializing attachment'+attachment);
              attachment=new attachment();}
              return attachment;  
        }
        set; 
    }

    public PageReference upload(){
    
        //Attachment a = new Attachment();
        a.Ownerid=UserInfo.getUserID();
        //Attachment.isPrivate=true;
        a.parentid=getVarAttachmentParentID();
        a.body = body;
        a.name = name;
        a.description = description;
        
        try{
        system.debug('these are the attachment values:'+a);
            insert a;
        }
    
        catch(DMLexception e){
            ApexPages.AddMessage(new ApexPages.Message(APexPages.Severity.Error,'Error'));
            return null;
            }
            
         finally{
             attachment=new attachment();
         }
         
         ApexPages.AddMessage(new ApexPages.Message(APexPages.Severity.INFO,'Success'));
         VarAttachmentParentID='';   
         return null;
         
    
    }
}