View operation in salesforce using apex and visualforce - salesforce

Below are the code of my VF and controller class
The save and cancel method works fine but my search method is not working correctly. After saving the objects when i click the view button it says to enter the field name instead of showing the list of objects.
VF code
<apex:page controller="c5">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1">
<apex:inputField value="{!a.Name}"/>
<apex:inputField value="{!a.First_Name__c}"/>
<apex:inputField value="{!a.Last_Name__c}"/>
<apex:inputField value="{!a.Email__c}"/>
<apex:inputField value="{!a.Contact_Number__c}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="save" action="{!save}"/>
<apex:commandButton value="cancel" action="{!cancel}"/>
<apex:commandButton value="view" action="{!search}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
controller class
public class c5
{
public PageReference cancel()
{
PageReference pageRef = new PageReference('/apex/Reg');
pageRef.setRedirect(true);
return pageRef;
}
List<Register__c> b = new List<Register__c>();
Register__c a = new Register__c();
public Register__c geta(){
return a;
}
public PageReference save(){
insert a;
return null;
}
public List<Register__c> getb(){
return b;
}
public PageReference search(){
b = (List<Register__c>)[select Name, First_Name__c, Last_Name__c, Email__c, Contact_Number__c from Register__c];
return null;
}
}

You're assigning the result of your search to the property b but you don't display / use this property in the VisualForce page.
Try something like this:
<apex:page controller="c5">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1">
<apex:inputField value="{!a.Name}"/>
<apex:inputField value="{!a.First_Name__c}"/>
<apex:inputField value="{!a.Last_Name__c}"/>
<apex:inputField value="{!a.Email__c}"/>
<apex:inputField value="{!a.Contact_Number__c}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="save" action="{!save}"/>
<apex:commandButton value="cancel" action="{!cancel}"/>
<apex:commandButton value="view" action="{!search}" rerender="bData"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1" id="bData">
<apex:dataTable value="{!b}" var="theSObject" rendered="{!NOT(ISBLANK(b))}">
<apex:column value="{!theSobject.Name}"/>
</apex:dataTable
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Related

Pre-Populate Lookup filter value from Parent field in VF Page

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;
}
}

Trying to add output link on redirect page to newly created Opportunity from another VF page

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>

Opening a VF Page in new tab/window

I am new to Salesforce Lightning and trying to convert a classic button that opens a VF page in a new pop up window (Detail page button) into a Lightning component doing the same. Below is what I tried, created a New Action in Lightning and linked it to tho the below lightning component
selectCampignContainer.cmp (iframe to open the VF page in a new window)
<aura:component implements="force:lightningQuickAction" access="global">
<aura:attribute name='vfpName' type='String'/>
<iframe src="https://abc.lightning.force.com/apex/ +v.vfpName" width="100%" height="500px;" frameBorder="0"/>
</aura:component>
selectCampignContainerController.cmp
({
handleClick : function (cmp, event, helper) {
var evt = $A.get("e.force:navigateToComponent");
evt.setParams({
componentDef : "c:selectCampignContainer",
componentAttributes: {
vfpName : 'Campaign_Selection_Page'
}
});
evt.fire();
}
});
And the VF page is like
<apex:page standardController="Opportunity" extensions="Campaign_Selection_Page_Ext" id="page" showHeader="false" sidebar="false" lightningStylesheets="true" >
<apex:form id="form">
<apex:pageBlock title="Campaign Attribution" id="pageBlock">
<apex:pageblockSection >
<apex:outputField value="{!currentRecord.CampaignId}" label="Primary Campaign"/>
</apex:pageblockSection>
<apex:pageBlockSection title="Search Criteria" collapsible="false">
<apex:inputText value="{!campaignRecord.Name}"/>
<apex:inputField value="{!campaignRecord.StartDate}"/>
<!--<apex:inputField value="{!campaignRecord.Type}"/>-->
<apex:selectList value="{!campaignRecord.Type}" multiselect="false" size="1">
<apex:selectOptions value="{!campaignType}" ></apex:selectOptions>
</apex:selectList>
<apex:inputField value="{!campaignRecord.EndDate}"/>
</apex:pageBlockSection>
<apex:pageBlockSection columns="1">
<apex:outputPanel style="float:right;">
<apex:commandButton value="Search" action="{!search}" rerender="form"/> <apex:commandButton value="Assign Campaign" action="{!assign}" rerender="form"/>
</apex:outputPanel>
</apex:pageBlockSection>
<apex:pageBlockSection title="Search Results"></apex:pageBlockSection>
<apex:pagemessages ></apex:pagemessages>
<apex:pageBlockTable value="{!campaignRecords}" var="rec" id="pbTable">
<apex:column id="pbColumn" >
<apex:inputCheckbox value="{!rec.bol}">
<apex:actionSupport event="onchange" action="{!selectUnselect}" reRender="pbTable">
<apex:param value="{!rec.Campaig.Id}" assignTo="{!selectedCampaign}" name="recId"/>
</apex:actionSupport>
</apex:inputCheckbox>
</apex:column>
<apex:column value="{!rec.Campaig.Name}"/>
<apex:column value="{!rec.Campaig.Campaign_Short_Name__c }"/>
<apex:column value="{!rec.Campaig.Type}"/>
<apex:column value="{!rec.Campaig.StartDate}"/>
<apex:column value="{!rec.Campaig.EndDate}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
But when I clicked the button in lightning I get error like
This VF page works perfectly in the Classic and I have just included lightningStylesheets="true". Can anyone please let me know what am I missing here

