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
Jack Rose 1Jack Rose 1 

create a case from an uploaded file

Hi, 

Is anyone aware if it is possible to use a visualforce page, a a web form, to create a case and upload a file to the case at the same time?
Best Answer chosen by Jack Rose 1
Harshit Garg 6Harshit Garg 6
Hi Jack,

Please use updated code below.

Class:
public class DLUploadDocumentController {
public case objcase{get;set;}

public Attachment myAttachment{get;set;}

public string fileName{get;set;} 

public Blob fileBody{get;set;}

 

    public  DLUploadDocumentController(Apexpages.standardcontroller controller)

    {

        objcase = new case();

        myAttachment =new Attachment();

      
    
    }

    public pagereference save()

    {

        //Fetching the assignment rules on case
            AssignmentRule AR = new AssignmentRule();
            AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
            
            //Creating the DMLOptions for "Assign using active assignment rules" checkbox
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;


            
            objcase.RecordTypeid= '012280000015LZZ';
            objcase.Origin='Web ';
            objcase.Customer_Brand__c='Direct Line';
            objcase.setOptions(dmlOpts);
            
             insert objcase;
   
       System.debug('@@@@@objcase' +objcase);
        System.debug('@@@@@fileBody'+fileBody);     

        myAttachment  = new Attachment();

              Integer i=0;

              myAttachment.clear();

              myAttachment.Body = fileBody; 

              myAttachment.Name = 'Logo_'+objcase.id+'.jpeg' ; 

              myAttachment.ParentId = objcase.id;             

              insert myAttachment;                 

        pagereference pr = new pagereference('/'+objcase.id);                           

        return pr;

    }

}

Pages:
 
<apex:page showHeader="false" sidebar="false" standardController="case" extensions="DLUploadDocumentController">
    <apex:form id="frm">
        <apex:pageBlock title="Case Edit">
            
        <apex:pageBlockButtons >

            <apex:commandButton value="Submit" action="{!save}"/>

        </apex:pageBlockButtons>       

        <apex:pageBlockSection title="Uploading the Attachment" collapsible="false" dir="LTR" columns="1">

        <div id="upload" class="upload">                                   

            <apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/>                            

        </div> 

         </apex:pageBlockSection>           

       </apex:pageBlock>        

    </apex:form>

</apex:page>

Thanks,
Harshit Garg
 

All Answers

Harshit Garg 6Harshit Garg 6
HI Jack,

As i understood about your requirment
i am sending vf page and class i feel this suits your requirement.

VF Page:​

<apex:page standardController="case" extensions="caseattachment">
    <apex:form id="frm">
        <apex:pageBlock title="Case Edit">
            <apex:pageBlockSection title="Case Information" collapsible="false">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Case Owner"/>     
                    <apex:outputlabel value="{!$User.FirstName} {!$User.LastName}"/>
                </apex:pageBlockSectionItem>
                    <apex:inputField value="{!objcase.Status}"/>
                    <apex:inputField value="{!objcase.Type}"/>
                    <apex:inputField value="{!objcase.Origin}"/>               
            </apex:pageBlockSection>      
        <apex:pageBlockButtons>
            <apex:commandButton value="Save" action="{!save}"/>
        </apex:pageBlockButtons>       
        <apex:pageBlockSection title="Uploading the Attachment" collapsible="false" dir="LTR" columns="1">
        <div id="upload" class="upload">                                   
            <apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/>                            
        </div> 
         </apex:pageBlockSection>           
       </apex:pageBlock>        
    </apex:form>
</apex:page>



Apex Class:
 
public class caseattachment
{
public case objcase{get;set;}
public Attachment myAttachment{get;set;}
public string fileName{get;set;} 
public Blob fileBody{get;set;}

