I have a VF page with two fields one is Client Contact name field with custom look up and other is Role with picklist.
Problem Description:
- Add 2 or more client contacts.
- Delete the middle row.
- Save the page and the row which gets replaced in the deleted row shows a blank value of Client contact name.
Any suggestions or Ideas. Thanks in advance :)
VF Page:
<apex:page standardController="Client_Account_Team_Member__c" extensions="newClientPageController">
<script type="text/javascript">
function openLookup(baseURL, width, modified, searchParam){
var originalbaseURL = baseURL;
var originalwidth = width;
var originalmodified = modified;
var originalsearchParam = searchParam;
width+=100;
var lookupType = baseURL.substr(baseURL.length-3, 3);
if (modified == '1') baseURL = baseURL + searchParam;
var isCustomLookup = false;
// Following "001" is the lookup type for Account object so change this as per your standard or custom object
if(lookupType == "003"){
var urlArr = baseURL.split("&");
var txtId = '';
if(urlArr.length > 2) {
urlArr = urlArr[1].split('=');
txtId = urlArr[1];
}
// Following is the url of Custom Lookup page. You need to change that accordingly
baseURL = "/apex/CustomContactLookup?txt=" + txtId +"&accid={!AccId}";
// Following is the id of apex:form control "myForm". You need to change that accordingly
baseURL = baseURL + "&frm=" + escapeUTF("{!$Component.myForm}");
if (modified == '1') {
baseURL = baseURL + "&lksearch=" + searchParam;
}
// Following is the ID of inputField that is the lookup to be customized as custom lookup
if(txtId.indexOf('contact') > -1 ){
isCustomLookup = true;
}
}
if(isCustomLookup == true){
openPopup(baseURL, "lookup", 350, 480, "width="+width+",height=800,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}
else {
if (modified == '1') originalbaseURL = originalbaseURL + originalsearchParam;
openPopup(originalbaseURL, "lookup", 350, 480, "width="+width+",height=800,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}
}
</script>
<apex:pagemessages ></apex:pagemessages>
<apex:form id="myForm">
<apex:actionFunction name="deleterecord" action="{!Del}" reRender="therepeat" immediate="true">
<apex:param name="rowindex" value="" assignTo="{!selectedRowIndex}"/>
</apex:actionFunction>
<apex:sectionHeader title="Client Contact Edit">
<p style="text-align: center;"><!--<apex:image value="{!$Resource.ClientAccountTeamOC}" style="height:60px;"/>--></p>
</apex:sectionHeader>
<apex:pageBlock mode="Client Contact" title="Client Contact" id="pb2">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!saveandnext}" disabled="{!lstInner.size<0}" />
<apex:commandButton value="Add Client Contact" action="{!Add}" immediate="true"/>
<apex:commandButton value="Cancel" action="{!Cancel}" immediate="true"/>
</apex:pageBlockButtons>
<apex:pageblockSection title="Select Client Contact">
<apex:pageblock id="pb1" >
<apex:pageBlockTable value="{!lstInner}" var="e1" id="therepeat" style="width:600px">
<apex:column style="width:500px" headervalue="Client Contact Name" >
<apex:inputField value="{!e1.ATM.Team_Member_Name__c}" id="contact" required="true" />
</apex:column>
<apex:column headervalue="Role" style="width:400px" >
<apex:inputField value="{!e1.ATM.Role__c}" required="true"/>
</apex:column>
<apex:column headervalue="">
<apex:commandLink action="{!Del}" rerender="pb1,pb2" immediate="true">
<apex:image value="{!$Resource.DeleteIcon}" height="20" width="20"/>
<apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}"></apex:param>
</apex:commandLink>
</apex:column>
</apex:pageblockTable>
</apex:pageBlock>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Class:
public class newClientPageController {
public Client_Account_Team_Member__c ATM {get;set;}
public List<Client_Account_Team_member__c>lstATM = new List<Client_Account_Team_member__c>();
public List<innerClass> lstInner{get;set;}
public String selectedRowIndex{get;set;}
//public string OppId{get;set;}
public string AccId{get;set;}
public Integer count = 1;
public PageReference Saveandnext(){
try
{
Set<Client_Account_Team_member__c>lstAT = new Set<Client_Account_Team_member__c>();
system.debug('helllo'+lstATM);
for(Integer j = 0;j<lstInner.size();j++)
{
if(j>0)
{
if(lstInner[0].ATM.Opportunity__c!=null)
lstInner[j].ATM.Opportunity__c=lstInner[0].ATM.Opportunity__c;
if(lstInner[0].ATM.Policy_Plan__c!=null)
lstInner[j].ATM.Policy_Plan__c=lstInner[0].ATM.Policy_Plan__c;
if(lstInner[0].ATM.Renewal__c!=null)
lstInner[j].ATM.Renewal__c=lstInner[0].ATM.Renewal__c;
}
else{
if(oppid!='')
lstInner[j].ATM.Opportunity__c = OppId.substring(0,15);
if(policyid!='')
lstInner[j].ATM.Policy_Plan__c=policyid.substring(0,15);
if(renewalid!='')
lstInner[j].ATM.Renewal__c=renewalid.substring(0,15);
}
// lstAT.add(lstInner[j].ATM);
lstATM.add(lstInner[j].ATM);
}
system.debug('helllo'+lstATM);
try{
upsert lstATM;
PageReference pageRef = new PageReference( System.currentPageReference().getParameters().get('retURL'));
pageref.setRedirect(true);
return pageRef;
system.debug('data========>');
}catch(Exception E){
ApexPages.addMessages(E);
}
return null;
}
catch(Exception Ex){
lstATM.clear();
ApexPages.addMessages(Ex);
return null;
}
}
public void Add()
{
count = count+1;
addMore();
}
public void addMore()
{
innerClass objInnerClass = new innerClass(count);
lstInner.add(objInnerClass);
system.debug('lstInner++>'+lstInner);
}
public void Del()
{
List<String> delids = new List<String>();
/*
system.debug('selected row index->'+selectedRowIndex);
lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
count = count - 1;
*/
if(selectedRowIndex!= null)
{
for(Integer i=lstInner.size()-1; i>=0;i--)
{
if(lstInner[i].recCount==selectedRowIndex)
{
delids.add(lstInner[i].ATM.Id);
lstInner.remove(i);
}
}
system.debug('delids++>'+delids);
List<Client_Account_Team_Member__c> catDel = [Select id from Client_Account_Team_Member__c where Id in: delids];
Delete catDel;
// lstInner.clear();
}
}
public string oppid='';
public string policyid='';
public string renewalid='';
public newClientPageController(ApexPages.StandardController controller) {
ATM = (Client_Account_Team_Member__c)controller .getRecord();
if(System.currentPageReference().getParameters().get('retURL')!=null&&System.currentPageReference().getParameters().get('id')==null)
{
Id RID=System.currentPageReference().getParameters().get('retURL').replace('/','');
for(opportunity opp1:[select id,name,accountid from opportunity where id =:RID])
{
oppid=opp1.id;
AccId=opp1.AccountId;
}
if(oppid=='')
for(policyplan__c pp:[select id,name,Client__c from policyplan__c where id =:RID])
{
policyid=pp.id;
AccId=pp.Client__c;
}
if(oppid==''&&policyid=='')
for(Renewal__c r:[select id,name,Account_Name__c from Renewal__c where id=:RID])
{
renewalid=r.id;
AccId=r.Account_Name__c;
}
lstInner = new List<innerClass>();
addMore();
selectedRowIndex = '0';
}
if(System.currentPageReference().getParameters().get('Id')!=null)
{
ATM=[select id,Team_Member_Name__c,Role__c,Team_Member_Name__r.accountid,Opportunity__c,Policy_Plan__c,Renewal__c from Client_Account_Team_Member__c where id=:System.currentPageReference().getParameters().get('Id') limit 1];
lstInner = new List<innerClass>();
innerClass objInnerClass = new innerClass(count);
objInnerClass.ATM=ATM;
Accid=ATM.Team_Member_Name__r.accountid;
lstInner.add(objInnerClass);
}
//ATM.Account__c = AccID;
}
public pagereference cancel()
{
pagereference p=new pagereference(System.currentPageReference().getParameters().get('retURL'));
return p;
}
public class innerClass
{
public String recCount{get;set;}
public Client_Account_Team_Member__c ATM{get;set;}
public Double sum;
newClientPageController ncpc;
public innerClass(Integer intCount)
{
recCount = String.valueOf(intCount);
ATM = new Client_Account_Team_Member__c();
}
}
}