Last active
May 18, 2016 22:51
-
-
Save Uncrasher/6f0c55fbb00fe55a6d548226ec536780 to your computer and use it in GitHub Desktop.
AccountMerger (copies contacts to another account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page Controller="AccountMergerController" tabStyle="Account"> | |
<apex:form > | |
<apex:pageBlock id="pageBlock1" title="Account Merger" > | |
<apex:pageMessage summary="Account Merger copies the selected source account's contacts, assigns the new contacts to the Destination Account and prefix's 'merged_' to the source contacts last name and email fields." | |
severity="info" strength="2" /> | |
<apex:pageBlockButtons location="top" > | |
<apex:commandButton value="Get Source Contacts" action="{!getSourceContacts}" rerender="pageBlock2" /> | |
</apex:pageBlockButtons> | |
<apex:pageBlockSection title="Select the Source and Destination (Master) Accounts" > | |
<apex:inputField label="Source Account" value="{!sourceOpp.AccountId}" /> | |
<apex:inputField label="Destination Account (Master)" value="{!destOpp.AccountId}" /> | |
</apex:pageBlockSection> | |
<apex:messages /> | |
</apex:pageBlock> | |
<apex:pageblock id="pageBlock2" title="Select Source Contacts to Copy to Selected Destination Account" > | |
<apex:pageblockButtons > | |
<apex:commandButton value="Process Selected Contacts" action="{!processSelected}" disabled="{!disabledProcessButton}" /> | |
</apex:pageblockButtons> | |
<!-- In our table we are displaying the cContact records --> | |
<apex:pageBlockTable id="pageBlockTable" value="{!contacts}" var="c" rendered="{!showPageBlockTable}" > <!-- rendered="{!showPageBlockTable}" --> | |
<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 headerValue="Change Email" > | |
<apex:inputText value="{!c.changeEmail}" /> | |
</apex:column> | |
<apex:column value="{!c.con.Phone}" /> | |
</apex:pageBlockTable> | |
</apex:pageblock> | |
</apex:form> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class AccountMergerController { | |
public Boolean disabledProcessButton { get; set; } | |
public Boolean showPageBlockTable { get; set; } | |
public Boolean showProcessButton { get; set; } | |
public Opportunity sourceOpp { get; set; } | |
public Opportunity destOpp { get; set; } | |
//Our collection of the class/wrapper objects cContact | |
public List<cContact> contactList {get; set;} | |
//For Account Billing Address | |
private String BillingStreet; | |
private String BillingCity; | |
private String BillingState; | |
private String BillingPostalCode; | |
private String BillingCountry; | |
//Controller Constructor | |
public AccountMergerController() { | |
sourceOpp = [ SELECT id, account.Name, accountId FROM Opportunity LIMIT 1 ]; | |
destOpp = [ SELECT id, account.Name, accountId FROM Opportunity LIMIT 1 ]; | |
//Defaults to hide processButton and PageBlockTable | |
disabledProcessButton = true; | |
showPageBlockTable = false; | |
} | |
//This method uses a simple SOQL query to return a List of Contacts | |
public List<cContact> getContacts() { | |
System.debug('sourceOpp: ' + sourceOpp); | |
System.debug('destOpp : ' + destOpp); | |
if(contactList == null) { | |
contactList = new List<cContact>(); | |
for(Contact c: [SELECT id, AccountId, Name, Email, Phone, FirstName, LastName, RecordTypeId, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry | |
FROM Contact | |
WHERE AccountId = :sourceOpp.AccountId | |
ORDER BY Name 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>(); | |
List<Contact> insertContacts = new List<Contact>(); | |
List<Contact> updateSourceContacts = new List<Contact>(); | |
Map<id, String> changeEmailMap = new Map<id, String>(); | |
String changeEmail; | |
//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); | |
//Build map with contact id and Change Email field from VF page | |
changeEmailMap.put(cCon.con.id, cCon.changeEmail); | |
} | |
} | |
// Loop through the selectedContacts, clone the record and update source related contacts and create a new copy of the source contact and assign it to the destincation account | |
System.debug('These are the selected Contacts...'); | |
for(Contact con: selectedContacts) { | |
// Clone source accounts related contact, clone it, assign the cloned contact to the destination account and add it to the insertContacts list | |
Contact insertContact = new Contact(); | |
insertContact = con.clone(false, false); | |
insertContact.AccountId = destOpp.AccountId; | |
//Get the value of the Change Email field on the VF page for contact | |
changeEmail = changeEmailMap.get(con.id); | |
if ( changeEmail != null && changeEmail != '' ) insertContact.email = changeEmail; | |
//Populate the contacts Mailing Address with Destination Account's Billing Address | |
insertContact.MailingStreet = BillingStreet; | |
insertContact.MailingCity = BillingCity; | |
insertContact.MailingState = BillingState; | |
insertContact.MailingPostalCode = BillingPostalCode; | |
insertContact.MailingCountry = BillingCountry; | |
insertContacts.add(insertContact); | |
// Update the source accounts related contact | |
con.email = 'merged_' + con.email; | |
con.firstName = 'merged_' + con.firstName; | |
updateSourceContacts.add(con); | |
} | |
// update updateSourceContacts; | |
insert insertContacts; | |
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info,'Process Completed Successfully. Go to the Destination Account and review.')); | |
disabledProcessButton = true; | |
contactList=null; // we need this line if we performed a write operation because getContacts gets a fresh list now | |
return null; | |
} | |
public void getSourceContacts() { | |
contactList = null; // set contactList to null so that the VF page rerenders | |
showPageBlockTable = true; | |
disabledProcessButton = false; | |
} | |
//********************************** | |
// 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 String changeEmail { get; set; } | |
public Boolean selected { get; set; } | |
//This is the constructor 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; | |
changeEmail = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I changed the lookup "InputField" to inputText just to see if I could get it to work by pasting account ID values. It works the same as it does with inputField lookup (opportunity.accountid). When I click the "Get Source Contacts" they are populated below in a table. When I change the input field data to a different account and click the "Get Source Contacts", nothing happens!!!
Any help is appreciated. Ideally, I want to get it to work with the lookup field to account (magnify glass).