Last active
November 16, 2016 11:59
-
-
Save florianhoehn/dcd5199aad23efbba8a991237c6ab2ec to your computer and use it in GitHub Desktop.
builder pattern for lead
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
@IsTest(seeAllData=false) | |
public with sharing class LeadBuilder { | |
private Lead testLead = | |
new Lead( | |
FirstName = 'test-FirstName', | |
LastName = 'test-LastName', | |
Email = 'test@email.com', | |
Phone = '01234 567 890', | |
Street__c = 'test street', | |
City__c = 'test city', | |
Company = 'test company', | |
Lead_Type__c = 'test lead type', | |
Status = 'New' | |
); | |
public LeadBuilder withRecordType(String recordTypeLabel) { | |
this.testLead.RecordTypeId = | |
Schema.SObjectType.Lead | |
.getRecordTypeInfosByName() | |
.get(recordTypeLabel) | |
.getRecordTypeId(); | |
return this; | |
} | |
public LeadBuilder withFirstName(String firstName) { | |
this.testLead.FirstName = firstName; | |
return this; | |
} | |
public LeadBuilder withLastName(String lastName) { | |
this.testLead.LastName = lastName; | |
return this; | |
} | |
public LeadBuilder withEmail(String email) { | |
this.testLead.Email = email; | |
return this; | |
} | |
public LeadBuilder withLeadType(String leadType) { | |
this.testLead.Lead_Type__c = leadType; | |
return this; | |
} | |
public LeadBuilder withStatus(String status) { | |
this.testLead.Status = status; | |
return this; | |
} | |
public Lead build() { | |
insert this.testLead; | |
return this.testLead; | |
} | |
@IsTest | |
private static void LeadBuilder_insertLead_Success() { | |
Test.startTest(); | |
Lead testLead = new LeadBuilder() | |
.withFirstName('test-FirstName') | |
.withLastName('test-LastName') | |
.withEmail('test@email.com') | |
.withLeadType('testLeadType') | |
.withStatus('New') | |
.build(); | |
Test.stopTest(); | |
System.assertNotEquals(null, | |
testLead.Id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now to create a simple lead I can do the minimum:
Lead simpleLead = new LeadBuilder().build();
or to set the Status to Qualified for a workflow scenario:
Lead qualifiedLead = new LeadBuilder().withStatus('Qualified').build();
and to set a specific recordtype:
Lead specificRecordTypeLead = new LeadBuilder().withRecordType('recordTypeLabel').build();
and I can stack those methods on top of each other to create a qualified lead with specific record type:
Lead specificRecordTypeLead = new LeadBuilder().withRecordType('recordTypeLabel').withStatus('Qualified').build();