From 5aa9b9967792f29c6c8c873d0102d6c04014ff05 Mon Sep 17 00:00:00 2001 From: david81 Date: Wed, 26 Oct 2011 10:39:52 -0300 Subject: [PATCH 1/5] Update src/classes/batchSegmentCreator.cls --- src/classes/batchSegmentCreator.cls | 79 +++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 10 deletions(-) diff --git a/src/classes/batchSegmentCreator.cls b/src/classes/batchSegmentCreator.cls index 34869a3..271634e 100644 --- a/src/classes/batchSegmentCreator.cls +++ b/src/classes/batchSegmentCreator.cls @@ -26,33 +26,91 @@ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISE OF THE POSSIBILITY OF SUCH DAMAGE. */ -global class batchSegmentCreator implements Database.Batchable{ +gglobal class batchSegmentCreator implements Database.Batchable,Database.Stateful{ global String query; global String email; global Id parentCampaignId; global List segmentCampaignIds; global List segmentSizes; global List membersAdded; - global Boolean success; + global String errorTxt = ''; + global boolean keepOriginal; global database.querylocator start(Database.BatchableContext BC){ return Database.getQueryLocator(query);} global void execute(Database.BatchableContext BC, List scope){ + //Old code from AppEx + /* boolean thisBatchSuccess = memberAssignment.assignMembersNow(parentCampaignId,segmentCampaignIds,segmentSizes,membersAdded); if(!thisBatchSuccess){ success = false; } + */ + + List newCampaignMembers = new List(); + + for(sObject s : scope){ + + CampaignMember oldCM = (CampaignMember)s; + + Boolean assigned = false; + + while(assigned == false){ + Integer assignedList = Math.floor(segmentCampaignIds.size() * Math.Random()).intValue(); + + if((membersAdded[assignedList] < segmentSizes[assignedList])){ + newCampaignMembers.add( + new CampaignMember( + CampaignId = segmentCampaignIds[assignedList], + LeadId = oldCM.LeadId, + ContactId = oldCM.ContactId + ) + ); + assigned = true; + + membersAdded[assignedList]++; + + } + } + + } + + if(!newCampaignMembers.isEmpty()){ + Database.saveresult[] srList = database.insert(newCampaignMembers,false); + + List deleteMe = new List(); + for(Integer i = 0;i{ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new String[] {email}); - mail.setReplyTo('steve@gokubi.com'); + mail.setReplyTo(email); mail.setSenderDisplayName('Batch Processing of Campaigns'); - mail.setSubject(resultHeader + ': Campaign Segmentation Completed - ' + parentCampaign.name); + mail.setSubject('Campaign Segmentation Completed - ' + parentCampaign.name + ' ' + hasErrors); mail.setPlainTextBody('The segmentation of ' + parentCampaign.name + resultBody +' https://na1.salesforce.com/' + parentCampaign.Id); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); @@ -119,7 +177,7 @@ global class batchSegmentCreator implements Database.Batchable{ for(Campaign myCamp : segmentCampaigns){ mySegmentIds.add(myCamp.Id); } - //set sizes for segments + //set sizes List sizes = new List(); sizes.add(100); sizes.add(100); @@ -136,6 +194,7 @@ global class batchSegmentCreator implements Database.Batchable{ batch.segmentCampaignIds = mySegmentIds; batch.segmentSizes = sizes; batch.membersAdded = members; + batch.keepOriginal = false; ID batchprocessid = Database.executeBatch(batch); Test.StopTest(); System.AssertEquals(database.countquery('SELECT count()' +' FROM CampaignMember WHERE campaignId=\'' + parentCampaign.Id + '\''), 0); From 1d57012f41348af14ff18b6769300f175bea30a5 Mon Sep 17 00:00:00 2001 From: david81 Date: Wed, 26 Oct 2011 10:41:59 -0300 Subject: [PATCH 2/5] Added support for retaining original campaign member records --- src/classes/memberAssignment.cls | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/classes/memberAssignment.cls b/src/classes/memberAssignment.cls index 573d110..66adb32 100644 --- a/src/classes/memberAssignment.cls +++ b/src/classes/memberAssignment.cls @@ -33,25 +33,31 @@ public with sharing class memberAssignment { static List sizes; static List members; static List newCampaignMembers; + static Boolean keep; - public static boolean assignMembersNow(Id parentCampaignId, List segmentCampaignIds, List segmentSizes, List membersAdded){ + public static boolean assignMembersNow(Id parentCampaignId, List segmentCampaignIds, List segmentSizes, List membersAdded, Boolean keepOriginal){ campId = parentCampaignId; semgmentIds = segmentCampaignIds; sizes = segmentSizes; members = membersAdded; - + keep = keepOriginal; newCampaignMembers = new List(); - for(CampaignMember thisMember : [select id,campaignId, leadId, contactid from campaignMember where CampaignId = :campId]){ + List oldMembers = [select id,campaignId, leadId, contactid from campaignMember where CampaignId = :campId]; + + for(CampaignMember thisMember : oldMembers){ assignMember(thisMember); } - - if(newCampaignMembers.size()!=0){ + system.debug('******newCampaignMembers = '+newCampaignMembers); + if(!newCampaignMembers.isEmpty()){ try { insert newCampaignMembers; - delete [select id,campaignId, leadId, contactid from campaignMember where CampaignId = :campId]; + if(!keep){ + delete oldMembers; + } return true; } catch (exception e){ + system.debug('******EXCEPTION = ' + e); return false; } } else { From fbe0dace8863afc687db39dd3353871d16f9ee59 Mon Sep 17 00:00:00 2001 From: david81 Date: Wed, 26 Oct 2011 10:43:38 -0300 Subject: [PATCH 3/5] Added support for retaining original campaign member records. --- src/classes/segmentsController.cls | 906 +++++++++++++++-------------- 1 file changed, 461 insertions(+), 445 deletions(-) diff --git a/src/classes/segmentsController.cls b/src/classes/segmentsController.cls index 14ffbe3..24f640f 100644 --- a/src/classes/segmentsController.cls +++ b/src/classes/segmentsController.cls @@ -27,245 +27,259 @@ OF THE POSSIBILITY OF SUCH DAMAGE. */ public with sharing class segmentsController { - public String stringSegments {get; set;} - Integer numberOfSegments {get; set;} - - public Boolean showSegmentsPicker {get; set;} - public Boolean showSegmentCreator {get; set;} - public Boolean showBatchInfo {get; set;} - - public List listOfSegments {get; set;} - public Integer MAX_SEGMENTS = 25; - public Integer memberCount {get; set;} - public Campaign parentCampaign {get; set;} - public Integer MAX_SYNCHRONOUS_MEMBERS {get; set;} - public String batchEmailNotification {get; set;} - - List listOfCampaigns = new List(); - List targetMemberCount = new List(); - List membersAdded = new List(); - Id parentCampaignId; - Integer targetForEachCampaign; - Integer remainderMembers; - Integer submittedMemberCount; - Boolean success; - Integer CONCURRENT_BATCH_LIMIT = 5;//at time of coding, there are at most 5 concurrent batch apex jobs in any org - - List newCampaignMembers = new List(); - - - public class segment{ - public Campaign campaign {get; set;} - public Integer membercount; - public Integer targetMemberCount {get; set;} - public void setTargetMemberCount(Integer value){ - targetMemberCount = value; - targetMemberPercentage = (value/memberCount)*100; - } - public Integer targetMemberPercentage {get; set;} - public Integer membersAdded {get; set;} - } - - - public segmentsController(ApexPages.StandardController stdController){ - parentCampaign = (Campaign)stdController.getRecord(); - parentCampaign = [select id,name,NumberOfContacts, NumberOfLeads,type,status,startDate,endDate from campaign where id=:parentCampaign.id]; - memberCount = parentCampaign.NumberOfContacts + parentCampaign.NumberOfLeads; - showSegmentsPicker = true; - showBatchInfo = false; - MAX_SYNCHRONOUS_MEMBERS = 4000; - - if(memberCount==0){ - ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please add members to this Campaign before creating segments.')); - showSegmentsPicker = false; - showSegmentCreator = false; - } - } - - public void setSegments(){ + public String stringSegments {get; set;} + Integer numberOfSegments {get; set;} + + public Boolean showSegmentsPicker {get; set;} + public Boolean showSegmentCreator {get; set;} + public Boolean showBatchInfo {get; set;} + public Boolean keepOriginal {get; set;} + + public List listOfSegments {get; set;} + public Integer MAX_SEGMENTS = 25; + public Integer memberCount {get; set;} + public Campaign parentCampaign {get; set;} + public Integer MAX_SYNCHRONOUS_MEMBERS {get; set;} + public String batchEmailNotification {get; set;} + + List listOfCampaigns = new List(); + List targetMemberCount = new List(); + List membersAdded = new List(); + Id parentCampaignId; + Integer targetForEachCampaign; + Integer remainderMembers; + Integer submittedMemberCount; + Boolean success; + Integer CONCURRENT_BATCH_LIMIT = 5;//at time of coding, there are at most 5 concurrent batch apex jobs in any org + + List newCampaignMembers = new List(); + + + public class segment{ + public Campaign campaign {get; set;} + public Integer membercount; + public Integer targetMemberCount {get; set;} + public void setTargetMemberCount(Integer value){ + targetMemberCount = value; + targetMemberPercentage = (value/memberCount)*100; + } + public Integer targetMemberPercentage {get; set;} + public Integer membersAdded {get; set;} + } + + + public segmentsController(ApexPages.StandardController stdController){ + parentCampaign = (Campaign)stdController.getRecord(); + parentCampaign = [select id,name,NumberOfContacts, NumberOfLeads,type,status,startDate,endDate from campaign where id=:parentCampaign.id]; + memberCount = parentCampaign.NumberOfContacts + parentCampaign.NumberOfLeads; + showSegmentsPicker = true; + showBatchInfo = false; + MAX_SYNCHRONOUS_MEMBERS = 4000; + + if(memberCount==0){ + ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please add members to this Campaign before creating segments.')); + showSegmentsPicker = false; + showSegmentCreator = false; + } + } + + public void setSegments(){ - if(pattern.matches('^[0-9]+$',stringSegments)){ - numberOfSegments = Integer.valueOf(stringSegments); - targetForEachCampaign = Math.floor(memberCount/numberOfSegments).intValue(); - remainderMembers = memberCount - (targetForEachCampaign*numberOfSegments); - - listOfSegments = new List(); - if(numberOfSegments>0 && numberOfSegments<= MAX_SEGMENTS){ - showSegmentsPicker = false; - showSegmentCreator = true; - //create the segments - //create destination campaigns - - for(Integer segment=0; segment(); + if(numberOfSegments>0 && numberOfSegments<= MAX_SEGMENTS){ + showSegmentsPicker = false; + showSegmentCreator = true; + //create the segments + //create destination campaigns + + for(Integer segment=0; segment segmentSizes = new List(); - List membersAdded = new List(); - listOfCampaigns.clear(); - - for(segment thisSegment : listOfSegments){ - listOfCampaigns.add(thisSegment.campaign); - segmentSizes.add(thisSegment.targetMemberCount); - submittedMemberCount += thisSegment.targetMemberCount; - membersAdded.add(0); - } - - //check to make sure user submitted count is right - if(submittedMemberCount==memberCount){ - - try { - insert listOfCampaigns; - } catch (exception e) { - ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'There was an error creating the segment Campaigns: ' + e.getMessage() )); - return null; - } - - //insert campaign statuses - //assume sent and responded are there, as they will be created in the new campaigns - List statuslist = [SELECT Id, CampaignId, HasResponded, Label, IsDefault,SortOrder from CampaignMemberStatus WHERE CampaignId = :parentCampaign.id and label <> 'Sent' and label <> 'Responded']; - List newStatuses = new List(); - for(Campaign thisSegmentCampaign : listOfCampaigns){ - for(CampaignMemberStatus thisStatus : statuslist){ - CampaignMemberStatus sclone = thisStatus.clone(false); - sclone.CampaignId = thisSegmentCampaign.Id; - newStatuses.add(sclone); - } - } - try { - insert newStatuses; - } catch (exception e) { - ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'There was an error copying statuses: ' + e.getMessage() )); - return null; - } - - List newCampaignIds = new List(); - - for(Campaign myCamp : listOfCampaigns){ - newCampaignIds.add(myCamp.Id); - } - success = false; - if(membercount<=MAX_SYNCHRONOUS_MEMBERS){ - success = memberAssignment.assignMembersNow(parentCampaign.Id,newCampaignIds,segmentSizes,membersAdded); - } else { - - List numBatchJobs = [SELECT Id, Status FROM AsyncApexJob WHERE Status = 'Queued' OR Status = 'Processing']; - if((CONCURRENT_BATCH_LIMIT - numBatchJobs.size())>0){ - - User thisUser = [select email from User where id = :UserInfo.getUserId()]; - batchEmailNotification = thisUser.email; - batchSegmentCreator batchSegments = new batchSegmentCreator(); - batchSegments.query='select id,campaignId, leadId, contactid from campaignMember where CampaignId = \'' + parentCampaign.Id + '\''; - batchSegments.parentCampaignId = parentCampaign.Id; - batchSegments.segmentCampaignIds = newCampaignIds; - batchSegments.segmentSizes = segmentSizes; - batchSegments.membersAdded = membersAdded; - batchSegments.email = batchEmailNotification; - ID batchprocessid = Database.executeBatch(batchSegments); - - showSegmentCreator = false; - showBatchInfo = true; - - return null; - } else { - ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'There are currently ' + numBatchJobs.size() + ' Apex jobs scheduled. Try again later.')); - return null; - } - } - - PageReference pageRef = new PageReference('/' + parentCampaign.Id); - pageRef.setRedirect(true); - return pageRef; - } else { - ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please make sure your segment member counts add up to the total member count.')); - return null; - } - - } - - public PageReference cancel(){ - PageReference pageRef = new PageReference('/' + parentCampaign.Id); - pageRef.setRedirect(true); - return pageRef; - } - - static testMethod void segmentation() { - - String stringSegments = '3'; - Integer segments = 3; - Integer contacts = 10; - //create a campaign - Campaign parentCampaign = new Campaign( - name='Parent for test', - IsActive=true - ); - - insert parentCampaign; - - List newContacts = new List(); - - - //create 10 contacts - for (Integer contactNumber=0; contactNumber myCampaignMembers = new List(); - - //add them to campaign - for(contact thisContact : newContacts){ - CampaignMember thisCM = new CampaignMember( - CampaignId=parentCampaign.Id, - ContactId = thisContact.Id - ); - myCampaignMembers.add(thisCM); - - } - - insert myCampaignMembers; - - Test.setCurrentPageReference(new PageReference('Page.segments')); - - ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); + } else { + ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please enter a valid number')); + } + } + + public void resetSegments(){ + numberOfSegments=null; + showSegmentsPicker=true; + showSegmentCreator=false; + } + + public PageReference runSegments(){ + + submittedMemberCount = 0; + + List segmentSizes = new List(); + List membersAdded = new List(); + listOfCampaigns.clear(); + + for(segment thisSegment : listOfSegments){ + listOfCampaigns.add(thisSegment.campaign); + segmentSizes.add(thisSegment.targetMemberCount); + submittedMemberCount += thisSegment.targetMemberCount; + membersAdded.add(0); + } + + //check to make sure user submitted count is right + if(submittedMemberCount==memberCount){ + + try { + insert listOfCampaigns; + } catch (exception e) { + ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'There was an error creating the segment Campaigns: ' + e.getMessage() )); + return null; + } + + //insert campaign statuses + //assume sent and responded are there, as they will be created in the new campaigns + List statuslist = [SELECT Id, CampaignId, HasResponded, Label, IsDefault,SortOrder from CampaignMemberStatus WHERE CampaignId = :parentCampaign.id and label <> 'Sent' and label <> 'Responded']; + List newStatuses = new List(); + for(Campaign thisSegmentCampaign : listOfCampaigns){ + for(CampaignMemberStatus thisStatus : statuslist){ + CampaignMemberStatus sclone = thisStatus.clone(false); + sclone.CampaignId = thisSegmentCampaign.Id; + newStatuses.add(sclone); + } + } + try { + insert newStatuses; + } catch (exception e) { + ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'There was an error copying statuses: ' + e.getMessage() )); + return null; + } + + List newCampaignIds = new List(); + + for(Campaign myCamp : listOfCampaigns){ + newCampaignIds.add(myCamp.Id); + } + + //add chatter message to the parent campaign\ + /* + try { + FeedPost fpost = new FeedPost(); + fpost.ParentId = parentCampaign.id; + fpost.Body = 'The Campaign Segmentation Wizard was run, creating ' + listOfSegments.size() + ' segments.'; + insert fpost; + } catch (exception e) { + system.debug('Chatter feeds aren\'t enabled on Campaigns. Enable Chatter feeds on Campaigns to automatically record when the Campaign Segementation Wizard is run.'); + } + */ + success = false; + if(membercount<=MAX_SYNCHRONOUS_MEMBERS){ + success = memberAssignment.assignMembersNow(parentCampaign.Id,newCampaignIds,segmentSizes,membersAdded,keepOriginal); + } else { + + List numBatchJobs = [SELECT Id, Status FROM AsyncApexJob WHERE Status = 'Queued' OR Status = 'Processing']; + if((CONCURRENT_BATCH_LIMIT - numBatchJobs.size())>0){ + + User thisUser = [select email from User where id = :UserInfo.getUserId()]; + batchEmailNotification = thisUser.email; + batchSegmentCreator batchSegments = new batchSegmentCreator(); + batchSegments.query='select id,campaignId, leadId, contactid from campaignMember where CampaignId = \'' + parentCampaign.Id + '\''; + batchSegments.parentCampaignId = parentCampaign.Id; + batchSegments.segmentCampaignIds = newCampaignIds; + batchSegments.segmentSizes = segmentSizes; + batchSegments.membersAdded = membersAdded; + batchSegments.keepOriginal = keepOriginal; + batchSegments.email = batchEmailNotification; + ID batchprocessid = Database.executeBatch(batchSegments); + + showSegmentCreator = false; + showBatchInfo = true; + + return null; + } else { + ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'There are currently ' + numBatchJobs.size() + ' Apex jobs scheduled. Try again later.')); + return null; + } + } + + PageReference pageRef = new PageReference('/' + parentCampaign.Id); + pageRef.setRedirect(true); + return pageRef; + } else { + ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please make sure your segment member counts add up to the total member count.')); + return null; + } + + } + + public PageReference cancel(){ + PageReference pageRef = new PageReference('/' + parentCampaign.Id); + pageRef.setRedirect(true); + return pageRef; + } + + static testMethod void segmentation() { + + String stringSegments = '3'; + Integer segments = 3; + Integer contacts = 10; + //create a campaign + Campaign parentCampaign = new Campaign( + name='Parent for test', + IsActive=true + ); + + insert parentCampaign; + + List newContacts = new List(); + + + //create 10 contacts + for (Integer contactNumber=0; contactNumber myCampaignMembers = new List(); + + //add them to campaign + for(contact thisContact : newContacts){ + CampaignMember thisCM = new CampaignMember( + CampaignId=parentCampaign.Id, + ContactId = thisContact.Id + ); + myCampaignMembers.add(thisCM); + + } + + insert myCampaignMembers; + + Test.setCurrentPageReference(new PageReference('Page.segments')); + + ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); //pass the controller into the extension segmentsController ext = new segmentsController(sc); ext.stringSegments = stringSegments; @@ -274,6 +288,7 @@ public with sharing class segmentsController { ext.listOfSegments[0].targetMemberCount = 1; ext.listOfSegments[1].targetMemberCount = 2; ext.listOfSegments[2].targetMemberCount = 7; + ext.keepOriginal = false; Test.startTest(); ext.runSegments(); Test.stopTest(); @@ -282,60 +297,61 @@ public with sharing class segmentsController { System.assertEquals(segments,createdCampaigns.size()); System.assert(createdCampaigns[0].NumberOfContacts==1); - System.assert(createdCampaigns[1].NumberOfContacts==2); - System.assert(createdCampaigns[2].NumberOfContacts==7); - System.assertEquals(contacts,createdCampaigns[0].NumberOfContacts + createdCampaigns[1].NumberOfContacts + createdCampaigns[2].NumberOfContacts); - System.assertEquals(0,parentCampaignAfter.NumberOfContacts); - - } - - static testMethod void BIGsegmentation() { - - String stringSegments = '3'; - Integer segments = 3; - Integer contacts = 200; - //create a campaign - Campaign parentCampaign = new Campaign( - name='Parent for test', - IsActive=true - ); - - insert parentCampaign; - - List newContacts = new List(); - - - //create contacts - for (Integer contactNumber=0; contactNumber myCampaignMembers = new List(); - - //add them to campaign - for(contact thisContact : newContacts){ - CampaignMember thisCM = new CampaignMember( - CampaignId=parentCampaign.Id, - ContactId = thisContact.Id - ); - myCampaignMembers.add(thisCM); - - } - - insert myCampaignMembers; - - Test.setCurrentPageReference(new PageReference('Page.segments')); - - ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); + System.assert(createdCampaigns[1].NumberOfContacts==2); + System.assert(createdCampaigns[2].NumberOfContacts==7); + System.assertEquals(contacts,createdCampaigns[0].NumberOfContacts + createdCampaigns[1].NumberOfContacts + createdCampaigns[2].NumberOfContacts); + System.assertEquals(0,parentCampaignAfter.NumberOfContacts); + + } + + static testMethod void BIGsegmentation() { + + String stringSegments = '3'; + Integer segments = 3; + Integer contacts = 200; + //create a campaign + Campaign parentCampaign = new Campaign( + name='Parent for test', + IsActive=true + ); + + insert parentCampaign; + + List newContacts = new List(); + + + //create contacts + for (Integer contactNumber=0; contactNumber myCampaignMembers = new List(); + + //add them to campaign + for(contact thisContact : newContacts){ + CampaignMember thisCM = new CampaignMember( + CampaignId=parentCampaign.Id, + ContactId = thisContact.Id + ); + myCampaignMembers.add(thisCM); + + } + + insert myCampaignMembers; + + Test.setCurrentPageReference(new PageReference('Page.segments')); + + ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); //pass the controller into the extension segmentsController ext = new segmentsController(sc); ext.stringSegments = stringSegments; ext.MAX_SYNCHRONOUS_MEMBERS = 200; ext.setSegments(); + ext.keepOriginal = false; ext.listOfSegments[0].targetMemberCount = 20; ext.listOfSegments[1].targetMemberCount = 100; @@ -348,26 +364,26 @@ public with sharing class segmentsController { System.assertEquals(segments,createdCampaigns.size()); System.assert(createdCampaigns[0].NumberOfContacts==20); - System.assert(createdCampaigns[1].NumberOfContacts==100); - System.assert(createdCampaigns[2].NumberOfContacts==80); - System.assertEquals(contacts,createdCampaigns[0].NumberOfContacts + createdCampaigns[1].NumberOfContacts + createdCampaigns[2].NumberOfContacts); - System.assertEquals(0,parentCampaignAfter.NumberOfContacts); - - } - - static testMethod void noMembers() { - - //create a campaign - Campaign parentCampaign = new Campaign( - name='Parent for test', - IsActive=true - ); - - insert parentCampaign; - - Test.setCurrentPageReference(new PageReference('Page.segments')); - - ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); + System.assert(createdCampaigns[1].NumberOfContacts==100); + System.assert(createdCampaigns[2].NumberOfContacts==80); + System.assertEquals(contacts,createdCampaigns[0].NumberOfContacts + createdCampaigns[1].NumberOfContacts + createdCampaigns[2].NumberOfContacts); + System.assertEquals(0,parentCampaignAfter.NumberOfContacts); + + } + + static testMethod void noMembers() { + + //create a campaign + Campaign parentCampaign = new Campaign( + name='Parent for test', + IsActive=true + ); + + insert parentCampaign; + + Test.setCurrentPageReference(new PageReference('Page.segments')); + + ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); //pass the controller into the extension segmentsController ext = new segmentsController(sc); @@ -375,101 +391,101 @@ public with sharing class segmentsController { System.assertEquals(false,ext.showSegmentsPicker); System.assertEquals(false,ext.showSegmentCreator); - - } - - static testMethod void tooManySegments() { - String stringSegments = '300'; - Integer segments = 300; - Integer contacts = 10; - //create a campaign - Campaign parentCampaign = new Campaign( - name='Parent for test', - IsActive=true - ); - - insert parentCampaign; - - List newContacts = new List(); - - - //create 10 contacts - for (Integer contactNumber=0; contactNumber myCampaignMembers = new List(); - - //add them to campaign - for(contact thisContact : newContacts){ - CampaignMember thisCM = new CampaignMember( - CampaignId=parentCampaign.Id, - ContactId = thisContact.Id - ); - myCampaignMembers.add(thisCM); - - } - - insert myCampaignMembers; - - Test.setCurrentPageReference(new PageReference('Page.segments')); - - ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); + + } + + static testMethod void tooManySegments() { + String stringSegments = '300'; + Integer segments = 300; + Integer contacts = 10; + //create a campaign + Campaign parentCampaign = new Campaign( + name='Parent for test', + IsActive=true + ); + + insert parentCampaign; + + List newContacts = new List(); + + + //create 10 contacts + for (Integer contactNumber=0; contactNumber myCampaignMembers = new List(); + + //add them to campaign + for(contact thisContact : newContacts){ + CampaignMember thisCM = new CampaignMember( + CampaignId=parentCampaign.Id, + ContactId = thisContact.Id + ); + myCampaignMembers.add(thisCM); + + } + + insert myCampaignMembers; + + Test.setCurrentPageReference(new PageReference('Page.segments')); + + ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); //pass the controller into the extension segmentsController ext = new segmentsController(sc); ext.stringSegments = stringSegments; ext.setSegments(); - system.assertEquals(0,ext.listOfSegments.size()); - } - - static testMethod void memberStatusCloning() { - String stringSegments = '3'; - Integer segments = 3; - Integer contacts = 10; - //create a campaign - Campaign parentCampaign = new Campaign( - name='Parent for test', - IsActive=true - ); - - insert parentCampaign; - - insert new CampaignMemberStatus(CampaignId = parentCampaign.Id,Label = 'NewStatus', HasResponded=true,SortOrder=10); - - List newContacts = new List(); - - - //create 10 contacts - for (Integer contactNumber=0; contactNumber myCampaignMembers = new List(); - - //add them to campaign - for(contact thisContact : newContacts){ - CampaignMember thisCM = new CampaignMember( - CampaignId=parentCampaign.Id, - ContactId = thisContact.Id - ); - myCampaignMembers.add(thisCM); - - } - - insert myCampaignMembers; - - Test.setCurrentPageReference(new PageReference('Page.segments')); - - ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); + system.assertEquals(0,ext.listOfSegments.size()); + } + + static testMethod void memberStatusCloning() { + String stringSegments = '3'; + Integer segments = 3; + Integer contacts = 10; + //create a campaign + Campaign parentCampaign = new Campaign( + name='Parent for test', + IsActive=true + ); + + insert parentCampaign; + + insert new CampaignMemberStatus(CampaignId = parentCampaign.Id,Label = 'NewStatus', HasResponded=true,SortOrder=10); + + List newContacts = new List(); + + + //create 10 contacts + for (Integer contactNumber=0; contactNumber myCampaignMembers = new List(); + + //add them to campaign + for(contact thisContact : newContacts){ + CampaignMember thisCM = new CampaignMember( + CampaignId=parentCampaign.Id, + ContactId = thisContact.Id + ); + myCampaignMembers.add(thisCM); + + } + + insert myCampaignMembers; + + Test.setCurrentPageReference(new PageReference('Page.segments')); + + ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); //pass the controller into the extension segmentsController ext = new segmentsController(sc); ext.stringSegments = stringSegments; @@ -484,55 +500,55 @@ public with sharing class segmentsController { Campaign parentCampaignAfter = [select NumberOfContacts from Campaign where Id=:parentCampaign.Id]; List statusesOnParent = [SELECT Id, CampaignId, HasResponded, Label, IsDefault,SortOrder from CampaignMemberStatus WHERE CampaignId = :parentCampaign.id]; - - List statusesOnChildren = [SELECT Id, CampaignId, HasResponded, Label, IsDefault,SortOrder from CampaignMemberStatus WHERE CampaignId IN (select id from Campaign where ParentId = :parentCampaign.id)]; - + + List statusesOnChildren = [SELECT Id, CampaignId, HasResponded, Label, IsDefault,SortOrder from CampaignMemberStatus WHERE CampaignId IN (select id from Campaign where ParentId = :parentCampaign.id)]; + System.assertEquals(3,statusesOnParent.size()); System.assertEquals(9,statusesOnChildren.size()); - } - - static testMethod void UICalcs() { - String stringSegments = '3'; - Integer segments = 3; - Integer contacts = 10; - //create a campaign - Campaign parentCampaign = new Campaign( - name='Parent for test', - IsActive=true - ); - - insert parentCampaign; - - List newContacts = new List(); - - - //create 10 contacts - for (Integer contactNumber=0; contactNumber myCampaignMembers = new List(); - - //add them to campaign - for(contact thisContact : newContacts){ - CampaignMember thisCM = new CampaignMember( - CampaignId=parentCampaign.Id, - ContactId = thisContact.Id - ); - myCampaignMembers.add(thisCM); - - } - - insert myCampaignMembers; - - Test.setCurrentPageReference(new PageReference('Page.segments')); - - ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); + } + + static testMethod void UICalcs() { + String stringSegments = '3'; + Integer segments = 3; + Integer contacts = 10; + //create a campaign + Campaign parentCampaign = new Campaign( + name='Parent for test', + IsActive=true + ); + + insert parentCampaign; + + List newContacts = new List(); + + + //create 10 contacts + for (Integer contactNumber=0; contactNumber myCampaignMembers = new List(); + + //add them to campaign + for(contact thisContact : newContacts){ + CampaignMember thisCM = new CampaignMember( + CampaignId=parentCampaign.Id, + ContactId = thisContact.Id + ); + myCampaignMembers.add(thisCM); + + } + + insert myCampaignMembers; + + Test.setCurrentPageReference(new PageReference('Page.segments')); + + ApexPages.StandardController sc = new ApexPages.standardController(parentCampaign); //pass the controller into the extension segmentsController ext = new segmentsController(sc); ext.stringSegments = stringSegments; @@ -540,13 +556,13 @@ public with sharing class segmentsController { ext.resetSegments(); ext.stringSegments = stringSegments; ext.setSegments(); - system.assertEquals(3,ext.listOfSegments.size()); + system.assertEquals(3,ext.listOfSegments.size()); ext.listOfSegments[0].targetMemberCount = 1; ext.listOfSegments[1].targetMemberCount = 2; ext.listOfSegments[2].targetMemberCount = 7; ext.cancel(); - } - + } + } \ No newline at end of file From 8cc7c79948c57eec37a4e6adae57f3819cc2c55f Mon Sep 17 00:00:00 2001 From: david81 Date: Wed, 26 Oct 2011 10:52:02 -0300 Subject: [PATCH 4/5] Added check box to allow retaining of original campaign member records --- src/pages/segments.page | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/pages/segments.page b/src/pages/segments.page index 22eef2a..039aece 100644 --- a/src/pages/segments.page +++ b/src/pages/segments.page @@ -118,6 +118,10 @@ OF THE POSSIBILITY OF SUCH DAMAGE. Total{!memberCount}100% + + + + From aecf0dc4bed6974262f883f7852263a759170ba8 Mon Sep 17 00:00:00 2001 From: Steve Andersen Date: Tue, 6 Dec 2011 10:14:34 -0800 Subject: [PATCH 5/5] typo in the global declaration --- src/classes/batchSegmentCreator.cls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/batchSegmentCreator.cls b/src/classes/batchSegmentCreator.cls index 271634e..3d5aa36 100644 --- a/src/classes/batchSegmentCreator.cls +++ b/src/classes/batchSegmentCreator.cls @@ -26,7 +26,7 @@ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISE OF THE POSSIBILITY OF SUCH DAMAGE. */ -gglobal class batchSegmentCreator implements Database.Batchable,Database.Stateful{ +global class batchSegmentCreator implements Database.Batchable,Database.Stateful{ global String query; global String email; global Id parentCampaignId;