0

I am trying to create a case external page that allows for me to use the standard controller with an extension so that they are able to add an attachment if they are wanting to. I had it where it wouldn't upload the attachment and now it will only upload the attachment and leaves all of the data alone. Anyone know of a way for it to do both?

VF Page:

<apex:page standardController="Case" extensions="caseattachment">
<apex:form id="frm">
    <apex:pageBlock title="New Salesforce Case">
        <apex:pageBlockSection columns="1"  title="Salesforce Case -- Red lines represent required fields." collapsible="false">
        </apex:pageBlockSection>
        <strong>Please click the lookup icon (small button to right of field) to search name selection from Salesforce lookup dialog.</strong>
        <apex:pageBlockSection columns="1">
           <apex:inputField value="{!Case.Requestor_Name__c}" required="true"/>
           <br></br>
           </apex:pageBlockSection>
        <strong>Select Problem and High Priority ONLY if the issue is preventing you from continuing work in Salesforce.</strong>
        <apex:pageBlockSection columns="1">
           <apex:inputField value="{!Case.Type}" required="true"/>
           <apex:inputField value="{!Case.Priority}" required="true"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="1" title="Case Details" collapsible="false">
           <apex:inputField value="{!Case.Department__c}" required="true"/>
           <apex:inputField value="{!Case.Salesforce_Object__c}" required="true"/>
           <apex:inputField value="{!Case.Description}" required="true" style="width:50%; height: 60px" />
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
           <apex:commandButton action="{!save}" value="Submit"/>
           <apex:commandButton action="{!cancel}" value="Cancel"/>
       </apex:pageBlockButtons>
    <apex:pageBlockSection title="Upload 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 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;
    }
}
2
  • try replacing objcase = new case(); with objcase = (Case)controller.getRecord();
    – Nick C
    Commented Sep 29, 2016 at 0:32
  • pro tip: use transient modifier on variable fileBody to avoid viewstate issues - especially if you extend this to allow for multiple uploads
    – cropredy
    Commented Oct 2, 2016 at 22:06

1 Answer 1

0

Replace objcase = new case(); with objcase = (Case)controller.getRecord();

In your code, you're creating a new instance of a Case record that is in no way connected to the case fields on the Visualforce page. To get the case record associated with the page, you need to use the case record associated with the standard controller by using the controller.getRecord() method.

You can then override the save method as you've done, which includes inserting the case with the details provided by the user on the Visualforce page.

3
  • I changed that line per your recommendation but now it is giving me the error, "Authorization Required You must first log in or register before accessing this page. If you have forgotten your password, click Forgot Password to reset it. " I am trying to let this be used as a guest user. Is there a way around this part?
    – SoleHack
    Commented Oct 3, 2016 at 4:04
  • @JoshuaAnderson In a visualforce page, that error can mean many things. I'd suggest checking whether the Site Guest User has access to the case object.
    – Nick C
    Commented Oct 3, 2016 at 5:00
  • I am not sure what I did this time but it is now working. Thanks.
    – SoleHack
    Commented Oct 3, 2016 at 5:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .