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
Shawn ReichnerShawn Reichner 

Help with Controller Extension Code

Hello,

I am building a "Site" for guest users to submit a Case for support.  I have the site built, the visualforce page, and a controller extension built to add an attchment.  When the case is submitted form the site, it creates a Case record in Salesforce, the attachment shows under the notes and attachments related list, but the subject and description fields that were filled out on the Site are not being populated on the case record.  

I am at a loss here as to why they are not working, and I am a new developer so I very well could have missed something very easy.  Please look over the following code for my VF Page, and the controller extension, and if available please help me figure out how to get the subject and description fields to populate on the case record with the information that was submitted via the site submission.  

Thank you in advance for your time and help!

VF Page

<apex:page standardcontroller="Case" extensions="caseattachment"
showHeader="false">
<img src="{!$resource.AVISPL_Logo2}"></img><b/><b/>
    <apex:form >
    <apex:pageBlock >
<apex:pageBlockSection title="Hello, Thank You For Reporting Your Incident!  A Salesforce Platform Engineer Will Be In Touch Shortly. " columns="1" showHeader="True" collapsible="False">
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageMessages />

        <apex:pageBlock >
        <apex:inputField value="{!Case.CaseNumber}"/>
            <apex:pageBlockSection title="Subject">
                <apex:inputText value="{!Case.subject}" />
            </apex:pageBlockSection>

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

            <apex:pageBlockSection title="Tell Us About The Incident">
                <apex:inputTextarea value="{!Case.Description}" rows="8" cols="80" />
            </apex:pageBlockSection>

            <apex:commandButton value="Submit Incident" action="{!Save}" />
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller extension

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 = this.fileName; 
              myAttachment.ParentId = objcase.id;             
              insert myAttachment;                 
        pagereference pr = new pagereference('/'+objcase.id);                           
        return pr;
    }
}
Best Answer chosen by Shawn Reichner
MTBRiderMTBRider
Try this :
...

if (fileName != null && fileName.trim().length() > 0  && fileBlob != null) {
   myAttachment  = new Attachment();
   Integer i=0;
   myAttachment .clear();
   myAttachment.Body = fileBody;
   myAttachment.Name = this.fileName; 
   myAttachment.ParentId = objcase.id;             
   insert myAttachment; 
}
....



 

All Answers

MTBRiderMTBRider
It is because your VF page is using inputText for Subject and inputTextarea for the description.  inputText... are not bound to SF fields.  You either need to use inputFields for subject and description or keep the inputText fields but then take the values from them in your controller once the page is submitted and insert those values into the case subject and description fields.
Shawn ReichnerShawn Reichner
MTBRider,

Thank you very much for your support steps.  Unfortunately changing to inputField did not resolve the issue, and subject and description are both still blank.  I then tested another scerio where I do not include an attachment but have the subject and description filled out, and without an attachment no case is created at all.  This tells me that I am possibly missing something in the controller extension class I wrote.  If I remove the attachment class, then teh subject and description fill out on the case as they should.  

Knowign those additional details, are you able to provide any further support methods to try and resolve my problem?

Thank you again for your time, it is very much appreciated,

Shawn
Shawn ReichnerShawn Reichner
MTBRider,

How would I perform the second option you kindly suggested? (Inserting the values from the VF page and use them in my controller for placment on the case record)

Thank you again,

Shawn
MTBRiderMTBRider
You would create a variables in your controller for subject and description along with getter and setters:
 
public with sharing class myController {
	...
	public String subj {get; set;}
	public String desc {get; set;}
...

Then in your save() method before inserting...
 
...
objcase.Subject = subj;
objcase.Descritpion = desc;
....

By the way, you should use the save method to do other things, like making sure required values are filled out, etc...  Also, there is a size limit to the size of attachment that can be uploaded, you can and should evaluate that too before attempting to insert it :)

You've a good start...Good luck!

 
Shawn ReichnerShawn Reichner
The subject and description fields woudl be the only required fields we woudl want to capture as we are trying to make this as easy as possible for our users to submit a case.  I woudl not want a size limit for the attachments as they usually will be screen grabs.  But in case I woudl repeat the steps you listed above to do that?

