1

I am trying to Clone account detail page with contact related list using Custom button which redirects to VF page.I am able to clone detail page with contact related list,but On clicking 'Add row' button,expected output is to display New row for Contact Name.This should happen on every click of button.On every click,a new row should be displayed.

enter image description here

VF page:

<apex:page standardController="Account" extensions="AccountClone" tabStyle="Account">
    <apex:form>
        <apex:pageBlock>
            <apex:sectionHeader title="AccountClone"/>
            <apex:pageBlockSection title="Account Detail">
                <apex:inputField value="{!AccountNew.Name}"/>
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!ContactList}" var="Contacts" columns="1" id="contacttable" >
                <apex:column headerValue="Name">
                    <apex:inputfield value="{!Contacts.lastName}" />
                </apex:column>
            </apex:pageBlockTable>
            <apex:commandButton value="save clone" action="{!clonerelatedlist}" />
            <apex:commandButton value="add row" action="{!addRow}" reRender="contacttable"/>
            </apex:pageBlock>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Extension code:

public with sharing class AccountClone {

  public ID AccountId {get;set;} 

  public Account AccountOld {get;set;}

  public Account AccountNew {get;set;}

  public List<Contact> ContactList {get;set;}

  public Contact ContactNew {get;set;}

  public List<Contact> ContactListNew = new List<Contact>();

  public AccountClone(ApexPages.StandardController controller) {

    accountID=Apexpages.CurrentPage().getParameters().get('id');

    clonewithcontact();

  }

  public void clonewithcontact(){

    AccountOld = [Select Name,(Select lastName from contacts) from Account where ID=:accountID];

    AccountNew =AccountOld.clone(false,false,false,false);

    ContactList= new List<Contact>();

    ContactList.addALL(AccountOld.Contacts);

  }

  //function to create contact

  public void addRow(){

    contact con = new contact();

    ContactList.add(con);

  }

  public pageReference clonerelatedlist(){

    Database.insert(AccountNew);


    for(Contact c: ContactList){

      ContactNew =c.clone();

      ContactNew.AccountId=AccountNew.Id;

      ContactListNew.add(ContactNew);
    }

    insert ContactListNew;

    pageReference pg = new pageReference('/'+AccountNew.Id);

    pg.setRedirect(True);

    return pg;
  }

}
4
  • when does addRow() get invoked? In what order to the controller's actionmethods get invoked?
    – cropredy
    Jan 12, 2016 at 14:56
  • it will be called on clicking a button Addrow which is defined in VF page
    – devsfdc
    Jan 12, 2016 at 15:04
  • @devsfdc Thanks for adding the Visualforce to your question! There's still additional information that is needed before I (or others) can give you a correct solution. Please edit your question to include what you expect to see in the pageBlockTable for the Contacts when you first navigate to the Visualforce page, and what you expect to see in the pageBlockTable after clicking the Add Row button.
    – Derek F
    Jan 12, 2016 at 15:57
  • Derek,added information about expected result.I hope,this will be good
    – devsfdc
    Jan 12, 2016 at 17:11

1 Answer 1

1

Your problem here is that the Contact record you're creating in addRow() is never being related to your new (cloned) Account.

cloneRelatedList() goes through the existing Contacts, clones them, and then re-parents them. That part is correct. However, you're only looping through ContactList to re-parent, and not ContactListNew (which is where you're putting the new Contact created by addRow().)

A simple change to make this work the way you're expecting it to would be to separate the cloning and re-parenting of existing Contacts into two loops like this. .

public pageReference clonerelatedlist(){

  Database.insert(AccountNew);

  for(Contact c: ContactList){
    ContactNew =c.clone();    
    ContactListNew.add(ContactNew);
  }

  for(Contact c : ContactListNew){
    c.AccountId=AccountNew.Id;
  }

  insert ContactListNew;

  pageReference pg = new pageReference('/'+AccountNew.Id);

  pg.setRedirect(True);

  return pg;
}

+edit:

While my previous answer solved one of the issues with the provided code, it didn't answer the text of the amended question.

The reason why addRow() isn't adding a row to your <apex:pageBlockTable> is because you are attempting to give it Accounts.Contacts, which is the list of contacts from the Account that you're going to clone, rather than the ContactListNew variable that addRow() is adding your new Contact to.

That said, I still don't think that you've identified all of the issues (or the core issue) that you're facing, and because of that, I don't think your code is complete.

addRow() simply creates a new Contact records, and adds it to a list. Nowhere in your provided code are you setting the Contact's LastName. As is, you'll be seeing rows being added to your table, but they'll be blank rows.

I'm afraid that I can't provide much more help with what you've provided us thus far.

5
  • Derek: Yes,that is fine.What I want is when I click on 'Addrow' button(Button is defined in VF page),addrow() function will be called and it should display a row of Contact with 'Name'(this is also done in VF page)
    – devsfdc
    Jan 12, 2016 at 15:03
  • @devsfdc, your comment isn't very clear. I think I can piece together what you're really asking (which is a bit different from what you initially asked)...but it would be very helpful if you would edit your question to include your Visualforce page, and perhaps a screenshot of what your page is displaying versus what you want your page to display.
    – Derek F
    Jan 12, 2016 at 15:15
  • I have added VF code and Screenshot of the current output
    – devsfdc
    Jan 12, 2016 at 15:31
  • I was able to add a new row,Thanks for help Derek.But still it is not displaying new row on each click on Button. And the reason,I have not mentioned 'lastname' in code as currently, My objective was simply to add a new row for contact.
    – devsfdc
    Jan 13, 2016 at 5:35
  • Finally,I got it.I have edited the code.
    – devsfdc
    Jan 13, 2016 at 7:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .