In the contact object i have a related list "Intralink Portal group" each contact can have multiple Intralink Porta groups. i have created a visual force page to add more than one intralink portal groups to a contact.now i want to select the Intralink Portal groups from one contact and add all of them to a new contact.how to do it? below is the visualforce page code and controller code.through controller i am able to add intralink portal groups to contact.but i am not able to copy all the list of intralink portal groups from one contact to another. below is the vf page enter image description here

<apex:page Controller="ilGroupCtrl" doctype="html-5.0">
<apex:form >
    <apex:pageBlock title="Add Intralinks Groups">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
            <apex:commandButton action="{!Cancel}" value="Cancel"/>
        <apex:pageBlockSection columns="1" id="main">
            <apex:inputField value="{!mainGroupLinker.Contact__c}" />

            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Copy From" />
                <apex:inputField value="{!copyFrom.Contact__c}" required="false"/>

            <apex:repeat value="{!groupList}" var="groupObj">
                <apex:pageBlockSectionItem >
                <apex:outputLabel value="Intralinks Group" />
                <apex:inputField value="{!groupObj.Intralinks_Portal_Group__c}" required="false"/>
            <apex:commandButton action="{!add}" value="Add" rerender="main"/>

Apex Controller

public class ilGroupCtrl {

Map<String, String> params;

public Intralinks_Group_and_Contact_Links__c mainGroupLinker { get; set; }
public Intralinks_Group_and_Contact_Links__c copyFrom { get; set; }
public List<Intralinks_Group_and_Contact_Links__c> groupList { get; set; }

public ilGroupCtrl() {
    params = ApexPages.currentPage().getParameters();
    mainGroupLinker = new Intralinks_Group_and_Contact_Links__c();
    copyFrom = new Intralinks_Group_and_Contact_Links__c();
    groupList = new List<Intralinks_Group_and_Contact_Links__c>();

    // if QueryString contains a contact Id, autofill the Contact field
    if(params.containsKey('cid')) {
        mainGroupLinker.Contact__c = params.get('cid');


public void add() {
    groupList.add(new Intralinks_Group_and_Contact_Links__c(Contact__c=mainGroupLinker.Contact__c));

public PageReference save() {
    List<Intralinks_Group_and_Contact_Links__c> toAdd = new List<Intralinks_Group_and_Contact_Links__c>();

    //query all the Groups the copyFrom contact has
    if(copyFrom.Contact__c != null) {
       List<Intralinks_Group_and_Contact_Links__c> toCopy = [SELECT Intralinks_Portal_Group__c FROM Intralinks_Group_and_Contact_Links__c where Contact__c = :copyFrom.Contact__c];

        for(Intralinks_Group_and_Contact_Links__c copyGroup : toCopy) {
            Intralinks_Group_and_Contact_Links__c newCopy = new Intralinks_Group_and_Contact_Links__c(Contact__c= mainGroupLinker.Contact__c,Intralinks_Portal_Group__c= copyGroup.Intralinks_Portal_Group__c);



    for (Intralinks_Group_and_Contact_Links__c g : groupList) {
        // save it if it's not null.
        if(g.Intralinks_Portal_Group__c != null) {


    System.debug('toAdd count: ' + toAdd.size());
    if(toAdd.size() > 0) {

        insert toAdd;

    return null;

  Public Pagereference Cancel(){
   Pagereference Page = new         PageReference(ApexPages.currentPage().getParameters().get('https://cs16.salesforce.com/{!mainGroupLinker.Contact__c}'));  
 return Page;
  • Exactly what problem are you having? You say you're not able to add all the groups to another contact. How do you know what you're missing? Try adding lots of debug statements to your controller to see where you're not having your issues. This shouldn't be necessary: if(g.Intralinks_Portal_Group__c != null) { toAdd.add(g); If it's in the list, it won't be null and you'd get a null pointer exception if it was.
    – crmprogdev
    May 16, 2016 at 12:37

1 Answer 1


I took the liberty of renaming some variables (and you must be ruing the decision by whomever to name a custom object with so many words :-))

Your save action method:

public PageReference save() {
 Intralinks_Group_and_Contact_Links__c[] igclInsList = new List<Intralinks_Group_and_Contact_Links__c>();
 for (Intralinks_Group_and_Contact_Links__c igcl : copyFrom.Contact__c != null
                ? [select id,   
                      from Intralinks_Group_and_Contact_Links__c
                      where Contact__c =:copyFrom.Contact__c]
                : new List<Intralinks_Group_and_Contact_Links__c>() {
    Intralinks_Group_and_Contact_Links__c igclCopy = igcl.clone(false,true); // w/o ID
    igclCopy.contact__c = mainGroupLinker.Contact__c;  // new parent
    igclInsList.add(igclCopy);  // build insert List
 insert igclInsList;  // if empty, no problem.  Could be wrapped in try-catch
 return null;
  • I have made some changes to your code and it worked.Thank you
    – chaitanya
    May 17, 2016 at 13:51
  • can you help me in auto-populating the contact field with Contact name. this page will be displayed when user clicks on a Button in one Related list of contact.
    – chaitanya
    May 17, 2016 at 13:53
  • @chaitanya - please pose this as a separate question
    – cropredy
    May 17, 2016 at 14:47

You must log in to answer this question.

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