Adding new records and displaying them on visualforce page from custom object

I get an error in visualforce page now. Saying:
Error:Unknown property 'tudent__cStandardController.d'
This happens when I add the delete commandLink button to the vf page. Before I add it it doesn't put out an error but on it doesn't display records on the page.
Thanks for help in advance
Visualforce code:
<apex:page standardController="tudent__c" extensions="vidsav">
<apex:form >
<apex:outputPanel id="check">
<apex:pageBlock title="Dodaj Študenta">
<apex:pageBlockSection columns="1">
<apex:inputField value="{! tudent__c.Name }"/>
<apex:inputField value="{! tudent__c.priimek__c }"/>
<apex:inputField value="{! tudent__c.Datum_rojstva__c }"/>
<apex:inputField value="{! tudent__c.letnik__c }"/>
<apex:inputField value="{! tudent__c.Naslov__c }"/>
<apex:inputField value="{! tudent__c.naziv_fakultete__c }"/>
<apex:inputField value="{! tudent__c.tudijski_program__c }"/>
<apex:inputField value="{! tudent__c.tip_tudija__c }">
<apex:actionSupport event="onchange" rerender="check" />
</apex:inputField>
<apex:inputField value="{! tudent__c.Samopla_nik__c }" rendered="{!IF( tudent__c.tip_tudija__c == 'izredni', true, false )}" />
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save" />
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:outputPanel>
<apex:pageBlock title="Študenti">
<apex:pageBlockTable value="{!studentsR}" var="s" >
<apex:commandLink action="{!deleteStudent}" onclick="if(!confirm('Are you sure?')) return false;">`enter code here`Del
<apex:param value="{!d.Id}" name="idToDel" assignTo="{!SelectedStudentId}"/>
</apex:commandLink>
<apex:column value="{!s.Name}"/>
<apex:column value="{!s.priimek__c}"/>
<apex:column value="{!s.Datum_rojstva__c}"/>
<apex:column value="{!s.letnik__c}"/>
<apex:column value="{!s.Naslov__c}"/>
<apex:column value="{!s.naziv_fakultete__c}"/>
<apex:column value="{!s.tudijski_program__c}"/>
<apex:column value="{!s.tip_tudija__c}"/>
<apex:column value="{!s.Samopla_nik__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class vidsav {
public vidsav(ApexPages.StandardController controller) {
}
public List<tudent__c> studentsR {get;set;}
public String SelectedStudentId {get;set;}
public vidsav() {
loadData();
}
public void loadData() {
studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c Order By CreatedDate desc];
}
public void deleteStudent(){
studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c where id = :SelectedStudentId];
if(studentsR.size() > 0 || studentsR[0].Id != ''){
delete studentsR;
}
loadData();
}
}
Here is the updated code you should use ->
1) Update your PageBlockTable to this -
<apex:pageBlockTable value="{!studentsR}" var="s" >
<apex:column value="{!s.Name}"/>
<apex:column value="{!s.priimek__c}"/>
<apex:column value="{!s.Datum_rojstva__c}"/>
<apex:column value="{!s.letnik__c}"/>
<apex:column value="{!s.Naslov__c}"/>
<apex:column value="{!s.naziv_fakultete__c}"/>
<apex:column value="{!s.tudijski_program__c}"/>
<apex:column value="{!s.tip_tudija__c}"/>
<apex:column value="{!s.Samopla_nik__c}"/>
<apex:column>
<apex:commandLink action="{!deleteStudent}" onclick="if(!confirm('Are you sure?')) return false;" value="Del">
<apex:param value="{!s.Id}" name="idToDel" assignTo="{!SelectedStudentId}"/>
</apex:commandLink>
</apex:column>
You needed to put your commandLink inside an apex:column in order for it to display correctly in the pageBlockTable and also of course the d had to be replaced with a s since your table variable is s (var="s").
2) Update your Controller to this -
public class vidsav {
public vidsav(ApexPages.StandardController controller) {
loadData();
}
public List<Account> studentsR {get;set;}
public String SelectedStudentId {get;set;}
public void loadData() {
studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c Order By CreatedDate desc];
}
public void deleteStudent(){
studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c where id = :SelectedStudentId];
if(studentsR.size() > 0 || studentsR[0].Id != ''){
delete studentsR;
}
loadData();
}
}
You were using two constructors and were calling loadData() from the wrong one and this is why it was not loading data. With this you will be able to delete records and reload your table.
Replace {!d.Id} with {!s.Id}
The variable used to traverse the records in the pageblocktable is s not d.