    public caseattachment(Apexpages.standardcontroller controller)
    {
        objcase = new case();
        myAttachment =new Attachment();
    }
    public pagereference save()
    {
        insert objcase;
        System.debug('@@@@@fileBody'+fileBody);     
        myAttachment  = new Attachment();
              Integer i=0;
              myAttachment .clear();
              myAttachment.Body = fileBody; 
              myAttachment.Name = 'Logo_'+objcase.id+'.jpeg' ; 
              myAttachment.ParentId = objcase.id;             
              insert myAttachment;                 
        pagereference pr = new pagereference('/'+objcase.id);                           
        return pr;
    }
}



if you get any doubts in this please feel free to ask me

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Harshit Garg
Jack Rose 1Jack Rose 1
Hi Harshit, 

First of all thank you for the help. I have attempted to use the above code with a couple of changes; 
I have removed the Status, Type & Origin fields from the VF Page. 
I have added into the controller the setting of the record type and origin and a page redirect. 

The code is creating a case but the attachment doesn't appear on the case. 

Any thoughts?
Harshit Garg 6Harshit Garg 6
Hi Jack,

Please edit your layout and drag the attachment and drop in the related list. 
Could you send me your page layout screenshot? But from the above solution, you can do that properly.


Thanks,
Harshit Garg
Jack Rose 1Jack Rose 1
Hi Harshit, 

I have the attachment and files both displayed as related lists but they are still not showing. Here is the Code I have entered based on what you have suggested. 

VF - 
<apex:page showHeader="false" sidebar="false" standardController="case" extensions="DLUploadDocumentController">
    <apex:form id="frm">
        <apex:pageBlock title="Case Edit">
            
        <apex:pageBlockButtons>

            <apex:commandButton value="Submit" action="{!save}"/>

        </apex:pageBlockButtons>       

        <apex:pageBlockSection title="Uploading the Attachment" collapsible="false" dir="LTR" columns="1">

        <div id="upload" class="upload">                                   

            <apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/>                            

        </div> 

         </apex:pageBlockSection>           

       </apex:pageBlock>        

    </apex:form>

</apex:page>

Controller - 
 
public class DLUploadDocumentController {
public case objcase{get;set;}

public Attachment myAttachment{get;set;}

public string fileName{get;set;} 

public Blob fileBody{get;set;}

 

    public DLUploadDocumentController(Apexpages.standardcontroller controller)

    {

        objcase = new case();

        myAttachment =new Attachment();

    }

    public pagereference save()

    {

        //Fetching the assignment rules on case
            AssignmentRule AR = new AssignmentRule();
            AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
            
            //Creating the DMLOptions for "Assign using active assignment rules" checkbox
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;


            
            objcase.RecordTypeid= '012260000000hl7';
            objcase.Origin='Web ';
            objcase.Customer_Brand__c='Direct Line';
            objcase.setOptions(dmlOpts);
            insert objcase;
            
            PageReference acctPage = Page.WebFormSubmitted;
       		 acctPage.setRedirect(true);
       		 return acctPage;
        
        insert objcase;

        System.debug('@@@@@fileBody'+fileBody);     

        myAttachment  = new Attachment();

              Integer i=0;

              myAttachment .clear();

              myAttachment.Body = fileBody; 

              myAttachment.Name = 'Logo_'+objcase.id+'.jpeg' ; 

              myAttachment.ParentId = objcase.id;             

              insert myAttachment;                 

        pagereference pr = new pagereference('/'+objcase.id);                           

        return pr;

    }

}

 
Harshit Garg 6Harshit Garg 6
let me check..Give me moment
Harshit Garg 6Harshit Garg 6
Hi Jack,

Please use updated code below.

Class:
public class DLUploadDocumentController {
public case objcase{get;set;}

public Attachment myAttachment{get;set;}

public string fileName{get;set;} 

public Blob fileBody{get;set;}

 

    public  DLUploadDocumentController(Apexpages.standardcontroller controller)

    {

        objcase = new case();

        myAttachment =new Attachment();

      
    
    }

    public pagereference save()

