0

I have a created a list button in contact object and added it to relatedlist. When the button is clicked, in account record relatedlist, a new vf page is opened with all the contacts based on some condition with a checkbox. Now all the selected records need to be attached back to relatedlist from where the button was clicked.

How do I do this. Below is my code.

VF Page

    <apex:page standardController="Contact" recordSetVar="Contact" id="muCt" tabStyle="contact" sidebar="false" extensions="CheckAllUsingJavascriptController">
   <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cContact records -->
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!c.selected}"/>
                </apex:column>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                <apex:column value="{!c.con.Name}" />
                <apex:column value="{!c.con.Email}" />
                <apex:column value="{!c.con.Phone}" />
                </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller

    public class CheckAllUsingJavascriptController{

    public CheckAllUsingJavascriptController(ApexPages.StandardController controller) {
    }
    public CheckAllUsingJavascriptController() {
    }

    public CheckAllUsingJavascriptController(ApexPages.StandardSetController controller) {
    }

    //Our collection of the class/wrapper objects cContact 
 public List<cContact> contactList {get; set;}
//This method uses a simple SOQL query to return a List of Contacts
    public List<cContact> getContacts() {
    currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        if(contactList == null) {
            contactList = new List<cContact>();
            for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]) {
                // As each contact is processed we create a new cContact object and add it to the contactList
                contactList.add(new cContact(c));

            }
        }
        return contactList;
    }


    public PageReference processSelected() {

                //We create a new list of Contacts that we be populated only with Contacts if they are selected
        List<Contact> selectedContacts = new List<Contact>();

        //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
        for(cContact cCon: getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);


            }
        }

        // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
        System.debug('These are the selected Contacts...');
        for(Contact con: selectedContacts) {
        con.AccountId=currentRecordId;
            system.debug(con);
        }
        contactList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
        return null;
    }


// This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}

        //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
        public cContact(Contact c) {
            con = c;
            selected = false;
        }
    }
}

Please assist.

Regards, Sfdc

0

You have not updated the Contacts after updating the Account ID. You have to add update(selectedContacts); after populating the Account ID in the for loop. It will fix your issue. I tried your code by hard coding an Account ID and it is working fine.

// Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
System.debug('These are the selected Contacts...');
for(Contact con: selectedContacts) {
    //con.AccountId=currentRecordId;
    con.AccountId='001i000001eMokE';
    system.debug(con);
}
update(selectedContacts);
contactList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
return null;
  • Thanks a lot Saroj for your quick review. You are right. I got it working after updating contacts. Your one line of code helped me. Thanks once again @saroj. – sfdcdev Jul 4 '16 at 12:25
  • Hi Saroj, I need some more help from you. When I try to implement the same for custom object, I am unable to do so. Can you please point out where do I need to make changes to use custom object. I guess I am stuck public List<cContact> getContacts() and the for loop that is querying. If I change the object in query, I get error. Please assist. – sfdcdev Jul 5 '16 at 7:26

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.