I am going to try your method as you listed baove and I will let you know if I am successful or not... 

Thank you again, you are a life saver

Shawn
Shawn ReichnerShawn Reichner

MTBRider,

Unfortunately the solution is providing the same results.   No subject of description is being populated on the case record.  (The only difference in your code and what I had to use was it woudl not accept desc....but it woudl accept description for the public string portion.  Any ideas??  


Thank you so much for your help,
 

Shawn

Shawn ReichnerShawn Reichner
MTBRider,

Ok, after some edits and cuts, I have the subject and description fields pulling over.  I am still having one small issue where if there is no attachment chosen on the site by the guest user a case is not created in the case object.   Any ideas on that one?  I am pasting my updated code that works to bring over subject and description now...but again if an attachment is chosen a case is made, if no attahcment is chosen, the case is not made.  (Each case could have an attachment or not, it shoudl not be required)

VF Page

<apex:page standardcontroller="Case" extensions="caseattachment"
showHeader="false">
<img src="{!$resource.AVISPL_Logo2}"></img><b/><b/>
    <apex:form >
    <apex:pageBlock >
<apex:pageBlockSection title="Hello, Thank You For Reporting Your Incident!  A Salesforce Platform Engineer Will Be In Touch Shortly. " columns="1" showHeader="True" collapsible="False">
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageMessages />

        <apex:pageBlock >
        <apex:inputField value="{!Case.CaseNumber}"/>
            <apex:pageBlockSection title="Subject">
                <apex:inputField value="{!objcase.Subject}" />
            </apex:pageBlockSection>

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

            <apex:pageBlockSection title="Tell Us About The Incident">
                <apex:inputField value="{!objcase.Description}"/>
            </apex:pageBlockSection>

            <apex:commandButton value="Submit Incident" action="{!Save}" />
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller Extension

public with sharing class caseattachment
{
public case objcase{get;set;}
public String subj {get;set;}
public String description {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 = this.fileName; 
              myAttachment.ParentId = objcase.id;             
              insert myAttachment;                 
        pagereference pr = new pagereference('/'+objcase.id);                           
        return pr;
    }
}
 
MTBRiderMTBRider
Have you checked the debug logs?   I suspect that you are getting an exception when they try to submit a case with no attachment.   This is becuase you are not doing any validation in your save method.  So when there is no attachment you are trying to insert a blank attachment into the DB, which probably throws an exception.  As I mentioned before, you should be checking to make sure that they are submitting valid entries.   The simplest way to do this would be:
 
insert objcase;
if (this.fileName != null) {
   //Now it is safe to insert the attachment
   myAttachment = new Attachment();
   ...
}
....

Also, on the file size thing,  the size restriction is not something you get to decide, it is imposed by Salesforce.  So if someone tries to insert a file that is too big (greater than 5Mb I think), an exception will be thrown.
Shawn ReichnerShawn Reichner
Yeah I lokke dup the file size limitation, thank you for that and your other support methods.  

I have implemented the additions you suggested above, and I am still getting the same results.....if no attachment is chosen, a case is not created.  I viewed the VF Page as Administrator and I got the following error: [File Name, Body]    How would I run a debug log for a guest site user?
Shawn ReichnerShawn Reichner
MTBRider,

I found the way to run the debug log for the guest site user, to which upon trying to create a case with no attachment I am gettign the following error from the debug log....Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [File Name, Body]: [File Name, Body

Just exactly as you thought.....how can we tweak your ealier suggestion of the if statement to work properly?


Thank you again for your time, and help, you have gotten me much further and after this last hurdle you will definately be my hero! 

Shawn
MTBRiderMTBRider
Try this :
...

if (fileName != null && fileName.trim().length() > 0  && fileBlob != null) {
   myAttachment  = new Attachment();
   Integer i=0;
   myAttachment .clear();
   myAttachment.Body = fileBody;
   myAttachment.Name = this.fileName; 
   myAttachment.ParentId = objcase.id;             
   insert myAttachment; 
}
....



 
This was selected as the best answer
Shawn ReichnerShawn Reichner
You are the BEST!!!!!   I am working in both scenarios now with an attachment or none!!!!

Thank you again!  

Shawn