I have created a VF page for an object Merchandise,which consists of 3 fields - name,price and quantity.when ever I create a new record and click save button , new record is saved in tat object (Merchandise) but the values in the fields are not refreshed.
What has to be added to the code, which is given below:
public with sharing class mdetailcon {
public Merchandise__c mer{set;get;}
public mdetailcon(){
mer = new Merchandise__c();
public PageReference save() {
insert mer;
return null;
} }
<apex:page sidebar="false" showHeader="false" controller="mdetailcon">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1" >
<apex:inputField value="{!mer.name}" label="Name"/>
<apex:inputField value="{!mer.Price__c}" label="Price"/>
<apex:inputField value="{!mer.Q__c}" label="Quantity"/>
</apex:pageBlockSection>
<apex:commandButton value="save" action="{!save}"/
</apex:pageBlock>
</apex:form>
</apex:page>
I got the answer,check the code below
public PageReference save() {
insert mer;
PageReference pageRef = new pageReference('/apex/merchdetail');
// ('/apex/yourvfpagename',as the vfpage i have created was merchdetail)
pageRef.setRedirect(true);
return pageRef;
}
Related
when i update my existing data with visualforce page , i got thi error
Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Error is in expression '{!edit}' in page contract_object: Class.ContractController.edit: line 28, column 1
An unexpected error has occurred. Your development organization has been notified.
please complite my code without error
My Visual force Code is
<apex:page Controller="ContractController" sidebar="false" showHeader="false">
<apex:form >
<apex:pageBlock title="Contract Form">
<apex:pageBlockSection >
<apex:selectList size="1">
<apex:selectOptions value="{!contactlist}">
</apex:selectOptions>
</apex:selectList>
</apex:pageBlockSection>
<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:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandLink value="Edit" action="{!edit}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
my controller code
public class ContractController {
public List<Contract__c> ContractTemp = new List<Contract__c>();
public List<SelectOption> contactlist
{
get
{
ContractTemp = [Select Id,Name from contract__c];
contactlist = new List<SelectOption>();
for(Contract__c con : ContractTemp)
{
contactlist.add(new SelectOption(con.Id, con.Name));
}
return contactlist;
}
set;
}
public ContractController() {
conObj = new Contract__c();
}
public Contract__c conObj {get; set;}
public void save() {
insert conObj;
}
public void edit() {
update conObj;
}
}
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
}
}