Created
May 24, 2017 15:28
-
-
Save Walletau/63e9a684f4b64e3261d9fe01e35ba966 to your computer and use it in GitHub Desktop.
Build
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
public class Build { | |
//------------------------------------------------------------------------------ | |
//Account | |
public class AccountBuilder { | |
public AccountBuilder withName(String name) { | |
this.name = name; | |
return this; | |
} | |
String name; | |
public AccountBuilder withIndustry(String ind) { | |
industry = ind; | |
return this; | |
} | |
String industry; | |
public AccountBuilder withRecordType(Build.AccountRecordType recType) { | |
this.recordTypeId = accountRecordTypeSObjects.get(recType).Id; | |
return this; | |
} | |
String recordTypeId; | |
public AccountBuilder withParentAccount(Account parentAccount) { | |
this.parentAccount = parentAccount; | |
return this; | |
} | |
Account parentAccount; | |
// object to build | |
public Account build() { | |
Account builtAccount = (Account) SmartFactory.createSObject('Account', false); | |
builtAccount.Name = name != null ? name : builtAccount.Name; | |
builtAccount.Industry = industry != null ? industry : builtAccount.Industry; | |
builtAccount.ParentId = parentAccount != null ? parentAccount.Id : builtAccount.ParentId; | |
builtAccount.RecordTypeId = recordTypeId != null ? recordTypeId : builtAccount.RecordTypeId; | |
return builtAccount; | |
} | |
} | |
public static AccountBuilder anAccount() { | |
return new AccountBuilder(); | |
} | |
//------------------------------------------------------------------------------ | |
//Contact | |
public class ContactBuilder { | |
public ContactBuilder withName(String first, String last) { | |
fName = first; | |
lName = last; | |
return this; | |
} | |
String fName; | |
String lName; | |
public ContactBuilder withAccount(Account acct) { | |
this.contactAccount = acct; | |
return this; | |
} | |
Account contactAccount; | |
public ContactBuilder withMember(Member__c member) { | |
this.contactMemberId = member.Id; | |
return this; | |
} | |
String contactMemberId; | |
public ContactBuilder withRecordType(Build.ContactRecordType recordType) { | |
this.recordTypeId = contactRecTypeSObjects.get(recordType).Id; | |
return this; | |
} | |
String recordTypeId; | |
public ContactBuilder withBirthdate(Date birthDate) { | |
this.birthDate = birthDate; | |
return this; | |
} | |
Date birthDate; | |
public ContactBuilder withEmail(String email) { | |
this.email = email; | |
return this; | |
} | |
String email; | |
public ContactBuilder withCascade() { | |
SmartFactory.FillAllFields = true; | |
cascadingContact = (Contact) SmartFactory.createSObject('Contact', true); | |
return this; | |
} | |
Contact cascadingContact; | |
// object to build | |
public Contact build() { | |
SmartFactory.FillAllFields = true; | |
// this.recordTypeId = contactRecTypeSObjects.get(ContactRecordType.OrganisationContact).Id; | |
Contact builtContact = cascadingContact != null ? cascadingContact : (Contact) SmartFactory.createSObject('Contact', false); | |
builtContact.FirstName = fName != null ? fname : builtContact.FirstName; | |
builtContact.LastName = lName != null ? lName : builtContact.LastName; | |
builtContact.Email = email != null ? email : builtContact.Email; | |
builtContact.Birthdate = birthDate != null ? birthDate : builtContact.Birthdate; | |
builtContact.OrgResp_RelatedMember__c = contactMemberId != null ? contactMemberId : builtContact.OrgResp_RelatedMember__c; | |
builtContact.RecordTypeId = recordTypeId != null ? recordTypeId : builtContact.RecordTypeId; | |
builtContact.AccountId = contactAccount.Id != null ? contactAccount.Id : builtContact.AccountId; | |
return builtContact; | |
} | |
} | |
public static ContactBuilder aContact() { | |
return new ContactBuilder(); | |
} | |
//--------------------------------------------------------------------------------------------------------------------------- | |
// User | |
public class UserBuilder { | |
public UserBuilder withLastName(String lastName) { | |
this.lastName = lastName; | |
return this; | |
} | |
String lastName; | |
public UserBuilder withUserName(String usName) { | |
this.userName = usName + '@test.com'; | |
return this; | |
} | |
String userName; | |
public UserBuilder withProfile(String profileName) { | |
Profile profile = [ | |
SELECT Id | |
FROM Profile | |
WHERE Name = :profileName | |
]; | |
this.profileId = profile.Id; | |
return this; | |
} | |
String profileId; | |
public UserBuilder withContact(Contact contact) { | |
this.userContact = contact; | |
return this; | |
} | |
Contact userContact; | |
public User build() { | |
User builtUser = (User) SmartFactory.createSObject('User', false); | |
builtUser.LastName = lastName != null ? lastName : builtUser.LastName; | |
builtUser.UserName = userName != null ? userName : builtUser.UserName+'.stgtest'; | |
builtUser.ProfileId = profileId != null ? profileId : builtUser.ProfileId; | |
if (userContact != null) { | |
builtUser.ContactId = userContact.Id; | |
builtUser.FirstName = userContact.FirstName; | |
builtUser.LastName = userContact.LastName; | |
builtUser.Email = userContact.Email; | |
builtUser.Username = userContact.Email+'.stgtest'; | |
} | |
return builtUser; | |
} | |
} | |
public static UserBuilder aUser() { | |
return new UserBuilder(); | |
} | |
public static UserBuilder aAdminUser() { | |
UserBuilder testAdmin = new UserBuilder(). | |
withProfile('System Administrator'); | |
return testAdmin; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment