You need to sign in to do that
Don't have an account?
Shawn 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;
}
}
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;
}
}
All Answers
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
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
Then in your save() method before inserting...
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!
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
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
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;
}
}
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.
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?
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
Thank you again!
Shawn