function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chaitanya salesforcecrmchaitanya salesforcecrm 

How to copy related list records of one contact to another contact?

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 pageUser-added image
<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:pageBlockButtons>
        <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:pageBlockSectionItem>

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

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');
    }

    groupList.add(mainGroupLinker);
}

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

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) {
            toAdd.add(g);

        }
    }

    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;
      }
   }

 
Lucian Mihai CiobanuLucian Mihai Ciobanu
To me it looks good, except that you are not adding the copied Intralinks_Group_and_Contact_Links__c object to the database.
At line 36 you are creating a new one, but you're not doing anything with it. Use the toAdd list at line 37 "toAdd.add(copyGroup );"
That will ensure that at line 54 the copied record will get pushed in the database.

Let me know if you hit any issues.