0

I am building a wizard with visual flow and one part of it has to be screen for uploading file. I found that there is no way to put file picker in flow, so indeed it has to be done in vf page. What is good solution to do? Is there a way to embed vf page in flow or create two flows, embed them in vf page for uploading file and make file uploading step a splitter between flows?

  • 1
    In the past I have embedded the flow in a VF page, had the flow set a property that tells the VF page when to render the required section. – Girbot Feb 13 '17 at 13:53
  • 1
    Like Girbot I have done this with a parameter in the flow that is set when I want to go to the visualforce to load the file, then it resumes the flow at the next step, – Dave Humm Feb 14 '17 at 7:53
1

Flow changes

Add a text variable with either input/output or Output only settings, e.g. varUpload with a default value of 0, then in the flow when you want to trigger the switch back to VF for the upload use an assignment to set it to 1.You will also need an input/output or output only variable to pass the id of the object you wish to attach the file to, e.g. varCaseId.

Custom controller

Add the following to the controller

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

    // Specific flow name for flow type
    public Flow.Interview.my_flow myflow { get; set; }

    public String varCaseId;
    public String varUpload;
    public Boolean upload;
    public Boolean uploaded = false;
    public String fName{ get; set;}
    public String displayName{get; set;}


    // The Case Id value from the flow
    public String getvarCaseId()
    {
        If(myFlow == null)
            Return null;
        Else
            return myflow.varCaseId;
    }

    // The varUpload variable value from the flow
    public String getvarUpload()
    {
        If(myFlow == null)
                    Return String.valueOf('0');
            Else
                    Return myflow.varUpload;
    }


    Public Boolean getupload()
    {
            If(myFlow != null && myflow.varUpload != null && myflow.varUpload == '1' && uploaded!= null && uploaded == false)
                    Return true;
            else
                    Return false;
    }

    // Save method, leading to next step in flow
    public PageReference Save()
    {               
            return upload(System.Boolean.valueOf(true));
    }

    // Save More method, leading to the attachment upload functionality again
    public PageReference SaveMore()
    {               
            return upload(System.Boolean.valueOf(false));           
    }

    // Cancel method, leading to next step in flow
    public PageReference Cancel()
    {       
            This.uploaded =  true;
            attachment = null;
            return null;
    }

    // Upload method for actual attachment upload, setting the upload variable appropriately to allow/ not allow another attachment upload
    public PageReference upload(Boolean varUploaded) 
    {
        attachment.OwnerId = UserInfo.getUserId();
        attachment.ParentId = getvarCaseId(); // the record the file is attached to
        attachment.IsPrivate = false;

        if( (!(attachment.name != null && attachment.name != '')) && (fName != null && fName != ''))
        {
            String afterExt = fName.substringAfter('.');
            System.debug('afterExt :: ' + afterExt );
            if(displayName != null && displayName != '')
            {
                attachment.name = displayName + '.' + afterExt;
            }
            else
                attachment.name = fName;
        }
        try 
        {
            if(!(attachment.body != null))
            {
                    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please specify a file to be uploaded'));
            }
            else if (!(attachment.name != null && attachment.name != ''))
            {
                    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'Please specify a file name'));
            }

            insert attachment;         
            displayName = '';
            fName = '';             

        } 
        catch (DMLException e) 
        {
          ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
          return null;
        } 
        finally 
        {
          attachment = new Attachment(); 
        }

        This.uploaded =  varUploaded;
        // true for save and false for Save more
        return null;

    // End of upload
    }   

Visualforce page


      <apex:outputPanel id="theAttachmentPanel" rendered="{!upload}">
      <apex:sectionHeader title="Attachment Upload (Upto 10 MB)"/>

       <apex:pageMessages />
      <apex:pageBlock title="Upload an Attachment">

       <apex:pageBlockButtons >
            <apex:commandButton action="{!SaveMore}" value="Upload More"/>
            <apex:commandButton action="{!Save}" value="Finish Uploading"/>
            <apex:commandButton action="{!Cancel}" value="Submit with no Attachment"/>
       </apex:pageBlockButtons>

       <apex:pageBlockSection showHeader="false" columns="2" id="block1">

             <apex:pageBlockSectionItem >
             <apex:outputLabel value="File Name" for=" filesName "/>
             <apex:inputText value="{!displayName}" id="filesName"/>                                              
             </apex:pageBlockSectionItem>

             <apex:pageBlockSectionItem >
             <apex:outputLabel value="File" for="file"/>
             <apex:inputFile value="{!attachment.body}" filename="{!fName}" 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>

  • Thanks, this is helpful, but I am more curious how to combine vf and flow. I am currently trying to use wait element in flow to assign variable from apex, but this does not work. Do you maybe have some suggestions? – Vladyslav K Feb 14 '17 at 10:27
  • You don't have to use the wait element, you just need to have the VF wrapper and controller and pass a variable value from the flow which starts the VF sections that you require - it's where I talk about the varUpload in this case which switches out to the VF from the flow, then returns after completion. – Dave Humm Feb 14 '17 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.