You need to sign in to do that
Don't have an account?
gaurav.sfdc
Adding opportunities from VF
I created a Opp List in controller and bind it with repeater, In VF page I used InputFild but on saving it gives Null pointer exception but when I uses InputText it works, problem is I cannot use inputText for CloseDate and Stages, what could be the issue with InputFiled
The null exception was because you were trying to access c.LastName.length when LastName was null. The same thing with the opp.Name.Length. When you use a inputText the value isn't null so the .length doesn't cause an error. Here is your controller code with two additional checks for null values. I have tested in my developer account and it works.
public class AddDataController {
public List<Contact> ContactList {get;set;}
public List<Opportunity> OppList {get;set;}
public List<Contact> finalContactList {get;set;}
public List<Opportunity> finalOppList {get;set;}
private final Account acct;
public AddDataController(ApexPages.StandardController stdController ) {
this.acct = (Account)stdController.getRecord();
ContactList = new List<Contact>();
OppList = new List<Opportunity>();
for (integer i=0;i<3;i++){
Contact C = new Contact();
ContactList.add(C);
Opportunity O = new Opportunity();
OppList.add(O);
}
}
public void AddCon() {
Contact C=new Contact();
ContactList.add(C);
}
public void AddOpp() {
Opportunity O=new Opportunity ();
OppList.add(O);
}
Public PageReference Save() {
finalContactList = new List<Contact>();
finalOppList = new List<Opportunity>();
for (Contact c : ContactList) {
c.Account = this.acct;
if(c.LastName != null && c.LastName.length()<>0){
finalContactList.add(c);
}
}
for (Opportunity o : OppList){
if(o.Name != null && o.Name.length()<>0){
finalOppList.add(o);
}
}
if(finalContactList.size() > 0)
insert finalContactList;
if(finalOppList.size() > 0)
insert finalOppList;
return Apexpages.currentPage();
}
}
All Answers
public list<Opportunity> opps {get;set;}
public void addOpp(Opportunity opp){
opps.add(opp);
}
Make sure that your opps list has been instantiated before you attempt to add anything to it. This is just an example to get the point across, there are better ways to handle this situation if it ends up being your problem. If not please post some code and I will troubleshoot it.
public void addOpp(Opportunity opp){
if(this.opps == null) {
this.opps = new list<Opportunity>();
}
opps.add(opp);
}
VF Page
------------
<apex:page standardController="Account" extensions="AddDataController">
<apex:form >
<apex:sectionHeader title="Enter Contacts and Opportunities for Account"/>
<apex:PageBlock title="Account: {!Account.Name}" mode="edit">
<apex:PageBlockButtons >
<apex:commandButton action="{!save}" value=" Save "/>
</apex:PageBlockButtons>
<apex:pageblockSection title="Contacts" columns="1" >
<apex:CommandButton Value="Add more Contact" action="{!AddCon}" />
<apex:repeat value="{!ContactList}" var="contact" >
<div class="row">
<apex:inputField required="False" styleClass="Field" value="{!Contact.LastName}"/>
</div>
</apex:repeat>
</apex:pageblockSection>
<apex:pageblockSection title="Opportunities" columns="1" >
<apex:CommandButton Value="Add more Opp" action="{!AddOpp}"/>
<apex:repeat value="{!OppList}" var="opp" >
<div class="row">
<apex:inputField required="False" styleClass="Field" value="{!opp.Name}"/>
<apex:inputField required="False" styleClass="Field" value="{!opp.CloseDate}"/>
<apex:inputField required="False" styleClass="Field" value="{!opp.StageName}"/>
</div>
</apex:repeat>
</apex:pageblockSection>
</apex:PageBlock>
</apex:form>
</apex:page>
Controller
---------------------
public class AddDataController {
public List<Contact> ContactList {get;set;}
public List<Opportunity> OppList {get;set;}
public List<Contact> finalContactList {get;set;}
public List<Opportunity> finalOppList {get;set;}
private final Account acct;
public void AddCon() {
Contact C=new Contact();
ContactList.add(C);
}
public void AddOpp() {
Opportunity O=new Opportunity ();
OppList.add(O);
}
public AddDataController(ApexPages.StandardController stdController ) {
this.acct = (Account)stdController.getRecord();
ContactList = new List<Contact>();
OppList = new List<Opportunity>();
for (integer i=0;i<3;i++)
{
Contact C=new Contact();
ContactList.add(C);
Opportunity O = new Opportunity();
OppList.add(O);
}
}
Public PageReference Save()
{
//insert contacts
finalContactList = new List<Contact>();
finalOppList = new List<Opportunity>();
for (Contact c : ContactList)
{
c.Account = this.acct;
if(c.LastName.length()<>0)
{
finalContactList.add(c);
}
}
for (Opportunity o : OppList)
{
if(o.Name.length()<>0)
{
finalOppList.add(o);
}
}
if(finalContactList.size() > 0)
insert finalContactList;
if(finalOppList.size() > 0)
insert finalOppList;
return Apexpages.currentPage();
}
}
if you replace inputfield with inputtext in contact section then this error does not come but then come for opportunity list, if i relace all fields with text then date field and stage fields looks odd.
The null exception was because you were trying to access c.LastName.length when LastName was null. The same thing with the opp.Name.Length. When you use a inputText the value isn't null so the .length doesn't cause an error. Here is your controller code with two additional checks for null values. I have tested in my developer account and it works.
public class AddDataController {
public List<Contact> ContactList {get;set;}
public List<Opportunity> OppList {get;set;}
public List<Contact> finalContactList {get;set;}
public List<Opportunity> finalOppList {get;set;}
private final Account acct;
public AddDataController(ApexPages.StandardController stdController ) {
this.acct = (Account)stdController.getRecord();
ContactList = new List<Contact>();
OppList = new List<Opportunity>();
for (integer i=0;i<3;i++){
Contact C = new Contact();
ContactList.add(C);
Opportunity O = new Opportunity();
OppList.add(O);
}
}
public void AddCon() {
Contact C=new Contact();
ContactList.add(C);
}
public void AddOpp() {
Opportunity O=new Opportunity ();
OppList.add(O);
}
Public PageReference Save() {
finalContactList = new List<Contact>();
finalOppList = new List<Opportunity>();
for (Contact c : ContactList) {
c.Account = this.acct;
if(c.LastName != null && c.LastName.length()<>0){
finalContactList.add(c);
}
}
for (Opportunity o : OppList){
if(o.Name != null && o.Name.length()<>0){
finalOppList.add(o);
}
}
if(finalContactList.size() > 0)
insert finalContactList;
if(finalOppList.size() > 0)
insert finalOppList;
return Apexpages.currentPage();
}
}