I have below requirement to Create a VF page – ‘Registration Forum’ having
1.Name field
2.Age field
3.Project Unit field
4.Gender as Radio Button with values – M and F
5.Certification as Picklist with Values – PD1, ADM 201, PD2, App Builder, Sales Cloud, Service Cloud
6.2 buttons – save and reset
7.Attachment area – where we can browse and add any document there.
Save Button – A record should get created in one object (Any object u can mention)
Reset Button – Page should not get refreshed, just values get refreshed with blank value.
As I am new to SFDC, could you please help me to get it done?
Thanks
It's hard to tell from your question exactly what you're looking for, but here is a visualforce page that saves to a custom object called Form__c.
To do the save and reset you'll probably need an Apex extension. I'm not sure if your browse documents is for Salesforce documents or local files.
<apex:page standardController="Form__c" >
<apex:form>
<apex:pageBlock>
<apex:pageBlockButtons>
<apex:commandButton value="Save" action="{!save}" />
</apex:pageBlockButtons>
<apex:pageBlockSection>
<apex:inputField value="{!Form__c.Name}" />
<apex:inputField value="{!Form__c.Age__c}" />
<apex:inputField value="{!Form__c.Project_Unit__c}" />
<apex:selectRadio value="{!Form__c.Gender__c}" ><apex:selectOption itemValue="Male" itemLabel="Male" /><apex:selectOption itemValue="Female" itemLabel="Female" /></apex:selectRadio>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Hi here is the sample code for your asking for form submission.i have create custom object Registration_Forum__c.
Click to see Custom Object Registration_Forum__c Image
<apex:page Controller="VFFileUpload">
<apex:pageMessages id="showmsg"></apex:pageMessages>
<apex:form>
<apex:pageBlock title="Upload Attachment">
<apex:pageBlockButtons location="top">
<apex:commandButton value="Save" action="{!saveForm}" />
<apex:commandButton value="reset" action="{!resetForm}" />
</apex:pageBlockButtons>
<apex:pageBlockSection>
<apex:inputField value="{!Registration_Forum.Name}" />
<apex:inputField value="{!Registration_Forum.age__c}" />
<apex:inputField value="{!Registration_Forum.Certification__c}" />
<apex:inputField value="{!Registration_Forum.Project_Unit__c}" />
<apex:selectRadio value="{!Registration_Forum.Gender__c}">
<apex:selectOption itemValue="Male" itemLabel="Male" />
<apex:selectOption itemValue="Female" itemLabel="Female" />
</apex:selectRadio>
<apex:inputFile id="file" value="{!fileBody}" filename="{!fileName}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
public class VFFileUpload
{
public Registration_Forum__c Registration_Forum{get;set;}
public String fileName {get;set;}
public Blob fileBody {get;set;}
public VFFileUpload() {
Registration_Forum=new Registration_Forum__c();
}
public void saveForm(){
upsert Registration_Forum;
if(fileBody != null && fileName != null && Registration_Forum.id!=null)
{
Attachment myAttachment = new Attachment();
myAttachment.Body = fileBody;
myAttachment.Name = fileName;
myAttachment.ParentId = Registration_Forum.Id;
upsert myAttachment;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info,'File Upload Success'));
}
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info,'Form Submission Success'));
}
public void resetForm(){
Registration_Forum=new Registration_Forum__c();
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info,'Reset'));
}
}
Related
While creating a form using salesforce visual force pages and saving the data, it results in the following error:
Attempt to de-reference a null object
Error is in expression '{!save}' in component apex:commandButton in page contract_object: Class.ContractController.save: line 7, column 1
An unexpected error has occurred. Your development organization has been notified.
My apex code is as follows:
<apex:page Controller="ContractController" sidebar="false" showHeader="false">
<apex:form >
<apex:pageBlock title="Contract Form">
<apex:pageBlockSection columns="1">
<apex:inputField value="{!conObj.Name}"/>
<apex:inputField value="{!conObj.First_Name__c}" />
<apex:inputField value="{!conObj.Last_Name__c}" />
<apex:inputField value="{!conObj.Phone_Number__c}" />
<apex:inputField value="{!conObj.Address__c}" />
<apex:inputField value="{!conObj.Email__c}" />
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
The controller is as follows:
public with sharing class ContractController {
public Contract__c conObj {get; set;}
public void newContract() {
conObj = new Contract__c();
}
public void save() {
insert conObj;
}
}
Your newContract() is not called anywhere.
You can make it run as constructor (special function with name same as class name)
public ContractController () {
conObj = new Contract__c();
}
If you haven't used constructors before you'll have hard time.
Or a slightly "pro" way would be to read up about "Standard Controller" and extension classes (that's a change mostly in apex side of stuff but also in 1st line of your VF)
I have create a new VF Page to create new record. It links to custom "New" button in related list. However, I wanted to pre-populate the Lookup field value from Parent object but not able to do so. Please help.
VF Page:
<apex:page standardController="SVMXC__Service__c" extensions="availableServiceExtCntrl" sidebar="false" recordSetVar="Service">
<apex:form >
<apex:pageBlock title="Service Details">
<apex:pageBlockbuttons >
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockbuttons>
<apex:pageBlockSection title="Available Service Details" columns="2" collapsible="false">
<apex:inputField value="{!SVMXC__Service__c.Name}"/>
<apex:inputField value="{!SVMXC__Service__c.SVMXC__Active__c}"/> //this needs to be true by default
<apex:inputField value="{!SVMXC__Service__c.Medical_Product__c}"/>// i want pre-populate this field.
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class availableServiceExtCntrl {
private final SVMXC__Service__c avlbleService;
public availableServiceExtCntrl(ApexPages.StandardController stdController){
}
public availableServiceExtCntrl(ApexPages.StandardSetController stdController) {
this.avlbleService = (SVMXC__Service__c)stdController.getRecord();
}
//Public Id Id = ApexPages.currentPage().getParameters().get('id');
public PageReference getProduct() {
SVMXC__Service__c service = new SVMXC__Service__c();
service = this.avlbleService;
service.Medical_Product__c = ApexPages.currentPage().getParameters().get('Medical_Product__c');
service.SVMXC__Active__c = true;
return null;
}
}
SF Admin trying to make steps into Dev. Trying to create some web-to-opportunity functionality using combination of Force.com Sites, Apex & VF. It works and the opportunities are created. When the Opp is saved on the VF page, it redirects to another VF page saying Congrats! new Opp created etc. I would like to add an link or button on this redirect page to give the user an option to navigate to the newly created Opp if they want however I'm not having much luck.
Controller:
public class OpportunityInformationPage{
public opportunity oppString{get;set;}
public OpportunityInformationPage(){
oppString = new opportunity();
}
public PageReference Saveto(){
opportunity opp = new opportunity();
opp.name = oppString.name;
opp.closedate = oppString.closedate;
opp.stagename = oppString.stagename;
opp.amount = oppString.amount;
opp.impact_level__c = oppString.impact_level__c;
insert opp;
PageReference reRend = new PageReference('/apex/Opportunity_Created');
reRend.setRedirect(true);
return reRend;
}
}
VF Page to input Opp details:
<apex:page controller="OpportunityInformationPage" showHeader="false" sidebar="false">
<apex:sectionHeader title="New Opportunity"/>
<apex:form >
<apex:pageBlock title="Opportunity Edit" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!Saveto}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Opportunity Information">
<apex:inputField value="{!oppString.name}"/>
<apex:inputField value="{!oppString.closeDate}"/>
<apex:inputField value="{!oppString.stageName}"/>
<apex:inputField value="{!oppString.amount}"/>
<apex:inputField value="{!oppString.Impact_Level__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Congrats Page where I would like the link/button to route to SF:
<apex:page controller="OpportunityInformationPage" showheader="false" sidebar="false">
<apex:PageBlock >
<apex:pageBlockSection >
<apex:outputText value="Congratulations! New Opportunity Succesfully Created" /> <br/>
</apex:pageBlockSection>
</apex:PageBlock>
</apex:page>
Any advise, help appreciated.
I'm not sure why you are redirecting to a new page after creating the opportunity. Why not just show the success message on your current page and add a link to the new
public class OpportunityInformationPage{
public opportunity oppString{get;set;}
public Boolean bOppCreated{get;set;}
public OpportunityInformationPage(){
oppString = new opportunity();
bOppCreated = false;
}
public PageReference Saveto(){
opportunity opp = new opportunity();
opp.name = oppString.name;
opp.closedate = oppString.closedate;
opp.stagename = oppString.stagename;
opp.amount = oppString.amount;
opp.impact_level__c = oppString.impact_level__c;
insert opp;
bOppCreated = true;
return null;
}
}
<apex:page controller="OpportunityInformationPage" showHeader="false" sidebar="false">
<apex:sectionHeader title="New Opportunity"/>
<apex:form >
<apex:pageBlock title="Opportunity Edit" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!Saveto}" rerender="mainBlock" />
</apex:pageBlockButtons>
<apex:outputpanel id="mainBlock">
<apex:pageBlockSection title="Opportunity Information" rendered="{!NOT(bOppCreated)>
<apex:inputField value="{!oppString.name}"/>
<apex:inputField value="{!oppString.closeDate}"/>
<apex:inputField value="{!oppString.stageName}"/>
<apex:inputField value="{!oppString.amount}"/>
<apex:inputField value="{!oppString.Impact_Level__c}"/>
</apex:pageBlockSection>
<apex:pageBlockSection rendered="{!bOppCreated}">
<apex:outputText value="Congratulations! New Opportunity Succesfully Created" /> <br/>
<apex:outputlink value="/{!oppString.id}">{!oppString.Name}</apex:outputlink>
</apex:pageBlockSection>
</apex:outputpanel>
</apex:pageBlock>
</apex:form>
</apex:page>
First of all modify reRend.setRedirect(true) to reRend.setRedirect(false) in your controller
Second thing is that when you want to display messages after cretaion of new opournity ,
Why we need another controller ?..
just after inserting new oppournity in Saveto() method
Add
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'New oppournity is created'));
Call this message in visual force page by simply mentioning apex:pagemessages/
Now in apex:outputlink mention the newly createtd oppournity </apex:outputlink>
Thanks to Psymn for pointing me in the right direction. The output link was landing me on the Home Tab due to a null Record ID (think this may be because I wasn't using the standard controller - Dev n00b sorry). Created a String variable to take the value of the ID and referenced this in the output link which did the trick.
Controller:
public class OpportunityInformationPage{
public opportunity oppString{get;set;}
public Boolean bOppCreated{get;set;}
public String opp_sfid{get;set;}
public OpportunityInformationPage(){
oppString = new opportunity();
bOppCreated = false;
opp_sfid = null;
}
public PageReference Saveto(){
opportunity opp = new opportunity();
opp.name = oppString.name;
opp.closedate = oppString.closedate;
opp.stagename = oppString.stagename;
opp.amount = oppString.amount;
opp.impact_level__c = oppString.impact_level__c;
insert opp;
bOppCreated = true;
opp_sfid = opp.id;
return null;
}
}
VF Page:
<apex:page controller="OpportunityInformationPage" showHeader="false" sidebar="false">
<apex:sectionHeader title="New Opportunity"/>
<apex:form >
<apex:pageBlock title="Opportunity Edit" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!Saveto}" rerender="mainBlock"/>
</apex:pageBlockButtons>
<apex:outputPanel id="mainBlock">
<apex:pageBlockSection title="Opportunity Information" rendered="{!NOT(bOppCreated)}">
<apex:inputField value="{!oppString.name}"/>
<apex:inputField value="{!oppString.closeDate}"/>
<apex:inputField value="{!oppString.stageName}"/>
<apex:inputField value="{!oppString.amount}"/>
<apex:inputField value="{!oppString.Impact_Level__c}"/>
</apex:pageBlockSection>
<apex:pageBlockSection rendered="{!bOppCreated}">
<apex:outputText value="Congratulations! New Opportunity Succesfully Created" /> <br/>
<apex:outputlink value="/{!opp_sfid}">{!oppString.Name}</apex:outputlink>
</apex:pageBlockSection>
</apex:outputpanel>
</apex:pageBlock>
</apex:form>
</apex:page>
I create page using visual force.How to create the custom button/link for the standard page. I need to open the page after click the custom button/link. how to do this. My code is below
<apex:page standardcontroller="Account" tabstyle="Account" extensions="MyExtension" >
<apex:form id="form1">
<apex:commandlink action="{!showForm2}" value="Show the Form" rendered="{!showForm}" reRender="form2,op1"/>
</apex:form>
<apex:outputpanel id="op1">
<apex:form id="form2">
<apex:sectionheader title="Account Details" subtitle="{!if(Account.Id==null,'New Account',Account.Name)}"></apex:sectionheader>
<apex:pageblock mode="edit" id="leadPB" title="Account Edit">
<apex:pageblockbuttons >
<apex:commandbutton action="{!save}" value="Save"></apex:commandbutton>
<!-- If you wish to implement Save & New functionality you will have to write an Apex Extension with your own Save & New Method -->
<apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
</apex:pageblockbuttons>
<apex:pageBlockSection >
<apex:inputtext value="{!Account.LastName}" label="Customer Name"/>
<apex:inputtext value="{!Account.PersonMobilePhone}"/>
<apex:inputtext value="{!Account.CustomLandLine__c}"/>
<apex:inputField value="{!Account.City__c}"/>
<apex:inputField value="{!Account.PersonEmail}"/>
<apex:inputField value="{!Account.Source__c}"/>
<!-- <apex:commandButton action="{!save}" value="Save!"/>-->
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageMessages />
</apex:form>
</apex:outputpanel>
</apex:page>`
My class code
public with sharing class MyExtension {
private ApexPages.StandardController sc;
public MyExtension(ApexPages.StandardController sc) {
this.sc = sc;
}
public PageReference save() {
Account a = (Account) sc.getRecord();
a.OwnerId = [select Id from User where LastName = 'Kapoor'].Id;
a.OwnerId = [select Id from User where FirstName = 'Raoul'].Id;
return sc.save();
}
public boolean showForm{get;set;}
// default the var to false;
showForm = false;
public void showForm2(){
showForm = true;
}
}
But it shows the following error
the controller
Error: Compile Error: unexpected token: '=' at line 15 column 9
And in my page,i got this error
Unknown method 'AccountStandardController.showForm2()'
how to solve this
Please put showForm = false; right below this.sc = sc;. Everything inside the constuctor is for initialization/default values.
You got the second error because the controller extension is not saved successfully. Once you fix the first one, both errors should disappear.
When I use the standard save action in the commandButton it goes every time to the default page.
But I want to chamge to a custom page when i click on the save button..how??
I tried a lot of things like this...
public Pagereference goHome(){
Pagereference to = Apexpages.currentPage();
to.setRedirect(true);
return to;
}
or
public Pagereference goHome(){
Pagereference to = new Pagereference('/apex/mypage?user=guest'); return to;
}
<apex:commandButton value="Save" action="{!goHome}" />
It should be very simple! Check how this example works for you (you'll need to associate the page to a valid Opportunity by adding ?id=006... in the URL).
public class redirectTestCtrl{
public Opportunity o {get;set;}
public redirectTestCtrl(ApexPages.StandardController ctrl){
o = (Opportunity)ctrl.getRecord();
}
public PageReference save(){
upsert o;
//return new PageReference('/home/home.jsp'); // go to home page
return new PageReference('/' + o.AccountId); // or to the related Account's page
}
}
<apex:page standardController="Opportunity" extensions="redirectTestCtrl">
<apex:outputField value="{!o.AccountId}" />
<apex:form>
<apex:inputField value="{!o.Name}" />
<apex:commandButton value="Save" action="{!save}" />
</apex:form>
<span style="visibility:hidden">{!Opportunity.Name} {!Opportunity.AccountId}</span>
</apex:page>
The standard save() method may be called from an extension using ApexPages.StandardController. Here's a simple example of how it could be achieved:
Apex Page:
<apex:page standardController="Account" extensions="AccountExtension">
<apex:form >
<apex:pageMessages />
<apex:pageBlock title="Account">
<apex:pageBlockSection title="Account Details">
<apex:inputField value="{!account.Name}" />
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!Save}" value="save" />
<apex:commandButton action="{!Cancel}" value="cancel" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Extension Class:
public class AccountExtension {
ApexPages.StandardController stdController;
public AccountExtension(ApexPages.StandardController controller) {
stdController = controller;
}
public PageReference save() {
stdController.save(); // calling standard save() method
return null; // return 'null' to stay on same page
}
}