VF PAGE:
<apex:page standardController="Account" extensions="AddContact">
<apex:form id="myForm">
<apex:panelGrid columns="2">
Account Name: <apex:inputField value="{!account.name}" />
no of contacts: <apex:inputText value="{!noOfRecordsToInsert}" />
</apex:panelGrid>
<apex:repeat value="{!lContacts}" var="x">
<apex:panelGrid columns="8">
First Name:<apex:inputField value="{!x.c.firstName}" />
Last Name:<apex:inputField value="{!x.c.lastName}" />
Lead Source:<apex:inputField value="{!x.c.leadSource}" />
<apex:commandButton action="{!deleteContact}" style="Button" value="Delete Contact" reRender="myForm" immediate="true">
<apex:param value="{!x.counter}" name="selected"
assignTo="{!selectedContact}"/>
</apex:commandButton>
</apex:panelGrid>
</apex:repeat>
<apex:commandButton value="Add Contact" action="{!addAContact}" reRender="myForm" immediate="true"/>
<apex:commandButton value="Save" action="{!saveAccount}" />
</apex:form>
</apex:page>
apex class:
public class AddContact {
ApexPages.StandardController sc;
public Account acct{get;set;}
public Integer marker=2;
public Integer selectedContact{get;set;}
public List<WrapperClass> lContacts{get;set;}
public integer noOfRecordsToInsert {get;set;}
public AddContact(ApexPages.StandardController controller) {
this.acct = (Account)controller.getRecord();
sc=controller;
for(integer i=0;i<=noOfRecordsToInsert;i++){
lContacts=new List<WrapperClass>();(here im struck)
Contact c=new Contact();
lContacts.add(new WrapperClass(c,noOfRecordsToInsert));
}
}
public PageReference deleteContact(){
Integer x=-1;
for(WrapperClass wc:lContacts){
x++;
if(wc.counter==selectedContact){
System.debug('-->selected contact:'+selectedContact+' position:'+x);
break;
}
}
lContacts.remove(x);
return null;
}
public PageReference saveAccount(){
try
{
system.debug('try block');
Database.SaveResult sr = Database.insert(acct, true);
//set<id> idey=new set<id>();
id idey=sr.getId();
integer i;
// map<id,account>Acc=new map<id,account>([select id,no_of_contact__c from account where id in:idey]);
system.debug('account'+sr);
List<Contact> contactList=new List<Contact>();
for(WrapperClass wc:lContacts){
i++;
//account a=new account();
Contact c=new Contact();
c.lastName=wc.c.lastName;
c.firstName=wc.c.firstName;
c.Phone=wc.c.Phone;
c.Email=wc.c.Email;
c.accountid=idey;
contactList.add(c);
insert contactList;
}
}
catch(exception e)
{
system.debug('catch block');
apexpages.addmessage(new apexpages.message(apexpages.severity.error,e.getmessage()));
}
return null;
}
public PageReference addAContact(){
/*integer i=0;
for(WrapperClass wc:lContacts){
i++;*/
Contact c=new Contact();
// for(integer i=0;i<=noOfRecordsToInsert;i++){
//lContacts.add(new WrapperClass(c,noofRecordsToInser));
//}(here i struck)
lContacts.add(new WrapperClass(c,marker));
//lContacts.add(c.get(i));
marker=marker+1;
// }
// }
return null;
}
public class WrapperClass{
public Integer counter{get;set;}
public Contact c{get;set;}
public WrapperClass(Contact cntc,Integer i){
this.c=cntc;
this.counter=i;
}
}
}
1 réponse
Hi Banu,
Sorry for the inconvenience. In case you don't receive a response here, may I also suggest joining the Developer formus below to collaborate with the experts for best practice and advice.
https://developer.salesforce.com/forums
https://salesforce.stackexchange.com/
Hope that helps.
Regards,
Jayson