Error : Unknown constructor

I have created a custom object "Assure__c" and a custom apex class "insEnfant" like this :
<apex:page standardController="Assure__c" extensions="insEnfant" standardStylesheets="true">
<apex:sectionHeader title="Ajouter un assuré"
subtitle="{!$User.FirstName}" help="/help/doc/user_ed.jsp?loc=help"></apex:sectionHeader>
<apex:form >
<apex:pageBlock title="Nouveau assuré" id="thePageBlock" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Enregistrer"></apex:commandButton>
<apex:commandButton action="{!cancel}" value=" Annuler "></apex:commandButton>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Liste des enfants" columns="1"
rendered="{!IF(Assure__c.Nombre_enfants__c > 0, true, false)}">
<apex:pageBlockTable value="{!accts}" var="a" id="table">
<apex:facet name="footer">
<apex:commandLink value="Ajouter" action="{!addRow}" rerender="table,error"/>
</apex:facet>
<apex:column headerValue="Nom">
<apex:inputHidden value="{!Assure__c.Name}" id="theHiddenInput"/>
</apex:column>
<apex:column headerValue="Nom">
<apex:inputField value="{!a.Name}"/>
</apex:column>
<apex:column headerValue="Prénom">
<apex:inputField value="{!a.Prenom__c}"/>
</apex:column>
<apex:column headerValue="Né le">
<apex:inputField value="{!a.Date_de_naissance__c}"/>
</apex:column>
<apex:column headerValue="Lieu de naissance">
<apex:inputField value="{!a.Lieu_de_naissance__c}"/>
</apex:column>
<apex:column headerValue="Situation">
<apex:inputField value="{!a.Situation__c }"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
public class insEnfant{
public List<Enfants__c> accts {get; set;}
public insEnfant(){
accts = new List<Enfants__c>();
accts.add(new Enfants__c());
}
public void addrow(){
accts.add(new Enfants__c());
}
public PageReference save(){
insert accts;
PageReference home = new PageReference('/home/home.jsp');
home.setRedirect(true);
return home;
}
}
But when I tried to save it I obtain this error:
Error: Unknown constructor 'insEnfant.insEnfant(ApexPages.StandardController controller)'
Create Apex method 'insEnfant.insEnfant(ApexPages.StandardController controller)'
As I am new to salesforce.com, can anyone please give me the code for this??
Take a look at http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm which explains that a controller extension needs to have a constructor that takes a StandardController as argument. (You've provided a constructor that takes no arguments.) There's some sample code on that page that you may find helpful.

Resources