+ Start a Discussion
Christopher MilnerChristopher Milner 

Create Visualforce Page/Button to Mass Relate Contacts to Implementation Project

Context:
Within our org we have a custom "Workfront" object we are using to track implementation projects for new customers. This object has a related list on the page layout called "Workfront Participants", which is a junction object with two lookup fields. One is a contact lookup and the other lookup for the "Workfront" project. The "Workfront" object is related to accounts in our org. 

Need:
I would like a visualforce page list button that loads a new page which is a list of contacts from the account the current project is related to. A user can then select checkboxes next to each desired contact and click "add" to add them to the "Workfront Participants" related list.

Is this something that is doable/someone could assist me with?

User-added image
Piyush Gautam 6Piyush Gautam 6
Hi Christopher Milner,

I have gone through the requirement, as per your requirement I have created a dummy code.
Please go through the code. Code is covering your essential requirement.

You need to create a custom button over Work Front Object. The button would be a Javascript button calling the Visualforce page with Account Id as WorkFront Id as parameters.

Below is the code with Visualforce Page Image:
User-added image

Visualforce Page:
<apex:page controller="contactListOnWorkFront">
    <apex:form>
        <apex:pageBlock>
            
            <apex:pageBlockTable value="{!contactChkList}" var="conChk">
                <apex:column>
                    <apex:inputCheckbox title="Test" value="{!conChk.checkbox}"></apex:inputCheckbox>{!conChk.con.Name}
                </apex:column>
            </apex:pageBlockTable>
            
            <apex:pageBlockButtons>
                <apex:commandButton action="{!saveMethod}" value="Save"/>
                <apex:commandButton action="{!cancelMethod}" value="Back"/>
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Controller:
public class contactListOnWorkFront {
    
    Public List<Contact> conList;
    Public List<contactWrapper> contactChkList{get; set;}
    
    public contactListOnWorkFront(){
        
        String accId= Apexpages.currentPage().getParameters().get('accId');
        conList= [SELECT Name from CONTACT where accountId= :accId];
        Boolean chk= False;
        
        contactChkList= new List<contactWrapper>();
        
        For(Contact con: conList){
            
            contactWrapper cntWrp= new contactWrapper(con, chk);
            contactChkList.add(cntWrp);
        }
    }
    
    
    public pageReference saveMethod(){
        
        String workFrontId;

        Set<Id> contactToAddSet= new Set<Id>();
        for(contactWrapper cntWr: contactChkList){
            
            if(cntWr.checkbox== True){
                contactToAddSet.add(cntWr.con.Id);
            }
        }
        
        if(contactToAddSet.size()>0){
            
            workFrontId= Apexpages.currentPage().getParameters().get('workFrontId');
            List<Workfront_Participant__c> wrkPartList= new List<Workfront_Participant__c>();
            
            For(String conId: contactToAddSet){
                Workfront_Participant__c wrkPart= new Workfront_Participant__c();
                wrkPart.Contact__c= conId;
                wrkPart.Workfront__c= workFrontId;
                wrkPartList.add(wrkPart);
            }
            
            if(wrkPartList.size()> 0){
                insert wrkPartList;
            }
            
        }
        
        pageReference pr= new pageReference('/'+workFrontId);
        return pr;
    }
    
    
      public pageReference cancelMethod(){
        
        String workFrontId= Apexpages.currentPage().getParameters().get('workFrontId');
        
        pageReference pr= new pageReference('/'+workFrontId);
        return pr;
    }
    
    
    public class contactWrapper{
        
        public contact con{get; set;}
        public boolean checkbox{get; set;}
        
        public contactWrapper(contact cont, Boolean chkbx){
            this.con= cont;
            this.checkbox= chkbx;
        }
    }

}

Modify the code with error handling functionality and use of CSS or SLDS to make it look more beautiful.

I would love to hear from you regarding this requirement. 

If found the satisfying, mark as solved.