From ba47e270311a25d34ce5028f79d78739579d208f Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Mon, 13 May 2024 18:12:43 +0000 Subject: [PATCH 1/5] Setting up GitHub Classroom Feedback From 75c0944171c7a760c96d8b5f3ac9fa4d522c80f0 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Mon, 13 May 2024 18:12:45 +0000 Subject: [PATCH 2/5] add online IDE url --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 58b22ac..be92783 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Open in Visual Studio Code](https://classroom.github.com/assets/open-in-vscode-718a45dd9cf7e7f842a935f5ebbe5719a5e09af4491e668f4dbf3b35d5cca122.svg)](https://classroom.github.com/online_ide?assignment_repo_id=15052810&assignment_repo_type=AssignmentRepo) # Developer Kickstart: Testing and Debugging This repository is an essential segment of the Developer Kickstart curriculum at Cloud Code Academy. Tailored specifically for up-and-coming Salesforce developers, this module plunges into the crucial aspects of testing and debugging, underscoring the necessity of robust test classes, effective debugging strategies, and the maintenance of high code coverage. From df50c24af7580cfa4efdf94919c32d11e8804409 Mon Sep 17 00:00:00 2001 From: Oxana Date: Thu, 16 May 2024 19:58:19 -0400 Subject: [PATCH 3/5] Implemented TestDataFactory and tests for Title Normalization. Solved issues in LeadTrigger and LeadTriggerHandler --- .../default/classes/LeadTriggerHandler.cls | 27 ++++-- .../classes/LeadTriggerHandlerTest.cls | 85 +++++++++++++++++++ .../LeadTriggerHandlerTest.cls-meta.xml | 5 ++ .../main/default/classes/TestDataFactory.cls | 21 +++++ .../classes/TestDataFactory.cls-meta.xml | 5 ++ .../main/default/triggers/LeadTrigger.trigger | 13 +-- 6 files changed, 140 insertions(+), 16 deletions(-) create mode 100644 force-app/main/default/classes/LeadTriggerHandlerTest.cls create mode 100644 force-app/main/default/classes/LeadTriggerHandlerTest.cls-meta.xml create mode 100644 force-app/main/default/classes/TestDataFactory.cls create mode 100644 force-app/main/default/classes/TestDataFactory.cls-meta.xml diff --git a/force-app/main/default/classes/LeadTriggerHandler.cls b/force-app/main/default/classes/LeadTriggerHandler.cls index 2bf09c2..04c73c3 100644 --- a/force-app/main/default/classes/LeadTriggerHandler.cls +++ b/force-app/main/default/classes/LeadTriggerHandler.cls @@ -32,17 +32,32 @@ public with sharing class LeadTriggerHandler { */ public static void handleTitleNormalization(List leadsToNormalize) { for (Lead ld : leadsToNormalize) { - if (ld.title == 'vp' || ld.title.contains('v.p.') || ld.title.contains('vice president')) { + if (String.isBlank(ld.Title)) { + continue; + } + if ( + ld.title.containsIgnoreCase('vp') || + ld.title.containsIgnoreCase('v.p.') || + ld.title.containsIgnoreCase('vice president') + ) { ld.Title = 'Vice President'; } else if ( - ld.title.contains('mgr') || - ld.title.contains('manage') || - ld.title.contains('head of department') + ld.title.containsIgnoreCase('mgr') || + ld.title.containsIgnoreCase('manage') || + ld.title.containsIgnoreCase('head of department') ) { ld.Title = 'Manager'; - } else if (ld.title.contains('exec') || ld.title == 'chief' || ld.title.contains('head')) { + } else if ( + ld.title.containsIgnoreCase('exec') || + ld.title.containsIgnoreCase('chief') || + ld.title.containsIgnoreCase('head') + ) { ld.Title = 'Executive'; - } else if (ld.title.contains('assist') || ld.title.contains('deputy') || ld.title == 'jr') { + } else if ( + ld.title.containsIgnoreCase('assist') || + ld.title.containsIgnoreCase('deputy') || + ld.title.containsIgnoreCase('jr') + ) { ld.Title = 'Assistant'; } } diff --git a/force-app/main/default/classes/LeadTriggerHandlerTest.cls b/force-app/main/default/classes/LeadTriggerHandlerTest.cls new file mode 100644 index 0000000..369755c --- /dev/null +++ b/force-app/main/default/classes/LeadTriggerHandlerTest.cls @@ -0,0 +1,85 @@ +/** + * This class contains unit tests for validating the behavior of + * Apex class LeadTriggerHandler + * and trigger LeadTrigger. + * + * Implemented by Oxana Suvorova + */ +@isTest +private class LeadTriggerHandlerTest { + + /* + * LeadTriggerHandler.handleTitleNormalization insert test + */ + @isTest + static void testHandleTitleNormalization_insert() { + // Prepare the test data + List titles = new List{ + 'vp', 'v.p.', 'vice president', 'VPOTUS', 'V.P.', 'vice person', // 5 'Vice President' + 'mgr', 'manage', 'head of department', '', 'Manager', // 4 'Manager' + 'exec', 'chief', 'head', 'Chief Executive', null, // 4 'Executive' + 'assist', 'deputy', 'jr', 'DEPUTY', 'junior'}; // 4 'Assistant' + + List leadsToInsert = TestDataFactory.createLeadsByTitle(titles, false); + + // Perform the test + Test.startTest(); + Database.insert(leadsToInsert); + Test.stopTest(); + + // Retrieve the count of Leads grouped by Title + List groupedLeads = [ + SELECT + Title, + COUNT(Name) aggCount + FROM Lead + GROUP BY Title + ]; + + Map countsByTitle = new Map(); + Integer totalRecords = 0; + for (AggregateResult ar : groupedLeads) { + totalRecords += (Integer) ar.get('aggCount'); + countsByTitle.put((String) ar.get('Title'), (Integer) ar.get('aggCount')); + } + + // Assert that the Title have been correctly changed + Assert.isTrue(countsByTitle.get('Vice President') == 5, 'Expected 5 Leads with Title \'Vice President\''); + Assert.isTrue(countsByTitle.get('Manager') == 4, 'Expected 4 Leads with Title \'Manager\''); + Assert.isTrue(countsByTitle.get('Executive') == 4, 'Expected 4 Leads with Title \'Executive\''); + Assert.isTrue(countsByTitle.get('Assistant') == 4, 'Expected 4 Leads with Title \'Assistant\''); + Assert.areEqual(21, totalRecords, 'Expected 21 Lead records'); + } + + /* + * LeadTriggerHandler.handleTitleNormalization update test + */ + @isTest + static void testHandleTitleNormalization_update() { + // Prepare the test data + List titles = new List{'asist', 'depty', 'junior', null, ''}; + List leadsToUpdate = TestDataFactory.createLeadsByTitle(titles, true); + + for (Lead lead : leadsToUpdate) { + lead.Title = 'jr'; + } + // Perform the test + Test.startTest(); + Database.update(leadsToUpdate); + Test.stopTest(); + + // Retrieve updated Leads + List leadsAfterUpdate = [ + SELECT + Title + FROM Lead + WHERE Id IN :leadsToUpdate + ]; + + // Assert that the Title have been correctly updated + Assert.isTrue(leadsAfterUpdate.size() == 5, 'Expected 5 Lead records'); + for (Lead lead : leadsAfterUpdate) { + Assert.areEqual('Assistant', lead.Title, 'Expected Title \'Assistant\''); + } + } +} \ No newline at end of file diff --git a/force-app/main/default/classes/LeadTriggerHandlerTest.cls-meta.xml b/force-app/main/default/classes/LeadTriggerHandlerTest.cls-meta.xml new file mode 100644 index 0000000..019e850 --- /dev/null +++ b/force-app/main/default/classes/LeadTriggerHandlerTest.cls-meta.xml @@ -0,0 +1,5 @@ + + + 59.0 + Active + \ No newline at end of file diff --git a/force-app/main/default/classes/TestDataFactory.cls b/force-app/main/default/classes/TestDataFactory.cls new file mode 100644 index 0000000..5cd4a33 --- /dev/null +++ b/force-app/main/default/classes/TestDataFactory.cls @@ -0,0 +1,21 @@ +/** + * TestDataFactory contain methods that can be called by test methods to perform useful tasks + */ +@isTest +public class TestDataFactory { + + public static List createLeadsByTitle(List titles, Boolean doInsert) { + List leadsToInsert = new List(); + for (String title : titles) { + leadsToInsert.add( + new Lead(LastName = 'Test Lead ' + title, + Company = 'Test Company', + Title = title)); + } + if (doInsert) { + Database.insert(leadsToInsert); + } + + return leadsToInsert; + } +} \ No newline at end of file diff --git a/force-app/main/default/classes/TestDataFactory.cls-meta.xml b/force-app/main/default/classes/TestDataFactory.cls-meta.xml new file mode 100644 index 0000000..019e850 --- /dev/null +++ b/force-app/main/default/classes/TestDataFactory.cls-meta.xml @@ -0,0 +1,5 @@ + + + 59.0 + Active + \ No newline at end of file diff --git a/force-app/main/default/triggers/LeadTrigger.trigger b/force-app/main/default/triggers/LeadTrigger.trigger index 74f7e8d..9e5c4bb 100644 --- a/force-app/main/default/triggers/LeadTrigger.trigger +++ b/force-app/main/default/triggers/LeadTrigger.trigger @@ -15,20 +15,13 @@ * - It's essential to test the trigger thoroughly after making any changes to ensure its correct functionality. * - Debugging skills will be tested, so students should look out for discrepancies between the expected and actual behavior. */ -trigger LeadTrigger on Lead(before insert) { +trigger LeadTrigger on Lead(before insert, before update, after insert, after update) { switch on Trigger.operationType { - when BEFORE_INSERT { + when BEFORE_INSERT, BEFORE_UPDATE { LeadTriggerHandler.handleTitleNormalization(Trigger.new); LeadTriggerHandler.handleAutoLeadScoring(Trigger.new); } - when BEFORE_UPDATE { - LeadTriggerHandler.handleTitleNormalization(Trigger.new); - LeadTriggerHandler.handleAutoLeadScoring(Trigger.new); - } - when AFTER_INSERT { - LeadTriggerHandler.handleLeadAutoConvert(Trigger.new); - } - when AFTER_UPDATE { + when AFTER_INSERT, AFTER_UPDATE { LeadTriggerHandler.handleLeadAutoConvert(Trigger.new); } } From f0b5deaf6cec965e965a4434eb2493408b9268d8 Mon Sep 17 00:00:00 2001 From: Oxana Date: Thu, 16 May 2024 21:21:11 -0400 Subject: [PATCH 4/5] Created test for testHandleAutoLeadScoring. Updated TestDataFactory. Fixed issue in LeadTriggerHandler --- .../default/classes/LeadTriggerHandler.cls | 13 +++---- .../classes/LeadTriggerHandlerTest.cls | 34 +++++++++++++++++++ .../main/default/classes/TestDataFactory.cls | 19 +++++++++++ 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/force-app/main/default/classes/LeadTriggerHandler.cls b/force-app/main/default/classes/LeadTriggerHandler.cls index 04c73c3..462fd98 100644 --- a/force-app/main/default/classes/LeadTriggerHandler.cls +++ b/force-app/main/default/classes/LeadTriggerHandler.cls @@ -76,21 +76,18 @@ public with sharing class LeadTriggerHandler { */ public static void handleAutoLeadScoring(List leadsToScore) { for (Lead ld : leadsToScore) { - Integer score = 10; + Integer score = 0; // Check and add points based on the specified conditions - if (ld.LeadSource == 'Website' && ld.Email != null) { - score = 3; + if (ld.LeadSource == 'Web' && ld.Email != null) { + score += 3; } - if (ld.Phone != null) { - score = 5; + score += 5; } - if (ld.Industry == 'Technology') { - score = 10; + score += 10; } - ld.Lead_Score__c = score; // Set the computed score back to the lead } } diff --git a/force-app/main/default/classes/LeadTriggerHandlerTest.cls b/force-app/main/default/classes/LeadTriggerHandlerTest.cls index 369755c..b3d188b 100644 --- a/force-app/main/default/classes/LeadTriggerHandlerTest.cls +++ b/force-app/main/default/classes/LeadTriggerHandlerTest.cls @@ -82,4 +82,38 @@ private class LeadTriggerHandlerTest { Assert.areEqual('Assistant', lead.Title, 'Expected Title \'Assistant\''); } } + + /* + * LeadTriggerHandler.handleTitleNormalization update test + */ + @isTest + static void testHandleAutoLeadScoring() { + // Prepare the test data + List> params = new List>(); + params.add(new Map{'Lead_Score__c' => 20, 'LeadSource' => null}); // 0 + params.add(new Map{'LeadSource' => 'Other', 'Email' => 'test@mail.com'}); // 0 + params.add(new Map{'LeadSource' => 'Web', 'Email' => 'test@mail.com'}); // 3 + params.add(new Map{'Phone' => '(908)345-1234', 'Industry' => 'Government'}); // 5 + params.add(new Map{'LeadSource' => 'Web', 'Email' => 'test2@mail.com', 'Phone' => '(908)345-2345'}); // 8 + params.add(new Map{'Lead_Score__c' => 10, 'Email' => 'test3@mail.com', 'Industry' => 'Technology'}); // 10 + params.add(new Map{'LeadSource' => 'Web', 'Email' => 'test4@mail.com', 'Industry' => 'Technology'}); // 13 + params.add(new Map{'LeadSource' => 'Web', 'Phone' => '(908)346-1234', 'Industry' => 'Technology'}); // 15 + params.add(new Map{'LeadSource' => 'Web', 'Email' => 'test5@mail.com', + 'Phone' => '(908)346-1234', 'Industry' => 'Technology'}); // 18 + + List leadsToScore = TestDataFactory.createLeadsByParams(params, false); + + // Perform the test + Test.startTest(); + LeadTriggerHandler.handleAutoLeadScoring(leadsToScore); + Test.stopTest(); + + // Assert that the Score calculates correctly + List scoreVariants = new List{0, 0, 3, 5, 8, 10, 13, 15, 18}; + for (Integer i = 0; i < leadsToScore.size(); i++) { + Assert.isTrue(leadsToScore[i].Lead_Score__c <= 18, 'Lead score shouldn\'t be more than 18'); + Assert.areEqual(scoreVariants[i], leadsToScore[i].Lead_Score__c, 'Score has not correctly calculated'); + } + + } } \ No newline at end of file diff --git a/force-app/main/default/classes/TestDataFactory.cls b/force-app/main/default/classes/TestDataFactory.cls index 5cd4a33..20a214a 100644 --- a/force-app/main/default/classes/TestDataFactory.cls +++ b/force-app/main/default/classes/TestDataFactory.cls @@ -18,4 +18,23 @@ public class TestDataFactory { return leadsToInsert; } + + public static List createLeadsByParams(List> params, Boolean doInsert) { + List leads = new List(); + for (Integer i = 0; i < params.size(); i++) { + Lead newLead = new Lead(); + newLead.LastName = 'Test Lead ' + i; + newLead.Company = 'Test Company'; + Map locParams = params[i]; + for (String key : locParams.keySet()) { + newLead.put(key, locParams.get(key)); + } + leads.add(newLead); + } + if (doInsert) { + Database.insert(leads); + } + + return leads; + } } \ No newline at end of file From a20a33d273407f66c3532836a6a8e312e81ba82c Mon Sep 17 00:00:00 2001 From: Oxana Date: Sat, 18 May 2024 10:25:48 -0400 Subject: [PATCH 5/5] Fixed handleLeadAutoConvert. Implemented tests for handleLeadAutoConvert --- .../default/classes/LeadTriggerHandler.cls | 68 ++++++++----- .../classes/LeadTriggerHandlerTest.cls | 99 +++++++++++++++++++ .../main/default/classes/TestDataFactory.cls | 80 +++++++++++++++ 3 files changed, 221 insertions(+), 26 deletions(-) diff --git a/force-app/main/default/classes/LeadTriggerHandler.cls b/force-app/main/default/classes/LeadTriggerHandler.cls index 462fd98..554ba60 100644 --- a/force-app/main/default/classes/LeadTriggerHandler.cls +++ b/force-app/main/default/classes/LeadTriggerHandler.cls @@ -116,42 +116,58 @@ public with sharing class LeadTriggerHandler { * - One of the errors is map related. Make sure you are using the correct contact map key */ public static void handleLeadAutoConvert(List leads) { + // Get convert status + LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = TRUE LIMIT 1]; // Step 1: Gather all lead emails Map leadToEmailMap = new Map(); for (Lead lead : leads) { - leadToEmailMap.put(lead.Id, lead.Email); + if (!lead.IsConverted && lead.Status != convertStatus.MasterLabel && lead.Email != null) { + leadToEmailMap.put(lead.Id, lead.Email); + } + } + + if (leadToEmailMap.size() == 0) { + return; } // Step 2: Find matching contacts based on email - Map emailToContactMap = new Map(); - for (Contact c : [SELECT Id, Email, AccountId FROM Contact WHERE Email IN :leadToEmailMap.values()]) { - if (!emailToContactMap.containsKey(c.Email)) { - emailToContactMap.put(c.Email, c); - } else { - // If we found another contact with the same email, we don't auto-convert. - // So we remove the email from the map. - emailToContactMap.remove(c.Email); - } + List ars = [ + SELECT Email + FROM Contact + GROUP BY Email + HAVING COUNT(Id) = 1 + AND Email IN :leadToEmailMap.values() + ]; + List singleMatchEmail = new List(); + for (AggregateResult ar : ars) { + singleMatchEmail.add((String) ar.get('Email')); } - // Step 3: Auto-convert leads - List leadConverts = new List(); - LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = TRUE LIMIT 1]; - for (Id leadId : leadToEmailMap.keySet()) { - String leadEmail = leadToEmailMap.get(leadId); - if (emailToContactMap.containsKey(leadEmail)) { - Database.LeadConvert lc = new Database.LeadConvert(); - lc.setLeadId(leadId); - lc.setContactId(emailToContactMap.get(leadEmail).Id); // Use existing Contact Id - lc.setAccountId(emailToContactMap.get(leadEmail).AccountId); // Use existing Account Id - lc.setDoNotCreateOpportunity(true); // Assuming we don't want to create an opportunity - lc.setConvertedStatus(convertStatus.MasterLabel); // Set the converted status - leadConverts.add(lc); + // If we have matching contacts + if (singleMatchEmail.size() > 0) { + Map emailToContactMap = new Map(); + for (Contact contact : [SELECT Id, Email, AccountId FROM Contact WHERE Email IN :singleMatchEmail]) { + emailToContactMap.put(contact.Email, contact); } - } - if (!leadConverts.isEmpty()) { - List lcrs = Database.convertLead(leadConverts); + // Step 3: Auto-convert leads + List leadConverts = new List(); + for (Id leadId : leadToEmailMap.keySet()) { + if (emailToContactMap.containsKey(leadToEmailMap.get(leadId))) { + Contact cont = emailToContactMap.get(leadToEmailMap.get(leadId)); + Database.LeadConvert lc = new Database.LeadConvert(); + lc.setLeadId(leadId); + lc.setContactId(cont.Id); // Use existing Contact Id + lc.setAccountId(cont.AccountId); // Use existing Account Id + lc.setDoNotCreateOpportunity(true); // Assuming we don't want to create an opportunity + lc.setConvertedStatus(convertStatus.MasterLabel); // Set the converted status + leadConverts.add(lc); + } + } + + if (!leadConverts.isEmpty()) { + List lcrs = Database.convertLead(leadConverts); + } } } } \ No newline at end of file diff --git a/force-app/main/default/classes/LeadTriggerHandlerTest.cls b/force-app/main/default/classes/LeadTriggerHandlerTest.cls index b3d188b..caa7a45 100644 --- a/force-app/main/default/classes/LeadTriggerHandlerTest.cls +++ b/force-app/main/default/classes/LeadTriggerHandlerTest.cls @@ -116,4 +116,103 @@ private class LeadTriggerHandlerTest { } } + + @isTest + static void testHandleLeadAutoConvert_insertPositive() { + // Prepare the test data + TestDataFactory.generateAccountWithContacts(50, 7); // 7 contacts with a unique email + Map params = new Map{'LeadSource' => 'Web', 'Email' => 'test'}; + List leads = TestDataFactory.createLeadsWithParams(50, params, false); + + // Perform the test + Test.startTest(); + Database.insert(leads); + Test.stopTest(); + + // Request resulted Leads + List insertedLeads = [ + SELECT Id, Email, IsConverted + FROM Lead + ]; + // Assertions + Assert.isTrue(insertedLeads.size() == 50, 'Expected 50 Leads inserted'); + Integer converted = 0; + for (Integer i = 0; i < 50; i++) { + if (i > 0 && Math.mod(i, 7) == 0) { + Assert.isTrue(insertedLeads[i].IsConverted, 'Lead with email ' + insertedLeads[i].Email + ' expected to be converted'); + converted++; + } else { + Assert.isFalse(insertedLeads[i].IsConverted, 'Lead with email ' + insertedLeads[i].Email + ' shouldn\t be converted'); + } + } + Assert.isTrue(converted == 7, 'Expected 7 Leads converted'); + } + + @isTest + static void testHandleLeadAutoConvert_insertNegative() { + // Prepare the test data + TestDataFactory.generateAccountWithContacts(5, 2); // 2 contacts with a unique email + Map params = new Map{'LeadSource' => 'Web', + 'Status' => 'Closed - Converted', + 'Email' => 'test'}; + List leads = TestDataFactory.createLeadsWithParams(5, params, false); + + // Perform the test + Test.startTest(); + Database.insert(leads); + Test.stopTest(); + + // Request resulted Leads + List insertedLeads = [ + SELECT + Id, + Email, + IsConverted + FROM Lead + ]; + // Assertions + Assert.isTrue(insertedLeads.size() == 5, 'Expected 5 Leads inserted'); + for (Lead lead : insertedLeads) { + Assert.isFalse(lead.IsConverted, 'Lead should\'t be converted'); + } + } + + @isTest + static void testHandleLeadAutoConvert_update() { + // Prepare the test data + TestDataFactory.generateAccountWithContacts(5, 2); // 2 contacts with a unique email + Map params = new Map{'LeadSource' => 'Web'}; + List leads = TestDataFactory.createLeadsWithParams(2, params, true); + + // Change email + List leadsToUpdate = [ + SELECT + Id, + Email + FROM Lead + WHERE Id IN :leads + AND IsConverted = false + ]; + Assert.areEqual(2, leadsToUpdate.size(), 'Expected to get 2 unconverted Leads'); + leadsToUpdate[0].Email = 'test0@mail.com'; // not going to be converted + leadsToUpdate[1].Email = 'test1@mail.com'; // going to be converted + + // Perform the test + Test.startTest(); + Database.update(leadsToUpdate); + Test.stopTest(); + + // Request resulted Leads + List updatedLeads = [ + SELECT + Id, + Email, + IsConverted + FROM Lead + WHERE Id IN :leadsToUpdate + ]; + // Assertions + Assert.isFalse(updatedLeads[0].IsConverted, 'Lead with email ' + updatedLeads[0].Email + ' shouldn\'t be converted'); + Assert.isTrue(updatedLeads[1].IsConverted, 'Lead with email ' + updatedLeads[1].Email + ' expected to be converted'); + } } \ No newline at end of file diff --git a/force-app/main/default/classes/TestDataFactory.cls b/force-app/main/default/classes/TestDataFactory.cls index 20a214a..4a90b9f 100644 --- a/force-app/main/default/classes/TestDataFactory.cls +++ b/force-app/main/default/classes/TestDataFactory.cls @@ -4,6 +4,51 @@ @isTest public class TestDataFactory { + /** + * Creates test Account + * @param name The name value for the Account + * @param doInsert Set true to insert new created records to the database + */ + public static Account getAccount(String name, Boolean doInsert) { + Account acc = new Account(Name = name); + if (doInsert) { + Database.insert(acc); + } + return acc; + } + + /** + * Generates test Account and its Contacts with emails. + * @param numContacts The number of contacts to generate + * @param fraction The number by which create unique emails + * Ex: If generate 50 contacts with fraction 7 we will get 7 Contacts with unique emails + * For fraction 5 we need 5x5+1 Contact to generate + */ + public static void generateAccountWithContacts(Integer numContacts, Integer fraction) { + Account acc = getAccount('Default Account ltd', true); + List contacts = new List(); + for(Integer i = 0; i < numContacts; i++) { + if (Math.mod(i, fraction) == 0) { + contacts.add(new Contact(FirstName = 'Test', + LastName = 'contact' + i, + accountId = acc.Id, + Email = 'test' + i + '@mail.com')); + } else { + contacts.add(new Contact(FirstName = 'Test', + LastName = 'contact' + i, + accountId = acc.Id, + Email = 'test' + Math.round(i / fraction) + '@mail.com')); + } + } + Database.insert(contacts); + } + + /** + * Creates new Leads. + * @param titles The list of Titles + * @param doInsert Set true to insert new created records to the database + * We will get so many Leads as number of Titles in the list + */ public static List createLeadsByTitle(List titles, Boolean doInsert) { List leadsToInsert = new List(); for (String title : titles) { @@ -19,6 +64,12 @@ public class TestDataFactory { return leadsToInsert; } + /** + * Creates new Leads. + * We will get so many Leads as number of param sets passed to the method + * @param params The list of maps that contains pairs of fieldName/value + * @param doInsert Set true to insert new created records to the database + */ public static List createLeadsByParams(List> params, Boolean doInsert) { List leads = new List(); for (Integer i = 0; i < params.size(); i++) { @@ -37,4 +88,33 @@ public class TestDataFactory { return leads; } + + /** + * Creates new Leads. + * get Leads with set of fields + * Email field generates separately if it was set in params. + * @param numLeads The number of Leads to generate + * @param params The map that contains pairs of fieldName/value + * @param doInsert Set true to insert new created records to the database + */ + public static List createLeadsWithParams(Integer numLeads, Map params, Boolean doInsert) { + List leads = new List(); + for(Integer i = 0; i < numLeads; i++) { + Lead lead = new Lead(); + lead.LastName = 'Test Lead ' + i; + lead.Company = 'Test Compaany'; + for (String key : params.keySet()) { + if (key == 'Email') { + lead.Email = 'test' + i + '@mail.com'; + } else { + lead.put(key, params.get(key)); + } + } + leads.add(lead); + } + if (doInsert) { + Database.insert(leads); + } + return leads; + } } \ No newline at end of file