0

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: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;
      }
   }
  • 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 '16 at 12:37
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 '16 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 '16 at 13:53
  • @chaitanya - please pose this as a separate question – cropredy May 17 '16 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.