Created
May 22, 2014 02:48
-
-
Save britishboyindc/186066b73b4820b65761 to your computer and use it in GitHub Desktop.
Associate Contact with no Account Id to Bucket Account
This file contains hidden or 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
| @isTest | |
| private class ContactAssociateAccount_TEST { | |
| @isTest | |
| private static void testblankaccountid() { | |
| //First test when no Account exists | |
| Contact testc = new Contact (LastName = 'Test Contact 1'); | |
| insert testc; | |
| List<Contact> checkresult = [SELECT Id, LastName, AccountId, Account.Name FROM Contact WHERE Id = :testc.Id]; | |
| system.assertequals(1, checkresult.size()); | |
| system.assertequals('Citizen', checkresult[0].Account.Name); | |
| //Now check a second contact gets added to same account | |
| Contact testc2 = new Contact (LastName = 'Test Contact 2'); | |
| insert testc2; | |
| checkresult = [SELECT Id, LastName, AccountId, Account.Name FROM Contact WHERE Id = :testc2.Id]; | |
| system.assertequals(1, checkresult.size()); | |
| system.assertequals('Citizen', checkresult[0].Account.Name); | |
| List<Account> checkaccountresults = [SELECT Id, Name, (SELECT Id FROM Contacts) FROM Account WHERE Name = 'Citizen']; | |
| system.assertequals(1, checkaccountresults.size()); | |
| system.assertequals(2, checkaccountresults[0].contacts.size()); | |
| //Now create new account and check new contact at existing account works | |
| Account testa = new Account (Name = 'Test A'); | |
| insert testa; | |
| Contact testc3 = new Contact (LastName = 'Test Contact 2', AccountId = testa.Id); | |
| insert testc3; | |
| checkresult = [SELECT Id, LastName, AccountId, Account.Name FROM Contact WHERE Id = :testc3.Id]; | |
| system.assertequals(1, checkresult.size()); | |
| system.assertequals('Test A', checkresult[0].Account.Name); | |
| } | |
| @isTest | |
| private static void testblankaccountidbulk() { | |
| //Test in bulk with 50% at no account, 50% at existing | |
| //Create new account and check new contact at existing account works | |
| Account testa = new Account (Name = 'Test A'); | |
| insert testa; | |
| List<Contact> testcons = new List<Contact> (); | |
| for (Integer i=0;i <100;i++) { | |
| testcons.add(new Contact(LastName = 'TestC' + String.valueof(i)) ); | |
| } | |
| for (Integer i=100;i <200;i++) { | |
| testcons.add(new Contact(LastName = 'TestC' + String.valueof(i), AccountId = testa.Id)); | |
| } | |
| insert testcons; | |
| List<Account> checkaccountresults = [SELECT Id, Name, (SELECT Id FROM Contacts) FROM Account WHERE Name = 'Citizen']; | |
| system.assertequals(1, checkaccountresults.size()); | |
| system.assertequals(100, checkaccountresults[0].contacts.size()); | |
| checkaccountresults = [SELECT Id, Name, (SELECT Id FROM Contacts) FROM Account WHERE Id = :testa.Id]; | |
| system.assertequals(1, checkaccountresults.size()); | |
| system.assertequals(100, checkaccountresults[0].contacts.size()); | |
| } | |
| } |
This file contains hidden or 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
| rigger ContactAssociateAccount on Contact (before insert) { | |
| Boolean noAccount = FALSE; | |
| for (Contact c : Trigger.new) { | |
| if (c.AccountId == null) { | |
| noAccount = true; | |
| break; | |
| } | |
| } | |
| if (noAccount) { | |
| Account[] citizen = [select Id from Account where name = 'Citizen' limit 2]; | |
| if (citizen.size() == 0 ) { | |
| //create citizen account | |
| citizen = new Account[1]; | |
| citizen [0] = new Account(name='Citizen'); | |
| insert citizen ; | |
| } | |
| for(Contact c : Trigger.new) { | |
| //set contact as a citizen | |
| if (c.AccountId == null) { | |
| c.AccountId = citizen[0].id; | |
| } | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a pretty crude trigger but will get the job done. You could move the name of the bucket account into a custom setting to make it more flexible. You would also normally move the trigger code into a separate handler, but assuming this is temporary to get you over the initial hill, you can leave as is...
You should also think about Data Skew sooner rather than later - anything other 10k contacts at a single account can cause issues...