Test Class ---
This is my Apex class--
Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack ExchangeTeams
Learn more about TeamsTest Class ---
This is my Apex class--
Test Class ---
This is my Apex class--
Test Class
This is my Apex class
Please help me. Thanks in advance
Please help me. Thanks in advance
@isTest private class SandboxPostRefreshTestCase {
@isTest
private class SandboxPostRefreshTestCase {
static Testmethod void testSandboxPostCopyScript() {
Account accVar = new Account();
accVar.Name ='Account Test';
INSERT accVar;
Contact conVar = new Contact();
conVar.LastName ='Contact Test';
conVar.AccountId =accVar.Id;
conVar.Email = '[email protected]';
conVar.Other_Email__c = '[email protected]';
INSERT conVar;
Case casVar = new Case();
casVar.Origin ='Email';
casVar.Status = 'New';
// casVar.ContactEmail = '[email protected]';
casVar.SuppliedEmail = '[email protected]';
INSERT casVar;
Campaign camVar = new Campaign();
camVar.Name ='test';
INSERT camVar;
CampaignMember newMemberVar = new CampaignMember();
newMemberVar.CampaignId = camVar.Id;
newMemberVar.ContactId = conVar.Id;
// newMember.Email = '[email protected]';
INSERT newMemberVar;
Lead leadVar = new Lead();
leadVar.LastName = 'testsds';
leadVar.Status = 'New';
leadVar.Company = 'test';
leadVar.Email = '[email protected]';
INSERT leadVar;
Opportunity oppVar = new Opportunity();
oppVar.Name = 'testsds';
oppVar.AccountId = accVar.Id;
oppVar.CloseDate = date.today();
oppVar.StageName = 'Proposal';
INSERT oppVar;
Quote quoteVar = new Quote();
quoteVar.Name = 'testsds';
// quoteVar.Account = accVar.Id;
quoteVar.OpportunityId = oppVar.Id;
Test.startTest();
PrepareSandboxPostCopy_Batch obj = new PrepareSandboxPostCopy_Batch();
PrepareSandboxPostCopy_Batch.executeBatch();
// system.assertEquals(true, PrepareSandboxPostCopy_Batch.isSandbox());
Test.stopTest();
}
}
}
global class PrepareSandboxPostCopy_Batch implements Database.Batchable {
String query;
String sObjectName;
Map <String, List <String>> sObjectEmaiLFieldsMap = new Map <String, List <String>>();
//Call this method from theglobal class thatPrepareSandboxPostCopy_Batch implements the SandboxPostCopy Interface or from Developer console
global static void executeBatch() {
Map <String, List <String>> sObjectEmaiLFieldsMap = getsObjectEmaiLFields();
if(!sObjectEmaiLFieldsMap.isEmpty()) {
Database.executeBatch(new PrepareSandboxPostCopy_Batch(sObjectEmaiLFieldsMap));
}
}
/** BATCHABLE INTERFACE IMPLEMENTATION STARTS **/
global PrepareSandboxPostCopy_Batch(Map <String, List <String>> sObjectFieldsMap)Batchable<sObject> {
//Get First Key In Map
sObjectName = new List<String> (sObjectFieldsMap.keySet()).get(0);
sObjectEmaiLFieldsMap = sObjectFieldsMap;
query = getSOQLString(sObjectName, sObjectEmaiLFieldsMap.get(sObjectName));
}
global Database.QueryLocator start(Database.BatchableContext BC) {String query;
returnString Database.getQueryLocator(query);sObjectName;
} Map <String, List <String>> sObjectEmaiLFieldsMap = new Map <String, List <String>>();
//Call this method from the class that implements the SandboxPostCopy Interface or from Developer console
global static void executeexecuteBatch(Database.BatchableContext) BC{
Map <String, List<sObject>List scope)<String>> {sObjectEmaiLFieldsMap = getsObjectEmaiLFields();
for(sObject thisRecord : scope) {
forif(String thisField : !sObjectEmaiLFieldsMap.getisEmpty(sObjectName)) {
String email = (String) thisRecord.get(thisField);
if(String.isEmpty(email) || emailDatabase.containsexecuteBatch('.invalid') ||new email.containsPrepareSandboxPostCopy_Batch('.noemail'sObjectEmaiLFieldsMap)) continue; //If empty or if contains .invalid then do not update
thisRecord.put(thisField, email + '.invalid'); //Append invalid to the email
}
}
Database.update(scope, false); //allow partial success
}
global void finish(Database.BatchableContext BC) {
//Remove the sObject** (FirstBATCHABLE Key)INTERFACE onceIMPLEMENTATION theSTARTS records**/
for the sObject processedglobal successfully
PrepareSandboxPostCopy_Batch(Map <String, List <String>> sObjectEmaiLFieldsMap.remove(sObjectNamesObjectFieldsMap);
{
executeBatch(sObjectEmaiLFieldsMap);
}
/** BATCHABLE INTERFACE IMPLEMENTATION ENDS **/
public/Get staticFirst BooleanKey isSandbox()In {Map
return [SELECT IsSandbox FROM OrganizationsObjectName WHERE= Idnew =:List<String> UserInfo(sObjectFieldsMap.getOrganizationIdkeySet()]).IsSandbox;
}get(0);
private static void executeBatch(Map <String, List <String>> sObjectFieldsMap) {
sObjectEmaiLFieldsMap = sObjectFieldsMap;
if(!sObjectFieldsMap.isEmpty()) {
query = Database.executeBatchgetSOQLString(newsObjectName, PrepareSandboxPostCopy_BatchsObjectEmaiLFieldsMap.get(sObjectFieldsMapsObjectName));
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}
private static String getSOQLString global void execute(StringDatabase.BatchableContext sObjectNameBC, List<String>List<sObject> sObjectFieldsscope) {
for(sObject thisRecord : scope) {
for(String soqlQuerythisField =: 'SELECTsObjectEmaiLFieldsMap.get(sObjectName)) {!sObjectFields
String email = (String) thisRecord.get(thisField);
if(String.isEmpty(email) || email.contains('.invalid') || email.contains('.noemail')) continue; //If empty or if contains .invalid then do not update
thisRecord.put(thisField, email + '.invalid'); //Append invalid to the email
}
FROM {!objectTypeName }
WHERE Database.update(scope, false); //allow partial success
}
global void finish(Database.BatchableContext BC) {!conditionals
//Remove the sObject (First Key) once the records for the sObject processed successfully
sObjectEmaiLFieldsMap.remove(sObjectName);
executeBatch(sObjectEmaiLFieldsMap);
}';
/** BATCHABLE INTERFACE IMPLEMENTATION ENDS **/
List<String> conditionals =public newstatic ListBoolean <String>isSandbox(); {
for (String thisField: sObjectFields) {
return [SELECT IsSandbox FROM Organization WHERE Id =: conditionalsUserInfo.addgetOrganizationId(thisField + ' != NULL');].IsSandbox;
}
soqlQueryprivate =static soqlQuery.replace('{!sObjectFields}',void String.joinexecuteBatch(sObjectFieldsMap <String, 'List ,')<String>> sObjectFieldsMap); {
soqlQuery = soqlQuery if(!sObjectFieldsMap.replaceisEmpty(')) {!objectTypeName}',
sObjectName Database.executeBatch(new PrepareSandboxPostCopy_Batch(sObjectFieldsMap));
soqlQuery = soqlQuery.replace('{!conditionals }',
String.join(conditionals, ' OR '));}
if(!isSandboxprivate static String getSOQLString()String sObjectName, List<String> sObjectFields) soqlQuery{
= soqlQuery + '
LIMIT 0'; //For Non Sandbox (Production) Instance DoString NotsoqlQuery Update= Records'SELECT {!sObjectFields} FROM {!objectTypeName} WHERE {!conditionals}';
return soqlQuery; List<String> conditionals = new List <String>();
for (String thisField: sObjectFields) {
conditionals.add(thisField + ' != NULL');
}
soqlQuery = soqlQuery.replace('{!sObjectFields}', String.join(sObjectFields, ' ,'));
soqlQuery = soqlQuery.replace('{!objectTypeName}', sObjectName);
soqlQuery = soqlQuery.replace('{!conditionals}', String.join(conditionals, ' OR '));
//Get all Queryable Objects and it's email fields
private staticif(!isSandbox()) MapsoqlQuery <String,= ListsoqlQuery <String>>+ getsObjectEmaiLFields' LIMIT 0'; //For Non Sandbox (Production) {Instance Do Not Update Records
Map <String, List <String>> sObjectEmailFieldMap =return newsoqlQuery;
Map <String, List <String>>();}
//Only pass the selected object, dynamically gettingGet all objects may exceed APEX CPU Limit
List<SObjectType> sObjectsToUpdate = new List<SObjectType>{ Account.SObjectType,
Contact.SObjectType,
Case.SObjectType,
CampaignMember.SObjectType,
Queryable Objects and it's Lead.SObjectType,email fields
private static Map Opportunity.SObjectType<String,
List <String>> Quote.SObjectTypegetsObjectEmaiLFields() };{
for(SObjectType sObjectType : sObjectsToUpdate) {Map <String, List <String>> sObjectEmailFieldMap = new Map <String, List <String>>();
//DescribeOnly sObjectpass the selected object, dynamically getting all objects may exceed APEX CPU Limit
DescribeSObjectResultList<SObjectType> sObjectDescribesObjectsToUpdate = sObjectTypenew List<SObjectType>{ Account.getDescribe()SObjectType,
Contact.SObjectType,
Case.SObjectType,
CampaignMember.SObjectType,
Lead.SObjectType,
Opportunity.SObjectType,
Quote.SObjectType };
String objectTypeName = sObjectDescribe.getName();
//Skip objects we cannot query or update
if(!sObjectDescribe.isQueryable() || !sObjectDescribe.isUpdateable()) continue;
for(SObjectFieldSObjectType sObjectFieldsObjectType : sObjectDescribe.fields.getMap().values()sObjectsToUpdate) {
DescribeFieldResult//Describe thisFieldsObject
DescribeSObjectResult sObjectDescribe = sObjectFieldsObjectType.getDescribe();
String objectTypeName = sObjectDescribe.getName();
//Skip allobjects nonwe emailcannot fieldsquery or update
if(thisField.getType() != Schema.DisplayType.EMAIL || !thisFieldsObjectDescribe.isFilterableisQueryable() || !thisFieldsObjectDescribe.isUpdateable()) continue;
for(SObjectField sObjectField: sObjectDescribe.fields.getMap().values()) {
DescribeFieldResult thisField = sObjectField.getDescribe();
//Skip all non email fields
if(thisField.getType() != Schema.DisplayType.EMAIL || !thisField.isFilterable() || !thisField.isUpdateable()) continue;
if(sObjectEmailFieldMap.containsKey(objectTypeName)) {
sObjectEmailFieldMap.get(objectTypeName).add(thisField.getName());
}
else {
sObjectEmailFieldMap.put(objectTypeName, new List <String> {thisField.getName()});
}
}
}
return sObjectEmailFieldMap;
}
return sObjectEmailFieldMap;
}
}
@isTest private class SandboxPostRefreshTestCase {
static Testmethod void testSandboxPostCopyScript() {
Account accVar = new Account();
accVar.Name ='Account Test';
INSERT accVar;
Contact conVar = new Contact();
conVar.LastName ='Contact Test';
conVar.AccountId =accVar.Id;
conVar.Email = '[email protected]';
conVar.Other_Email__c = '[email protected]';
INSERT conVar;
Case casVar = new Case();
casVar.Origin ='Email';
casVar.Status = 'New';
// casVar.ContactEmail = '[email protected]';
casVar.SuppliedEmail = '[email protected]';
INSERT casVar;
Campaign camVar = new Campaign();
camVar.Name ='test';
INSERT camVar;
CampaignMember newMemberVar = new CampaignMember();
newMemberVar.CampaignId = camVar.Id;
newMemberVar.ContactId = conVar.Id;
// newMember.Email = '[email protected]';
INSERT newMemberVar;
Lead leadVar = new Lead();
leadVar.LastName = 'testsds';
leadVar.Status = 'New';
leadVar.Company = 'test';
leadVar.Email = '[email protected]';
INSERT leadVar;
Opportunity oppVar = new Opportunity();
oppVar.Name = 'testsds';
oppVar.AccountId = accVar.Id;
oppVar.CloseDate = date.today();
oppVar.StageName = 'Proposal';
INSERT oppVar;
Quote quoteVar = new Quote();
quoteVar.Name = 'testsds';
// quoteVar.Account = accVar.Id;
quoteVar.OpportunityId = oppVar.Id;
Test.startTest();
PrepareSandboxPostCopy_Batch obj = new PrepareSandboxPostCopy_Batch();
PrepareSandboxPostCopy_Batch.executeBatch();
// system.assertEquals(true, PrepareSandboxPostCopy_Batch.isSandbox());
Test.stopTest();
}
}
global class PrepareSandboxPostCopy_Batch implements Database.Batchable {
String query;
String sObjectName;
Map <String, List <String>> sObjectEmaiLFieldsMap = new Map <String, List <String>>();
//Call this method from the class that implements the SandboxPostCopy Interface or from Developer console
global static void executeBatch() {
Map <String, List <String>> sObjectEmaiLFieldsMap = getsObjectEmaiLFields();
if(!sObjectEmaiLFieldsMap.isEmpty()) {
Database.executeBatch(new PrepareSandboxPostCopy_Batch(sObjectEmaiLFieldsMap));
}
}
/** BATCHABLE INTERFACE IMPLEMENTATION STARTS **/
global PrepareSandboxPostCopy_Batch(Map <String, List <String>> sObjectFieldsMap) {
//Get First Key In Map
sObjectName = new List<String> (sObjectFieldsMap.keySet()).get(0);
sObjectEmaiLFieldsMap = sObjectFieldsMap;
query = getSOQLString(sObjectName, sObjectEmaiLFieldsMap.get(sObjectName));
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
for(sObject thisRecord : scope) {
for(String thisField : sObjectEmaiLFieldsMap.get(sObjectName)) {
String email = (String) thisRecord.get(thisField);
if(String.isEmpty(email) || email.contains('.invalid') || email.contains('.noemail')) continue; //If empty or if contains .invalid then do not update
thisRecord.put(thisField, email + '.invalid'); //Append invalid to the email
}
}
Database.update(scope, false); //allow partial success
}
global void finish(Database.BatchableContext BC) {
//Remove the sObject (First Key) once the records for the sObject processed successfully
sObjectEmaiLFieldsMap.remove(sObjectName);
executeBatch(sObjectEmaiLFieldsMap);
}
/** BATCHABLE INTERFACE IMPLEMENTATION ENDS **/
public static Boolean isSandbox() {
return [SELECT IsSandbox FROM Organization WHERE Id =: UserInfo.getOrganizationId()].IsSandbox;
}
private static void executeBatch(Map <String, List <String>> sObjectFieldsMap) {
if(!sObjectFieldsMap.isEmpty()) {
Database.executeBatch(new PrepareSandboxPostCopy_Batch(sObjectFieldsMap));
}
}
private static String getSOQLString(String sObjectName, List<String> sObjectFields) {
String soqlQuery = 'SELECT {!sObjectFields} FROM {!objectTypeName} WHERE {!conditionals}';
List<String> conditionals = new List <String>();
for (String thisField: sObjectFields) {
conditionals.add(thisField + ' != NULL');
}
soqlQuery = soqlQuery.replace('{!sObjectFields}', String.join(sObjectFields, ' ,'));
soqlQuery = soqlQuery.replace('{!objectTypeName}', sObjectName);
soqlQuery = soqlQuery.replace('{!conditionals}', String.join(conditionals, ' OR '));
if(!isSandbox()) soqlQuery = soqlQuery + ' LIMIT 0'; //For Non Sandbox (Production) Instance Do Not Update Records
return soqlQuery;
}
//Get all Queryable Objects and it's email fields
private static Map <String, List <String>> getsObjectEmaiLFields() {
Map <String, List <String>> sObjectEmailFieldMap = new Map <String, List <String>>();
//Only pass the selected object, dynamically getting all objects may exceed APEX CPU Limit
List<SObjectType> sObjectsToUpdate = new List<SObjectType>{ Account.SObjectType,
Contact.SObjectType,
Case.SObjectType,
CampaignMember.SObjectType,
Lead.SObjectType,
Opportunity.SObjectType,
Quote.SObjectType };
for(SObjectType sObjectType : sObjectsToUpdate) {
//Describe sObject
DescribeSObjectResult sObjectDescribe = sObjectType.getDescribe();
String objectTypeName = sObjectDescribe.getName();
//Skip objects we cannot query or update
if(!sObjectDescribe.isQueryable() || !sObjectDescribe.isUpdateable()) continue;
for(SObjectField sObjectField: sObjectDescribe.fields.getMap().values()) {
DescribeFieldResult thisField = sObjectField.getDescribe();
//Skip all non email fields
if(thisField.getType() != Schema.DisplayType.EMAIL || !thisField.isFilterable() || !thisField.isUpdateable()) continue;
if(sObjectEmailFieldMap.containsKey(objectTypeName)) {
sObjectEmailFieldMap.get(objectTypeName).add(thisField.getName());
}
else {
sObjectEmailFieldMap.put(objectTypeName, new List <String> {thisField.getName()});
}
}
}
return sObjectEmailFieldMap;
}
}
@isTest
private class SandboxPostRefreshTestCase {
static Testmethod void testSandboxPostCopyScript() {
Account accVar = new Account();
accVar.Name ='Account Test';
INSERT accVar;
Contact conVar = new Contact();
conVar.LastName ='Contact Test';
conVar.AccountId =accVar.Id;
conVar.Email = '[email protected]';
conVar.Other_Email__c = '[email protected]';
INSERT conVar;
Case casVar = new Case();
casVar.Origin ='Email';
casVar.Status = 'New';
// casVar.ContactEmail = '[email protected]';
casVar.SuppliedEmail = '[email protected]';
INSERT casVar;
Campaign camVar = new Campaign();
camVar.Name ='test';
INSERT camVar;
CampaignMember newMemberVar = new CampaignMember();
newMemberVar.CampaignId = camVar.Id;
newMemberVar.ContactId = conVar.Id;
// newMember.Email = '[email protected]';
INSERT newMemberVar;
Lead leadVar = new Lead();
leadVar.LastName = 'testsds';
leadVar.Status = 'New';
leadVar.Company = 'test';
leadVar.Email = '[email protected]';
INSERT leadVar;
Opportunity oppVar = new Opportunity();
oppVar.Name = 'testsds';
oppVar.AccountId = accVar.Id;
oppVar.CloseDate = date.today();
oppVar.StageName = 'Proposal';
INSERT oppVar;
Quote quoteVar = new Quote();
quoteVar.Name = 'testsds';
// quoteVar.Account = accVar.Id;
quoteVar.OpportunityId = oppVar.Id;
Test.startTest();
PrepareSandboxPostCopy_Batch obj = new PrepareSandboxPostCopy_Batch();
PrepareSandboxPostCopy_Batch.executeBatch();
// system.assertEquals(true, PrepareSandboxPostCopy_Batch.isSandbox());
Test.stopTest();
}
}
global class PrepareSandboxPostCopy_Batch implements Database.Batchable<sObject> {
String query;
String sObjectName;
Map <String, List <String>> sObjectEmaiLFieldsMap = new Map <String, List <String>>();
//Call this method from the class that implements the SandboxPostCopy Interface or from Developer console
global static void executeBatch() {
Map <String, List <String>> sObjectEmaiLFieldsMap = getsObjectEmaiLFields();
if(!sObjectEmaiLFieldsMap.isEmpty()) {
Database.executeBatch(new PrepareSandboxPostCopy_Batch(sObjectEmaiLFieldsMap));
}
}
/** BATCHABLE INTERFACE IMPLEMENTATION STARTS **/
global PrepareSandboxPostCopy_Batch(Map <String, List <String>> sObjectFieldsMap) {
//Get First Key In Map
sObjectName = new List<String> (sObjectFieldsMap.keySet()).get(0);
sObjectEmaiLFieldsMap = sObjectFieldsMap;
query = getSOQLString(sObjectName, sObjectEmaiLFieldsMap.get(sObjectName));
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
for(sObject thisRecord : scope) {
for(String thisField : sObjectEmaiLFieldsMap.get(sObjectName)) {
String email = (String) thisRecord.get(thisField);
if(String.isEmpty(email) || email.contains('.invalid') || email.contains('.noemail')) continue; //If empty or if contains .invalid then do not update
thisRecord.put(thisField, email + '.invalid'); //Append invalid to the email
}
}
Database.update(scope, false); //allow partial success
}
global void finish(Database.BatchableContext BC) {
//Remove the sObject (First Key) once the records for the sObject processed successfully
sObjectEmaiLFieldsMap.remove(sObjectName);
executeBatch(sObjectEmaiLFieldsMap);
}
/** BATCHABLE INTERFACE IMPLEMENTATION ENDS **/
public static Boolean isSandbox() {
return [SELECT IsSandbox FROM Organization WHERE Id =: UserInfo.getOrganizationId()].IsSandbox;
}
private static void executeBatch(Map <String, List <String>> sObjectFieldsMap) {
if(!sObjectFieldsMap.isEmpty()) {
Database.executeBatch(new PrepareSandboxPostCopy_Batch(sObjectFieldsMap));
}
}
private static String getSOQLString(String sObjectName, List<String> sObjectFields) {
String soqlQuery = 'SELECT {!sObjectFields} FROM {!objectTypeName} WHERE {!conditionals}';
List<String> conditionals = new List <String>();
for (String thisField: sObjectFields) {
conditionals.add(thisField + ' != NULL');
}
soqlQuery = soqlQuery.replace('{!sObjectFields}', String.join(sObjectFields, ' ,'));
soqlQuery = soqlQuery.replace('{!objectTypeName}', sObjectName);
soqlQuery = soqlQuery.replace('{!conditionals}', String.join(conditionals, ' OR '));
if(!isSandbox()) soqlQuery = soqlQuery + ' LIMIT 0'; //For Non Sandbox (Production) Instance Do Not Update Records
return soqlQuery;
}
//Get all Queryable Objects and it's email fields
private static Map <String, List <String>> getsObjectEmaiLFields() {
Map <String, List <String>> sObjectEmailFieldMap = new Map <String, List <String>>();
//Only pass the selected object, dynamically getting all objects may exceed APEX CPU Limit
List<SObjectType> sObjectsToUpdate = new List<SObjectType>{ Account.SObjectType,
Contact.SObjectType,
Case.SObjectType,
CampaignMember.SObjectType,
Lead.SObjectType,
Opportunity.SObjectType,
Quote.SObjectType };
for(SObjectType sObjectType : sObjectsToUpdate) {
//Describe sObject
DescribeSObjectResult sObjectDescribe = sObjectType.getDescribe();
String objectTypeName = sObjectDescribe.getName();
//Skip objects we cannot query or update
if(!sObjectDescribe.isQueryable() || !sObjectDescribe.isUpdateable()) continue;
for(SObjectField sObjectField: sObjectDescribe.fields.getMap().values()) {
DescribeFieldResult thisField = sObjectField.getDescribe();
//Skip all non email fields
if(thisField.getType() != Schema.DisplayType.EMAIL || !thisField.isFilterable() || !thisField.isUpdateable()) continue;
if(sObjectEmailFieldMap.containsKey(objectTypeName)) {
sObjectEmailFieldMap.get(objectTypeName).add(thisField.getName());
}
else {
sObjectEmailFieldMap.put(objectTypeName, new List <String> {thisField.getName()});
}
}
}
return sObjectEmailFieldMap;
}
}