    {

        //Fetching the assignment rules on case
            AssignmentRule AR = new AssignmentRule();
            AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
            
            //Creating the DMLOptions for "Assign using active assignment rules" checkbox
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;


            
            objcase.RecordTypeid= '012280000015LZZ';
            objcase.Origin='Web ';
            objcase.Customer_Brand__c='Direct Line';
            objcase.setOptions(dmlOpts);
            
             insert objcase;
   
       System.debug('@@@@@objcase' +objcase);
        System.debug('@@@@@fileBody'+fileBody);     

        myAttachment  = new Attachment();

              Integer i=0;

              myAttachment.clear();

              myAttachment.Body = fileBody; 

              myAttachment.Name = 'Logo_'+objcase.id+'.jpeg' ; 

              myAttachment.ParentId = objcase.id;             

              insert myAttachment;                 

        pagereference pr = new pagereference('/'+objcase.id);                           

        return pr;

    }

}

Pages:
 
<apex:page showHeader="false" sidebar="false" standardController="case" extensions="DLUploadDocumentController">
    <apex:form id="frm">
        <apex:pageBlock title="Case Edit">
            
        <apex:pageBlockButtons >

            <apex:commandButton value="Submit" action="{!save}"/>

        </apex:pageBlockButtons>       

        <apex:pageBlockSection title="Uploading the Attachment" collapsible="false" dir="LTR" columns="1">

        <div id="upload" class="upload">                                   

            <apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/>                            

        </div> 

         </apex:pageBlockSection>           

       </apex:pageBlock>        

    </apex:form>

</apex:page>

Thanks,
Harshit Garg
 
This was selected as the best answer
Harshit Garg 6Harshit Garg 6
Please give your recordtype id...In Testing time i have given my recordtype id.
Jack Rose 1Jack Rose 1
It is now working. Thank you for your help, it's much appreciated. One last Question, What would the change need to be to the controller be to retain the original name of the file? I have located the line in the controller that generates the name
 
Jack Rose 1Jack Rose 1
Harshit, 
I have added the visualforce page to a site and I'm now getting an Authorisation Required when submitting the form. Any Ideas?
Harshit Garg 6Harshit Garg 6
HI,

If I am not wrong you have a profile guest user. Please give the access of your VF page and class for that guest user profile.

Thanks,
Harshit Garg
Jack Rose 1Jack Rose 1
Hi, 
Please can you let me know how I would do that? I have created to other VF pages on the site and they seem ok. It only seems to cause an issue when i click save. 
Harshit Garg 6Harshit Garg 6
public class DLUploadDocumentController {
public case objcase{get;set;}

public Attachment myAttachment{get;set;}

public string fileName{get;set;} 

public Blob fileBody{get;set;}

 

    public  DLUploadDocumentController(Apexpages.standardcontroller controller)

    {

        objcase = new case();

        myAttachment =new Attachment();

      
    
    }

    public pagereference save()

    {

        //Fetching the assignment rules on case
            AssignmentRule AR = new AssignmentRule();
            AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
            
            //Creating the DMLOptions for "Assign using active assignment rules" checkbox
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;


            
            objcase.RecordTypeid= '012280000015LZZ';
            objcase.Origin='Web ';
            objcase.Customer_Brand__c='Direct Line';
            objcase.setOptions(dmlOpts);
            
             insert objcase;
   
       PageReference acctPage = Page.WebFormSubmitted;
             

      
       System.debug('@@@@@objcase' +objcase);
        System.debug('@@@@@fileBody'+fileBody);     

        myAttachment  = new Attachment();

              Integer i=0;

              myAttachment.clear();

              myAttachment.Body = fileBody; 

              myAttachment.Name = 'Logo_'+objcase.id+'.jpeg' ; 

              myAttachment.ParentId = objcase.id;             

              insert myAttachment;                 

        pagereference pr = new pagereference('/'+objcase.id);                           

        return null;

    }

}

Please use that above class again..it will redirect on same page same as you want.

Thanks,
Harsh
Jack Rose 1Jack Rose 1
Thank You it now appears to be working as expected on the site. I really appreciate all your help. 
1 final question, are you able to help with the file naming that i mentioned earlier?
Harshit Garg 6Harshit Garg 6
HI Jack,

I am unable to understand what you are saying.. Could you explain again in detail?

Thanks,
Harshit Garg